16-bit itoa funtion (AT&T ASM)

Programming, for all ages and all languages.
Post Reply
User avatar
furryfreak
Member
Member
Posts: 28
Joined: Mon Nov 03, 2008 12:45 pm
Location: SW England

16-bit itoa funtion (AT&T ASM)

Post 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... :?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

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

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
i586coder
Member
Member
Posts: 143
Joined: Sat Sep 20, 2008 6:43 am

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

Post 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
Distance doesn't make you any smaller,
but it does make you part of a larger picture.
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

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

Post 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
User avatar
furryfreak
Member
Member
Posts: 28
Joined: Mon Nov 03, 2008 12:45 pm
Location: SW England

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

Post 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.
User avatar
furryfreak
Member
Member
Posts: 28
Joined: Mon Nov 03, 2008 12:45 pm
Location: SW England

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

Post 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.
CodeCat
Member
Member
Posts: 158
Joined: Tue Sep 23, 2008 1:45 pm
Location: Eindhoven, Netherlands

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

Post 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. ;)
tantrikwizard
Member
Member
Posts: 153
Joined: Sun Jan 07, 2007 9:40 am
Contact:

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

Post 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)
Post Reply