Reputation: 41
I have to take 16 bit as an input as 1234 and tried to display it. But it gives output as 4660. I tried to store digit by digit because when I take input, it will get stored in al
as in ASCII form. After that I tried to shift the whole bit in al
to left using shift left(SHL
) operation which will give me 10 in al
if I have inserted 1. After that I inserted second digit and performing shift and rotate operation, I tried to make it in 02 form if the second digit stored is 2. Further, I performed OR
operation on 10 and 02 which are stored in register. I repeated the same process for storing lower 8-bit number. But the output is different.
.model small
.stack 100h
.data
.code
main proc
mov ax,@data
mov ds,ax
;taking 16 bit number input
mov ah,01h
int 21h
mov bh,al
mov cl,4
shl bh,cl
mov ah,01h
int 21h
mov cl,4
shl al,cl
mov cl,4
ror al,cl
or bh,al
mov ah,01h
int 21h
mov bl,al
mov cl,4
shl bl,cl
mov ah,01h
int 21h
mov cl,4
shl al,cl
mov cl,4
ror al,cl
or bl,al
;taking 16 bit number input
;displaying number in dos
mov ax,bx
mov bx,10
xor cx,cx
.a:
xor dx,dx
div bx
push dx
inc cx
test ax,ax
jnz .a
.b:
pop dx
add dl,"0"
mov ah,02h
int 21h
loop .b
exit:
mov ah,4ch
int 21h
main endp
end main
Upvotes: 2
Views: 1533
Reputation: 364210
After converting ASCII digits to integer, you're packing the 4-bit nibbles together into BCD (Bindary Coded Decimal), rather than multiplying by decimal place-values (powers of 10) to make a binary integer.
4660 (decimal) = 0x1234 so you did correctly implement your BCD packing, but that's not what you want to do in the first place.
Left-shift by 4 is equivalent to multiply by 16, not 10.
NASM Assembly convert input to integer? for string->binary integer using the standard
total = total*10 + digit
algorithm, starting with the most-significant digit.
That Q&A has 32-bit code, but the algorithm itself is easy enough to implement once you understand it. Or you can search for other Q&As on stack overflow (e.g. using google if the built-in search doesn't help) with 16-bit code.
Upvotes: 3