Page 1 of 1

16-bit itoa funtion (AT&T ASM)

Posted: Thu Dec 04, 2008 10:55 am
by furryfreak
I'm having a problem with an itoa style function for use in a bootloader made in AT&T assembly. Heres the function code:

Code: Select all

rm_itoa:
	pushw	%bp
	movw	%sp, %bp
	movw	4(%bp), %ax
	movw	6(%bp), %di
	movw	%di, %si
	movb	8(%bp), %bl
	movb	$0, %bh
0:
	divw	%bx
	cmpb	$9, %dl
	jg		1f
	addb	$48, %dl
	jmp		2f
1:
	addb	$55, %dl
2:
	movb	%dl, (%di)
	cmpw	$0, %ax
	je		3f
	addw	$1, %di
	movw	$0, %dx
	jmp		0b
3:
	movb	$0, 1(%di)
4:
	cmpw	%di, %si
	jge		5f
	movb	(%di), %al
	movb	(%si), %ah
	movb	%al, (%si)
	movb	%ah, (%di)
	addw	$1, %si
	subw	$1, %di
	jmp		4b
5:
	popw	%bp
	ret
and heres an example function call:

Code: Select all

	movw	%sp, %bp
	subw		$5, %sp
	movb	$10, -1(%bp)
	movw	$(buffer-_start), -3(%bp)
	movw	$50, -5(%bp)
	call		rm_itoa
buffer:	.ascii "        "
when tested in bochs, the function causes an infinite loop:

Code: Select all

(0) [0x00007d9f] 07c0:019f (unk. ctxt): div ax, bx                ; f7f3
(0) [0x000fff53] f000:ff53 (unk. ctxt): iret                      ; cf
(0) [0x00007d9f] 07c0:019f (unk. ctxt): div ax, bx                ; f7f3
(0) [0x000fff53] f000:ff53 (unk. ctxt): iret                      ; cf
...
...
...
stack and registers:

0x0000000000007bf0: 0xc0 0x07 0x46 0x00 0xff 0x7b 0x72 0x02
0x0000000000007bf8: 0x06 0x00 0x70 0x7f 0x0a 0x06 0x00 0x00

SP=7bf4
BP=7bf4
BX=000a
AX=0006

this is really confusing me, i cant see any reason for the error :? , I could understand if i were dividing by 0, but I'm not... :?

Re: 16-bit itoa funtion (AT&T ASM)

Posted: Thu Dec 04, 2008 11:13 am
by Combuster
furryfreak wrote:I could understand if i were dividing by 0, but I'm not... :?
Yes you are. At least the divide exception is generated. Have you seen where the instruction after the div is located?

note that div ax, bx isn't quite the valid instruction - its div bx, or div dx:ax, bx, which doen't do what you expect because dx isn't zero.

Re: 16-bit itoa funtion (AT&T ASM)

Posted: Thu Dec 04, 2008 1:20 pm
by i586coder
Hi,

first of all, i don't know any things about AT&T assembly, i use INTEL syntax using TASM,anyway, if you have problem writing code in
assembly ?
no problem, just write your problem in C or C++ and convert it to assembly by compiler, like me in some matters,
look,

Code: Select all

void itoa(int num, unsigned int base, char* buffer){
	unsigned int i=0,r=0;
	char tmp[32];
	if(num<0) *buffer++='-';
	num=abs(num);
	if(num < base){
		*buffer++=hexdigit(num);
		*buffer=0;
		return;
	}
	if(num >= base)
		while(num){
			r=num % base;
			num /= base;
			tmp[i++]=hexdigit(r);	
		}
	i--;
	for(; i>=0; i--) *buffer++=tmp[i];
	*buffer=0;
}

your problem look like this in C, in turbo c use:
TCC -S [fileName.C]
this will generate INTEL-ASM code,


finaly, my advice is: somehow convert C code to AT&T asm by setting the right parameters in GCC

that's all

Good luck,
a.T.D

Re: 16-bit itoa funtion (AT&T ASM)

Posted: Thu Dec 04, 2008 1:57 pm
by DeletedAccount
Hi ,
My friend that is not really recommended all the time ,coz compiler generates labels and symbols without much meaning and leads to unreadable code . It is sometimes better to write it "by hand" at times because it leads to more readable code .FYI -> even for gcc its the same command line argument.

Regards
Shrek

Re: 16-bit itoa funtion (AT&T ASM)

Posted: Thu Dec 04, 2008 2:55 pm
by furryfreak
just write your problem in C or C++ and convert it to assembly by compiler
unfortunately, there are very few 16-bit C compilers, the ones that are available are pretty old, so they cant be linked with elf64 (in my case this is a necessity). Anyways, I don't have a problem with assembly, just this particular conundrum.

Re: 16-bit itoa funtion (AT&T ASM)

Posted: Thu Dec 04, 2008 3:21 pm
by furryfreak
cheers Combuster, you're a saint, damn I cant believe i didn't see that, just had to add a movw $0, %dx before the loop.

Re: 16-bit itoa funtion (AT&T ASM)

Posted: Thu Dec 04, 2008 3:56 pm
by CodeCat
Tip: If your code doesn't have to run on a 286 or older, you can use 32 bit registers in real mode. ;)

Re: 16-bit itoa funtion (AT&T ASM)

Posted: Thu Dec 04, 2008 5:47 pm
by tantrikwizard
furryfreak wrote:
just write your problem in C or C++ and convert it to assembly by compiler
unfortunately, there are very few 16-bit C compilers, the ones that are available are pretty old, so they cant be linked with elf64 (in my case this is a necessity). Anyways, I don't have a problem with assembly, just this particular conundrum.
Try open watcom, it has 16 bit compilers and links elf. (and its currently maintained)