Reputation: 25
Hi I am beginner in assembly language , can any one help me to understand how to deal with numbers in assembly language x86.
I face difficulty when I handle any task related with numbers, I can't execute programs like shl
, shr
, mul
, div
two digit addition if any one can explain or share code for the following I will be very thankful
Like :
Mov Al 2
Mov Bx 2
mul bx
mov dx,bx
mov ah,2
int 21h
But it can not show correct output it was shown in symbols if I covert it through subtracting 30h
even that cant show correct answer.
Upvotes: 0
Views: 5726
Reputation: 39216
I can't execute programs like
shl
,shr
,mul
,div
shl
, shr
, mul
, div
are instructions. Please don't refer to them as programs.
... I covert it through subtracting 30h ...
See in the code below that the conversion requires you to add 30h instead of subtracting 30h.
Mov Al 2 Mov Bx 2 mul bx mov dx,bx mov ah,2 int 21h
You can correct this program that multiplies 2 single digit numbers.
Error 1 : Choose the correct size multiplication. Using mul bx
, you've calculated DX:AX = AX * BX
which is overkill in this case. Moreover since you forgot to zero AH
, you got random results!
mov al, 2
mov bl, 2
mul bl ; -> AX = AL * BL
Error 2 : Convert the result which is the number 4 ( 2 * 2 ) into the character "4".
Also notice that the result of the mul
instruction is not to be found in the BX
register, like your code is thinking (mov dx, bx
).
mul bl
leaves its result in AX
, and since the value is so small, we need only use AL
(the low half of AX
)
add al, "0" ;"0" = 30h
mov dl, al
mov ah, 02h
int 21h
It's important to see that this very simple way of displaying the result can only work for results varying from 0 to 9.
To display numbers that are outside this [0,9] range take a look at Displaying numbers with DOS. The text explains in great detail how you can convert any number into its textual representation and output that to the screen.
Upvotes: 1