16-bit itoa funtion (AT&T ASM)
Posted: Thu Dec 04, 2008 10:55 am
I'm having a problem with an itoa style function for use in a bootloader made in AT&T assembly. Heres the function code:
and heres an example function call:
when tested in bochs, the function causes an infinite loop:
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...
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
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 " "
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
...
...
...
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...