Reputation: 41
I am beginner in assembly and I need help for this conversion.The code below should convert from a string read from keyboard with the interrupt 01h.I know it is incorrect but can you help me identify the mistake?
mov dx,0
convert:
sub al,48 ;to covert to int
mov bl,al ;in bl will be the digit read
mov ax,dx
mul ten ;ax will store the old result multiplied by 10
mov bh,0
add ax,bx
mov dx,ax
Upvotes: 0
Views: 15388
Reputation: 11018
mul
comes in 2 flavors:
AL
and mul
's operand); store the 16-bit result in AX
AX
and mul
's operand); store the 32-bit result in DX AX
Your question is a bit vague, but I assume you want the first flavor, but instead you found yourself faced with the second.
To tell the assember you want the first flavor, give mul
an operand that is unmistakably 8-bit. One way that certainly works on all assemblers, is to use an 8-bit register, for example BH
(I chose that one because it is obvious its value is irrelevant at the time of mul
since it is overwritten shortly after).
sub al,48 ; to covert to int
mov bl,al ; in bl will be the digit read
mov ax,dx
mov bh,10 ; use BH to hold factor 10
mul bh ; multiply AL by BH; the product is stored in AX; DX is unaffected
mov bh,0
add ax,bx
mov dx,ax
EDIT:
I just realized this will limit the range of numbers that can be entered to 0...255, which is probably not what you want. Use user3628942's solution instead; it allows you to enter numbers up to 65535.
As often, there are other ways. Below is a solution that uses add
instead of mul
. Many years ago, this was a popular trick for processor architectures where mul
was either an expensive (i.e. slow) instruction, or totally absent. Works for numbers up to 65535; silently wraps to zero for higher numbers.
sub al,48 ; ASCII value --> numeric value
mov ah,0 ; AX = numeric value of digit
add dx,dx ; DX = 2 * original DX
add ax,dx ; AX = 2 * original DX + digit
add dx,dx ; DX = 4 * original DX
add dx,dx ; DX = 8 * original DX
add dx,ax ; DX = 10 * original DX + digit
Upvotes: 0
Reputation: 9899
mul ten ;ax will store the old result multiplied by 10
From the comment I understand that ten is a word sized variable containing the value 10.
This means that the multiply is word sized and thus overwrites DX.
Solution : Change DX to p.e. CX
mov cx,0
convert:
sub al,48 ;to covert to int
mov bl,al ;in bl will be the digit read
mov ax,cx
mul ten ;ax will store the old result multiplied by 10
mov bh,0
add ax,bx
mov cx,ax
Upvotes: 1