Reputation:
I have a program that multiplies 3 numbers and i am trying to understand. I have some questions and i am hoping someone can explain whats going on with the program and tell me if i am on the right track. I understand i have more than one question so i am sorry about that.
.data?
num1 dd ?
num2 dd ?
num3 dd ?
.data
sum dd 0
prod dd 0
.code
start:
main proc
mov EAX, sval(input("Enter a number: "))
mov num1, EAX
mov EAX, sval(input("Enter a number: "))
mov num2, EAX
mov EAX, sval(input("Enter a number: "))
mov num3, EAX
mov EAX, num1
mov EBX, num2
mul BL
mov EBX, num3
mul BX
mov prod, EDX
this has me confused...
mov EBX, num3
mul BX
so, we are storing num3 into BL? but since the result of num1 and num2 is 16 bit and stored into AX we mul BX? instead of BL? but isnt num3 in BL?
im sorry there isnt one specific question. If my logic is incorrect or close can you explain whats going on piece by piece and why?
Thank you
Upvotes: 1
Views: 4921
Reputation: 490148
When you do an 8-bit multiplication like mul bl
, it takes al
, multiplies it by the specified input, and puts the result in ax
.
When you do a 16-bit multiplication like mul bx
, it takes ax
, multiplies it by the specified input, and puts the result in dx:ax
(i.e., the 16 most significant bits of the result in dx
, and the 16 least significant bits of the result in ax
).
(Just for completeness): if you do a 32-bit multiplication like mul ebx
, it multiplies eax
by ebx
, and puts the result in edx:eax
(offhand I don't remember for sure, but I'd guess 64-bit multiplication works about the same way).
As far as BL
vs. BX
goes (or AL
/AH
/AX
, etc.), what you have aren't really separate registers -- AL
is really the 8 least significant bits of AX
. AH
is the 8 most significant bits of AX. BL is the 8 least significant bits of BX, and BH is the 8 most significant bits of BX (and so on for CL/CH/DL/DH).
For example, if you execute code like:
xor bx, bx
mov bl, 1
BH will then equal 0 (set to zero by the xor bx, bx
), BL will equal 1 (set by the mov) and bx will also equal 1 (because it now contains the bit pattern 00000000 00000001).
Upvotes: 3