Reading 5-digit decimal number in x86 assembly
I am writing a number converter between the (b h d) numbering systems , the program accepts 16 bits binary number , or 4 digits hex. or 5 decimal.
the reading procedure i wrote seems to have a problem when the decimal value is above 65535 (FFFFh) since im dealing with 16 bit registers and it cant contain larger values
i would appreciate it if you help me.
here is my read procedure:
Proc R
mov ah,01;read first digit
int 21h
mov saveal,al
cmp al,0dh; if it is a new line break then dont read
jz toret
mov al,radex ; 开发者_开发问答the radex value already entered by user
mov ah,0
mul dx
mov dx,ax; multiplies the radex by the number entered so far to shift it 1 dig.
mov al,saveal
cmp al,65
jge big2
sub al,30h; taking decimal value of the character
cont2:
call checkerror
mov ah,0
add dx,ax; adding the digit to the number read so far
loop R
toret:
ret
endp
Thanks Nataly
you need more bits to go above 65536 (0xFFFF) so you need a larger register, 32 bit, or another 16 bit register. Set that other 16 bit register to zero and after your add to add the digits so far, put an add with carry to add the carry bit into this next register.
So 0xFFFF + 5 for example is 0x10004 or 0x0004 with the carry bit set, take this other register add the carry bit to it to pick up that other bit, now you have 0x0001 in the high order register and 0x0004 in the low order register.
Absolutely no difference than if you were to do addition with pencil and paper. 99+5 = 04 with a "carry the one" to the hundreds place. And when the hundreds place overflows you carry to the next place. binary is the same just much easier. Each bit column is like a decimal column when you do it on paper, it is just invisible from bit 0 to bit 15, but the carry from bit 15 is visible so you can chain adders together and make them as arbitrarily wide as you wish.
精彩评论