I'm rather confused about how the multiply and divide operations work in x86 assembly. For example, the code below doesn't seem too difficult since deals with 8-bit.
8-Bit Multiplication:
; User Input:
; [num1], 20
; [num2] , 15
mov ax, [num1] ; moves the 8 bits into AL
mov bx, [num2] ; moves the 8 bits into BL
mul bl ; product stored in AX
print ax
But what happens when you want to multiply two 16-bit numbers? How would one multiply two 16 bit numbers the same way as it has been done with the 8 bit numbers?
I'm confused as to what registers the values would be stored in. Would they be stored in AL and AH or would it simply store the 16-bit number in AX. To show what I mean:
; User Input:
; [num1], 20
; [num2], 15
mov eax, [num1] ; Does this store the 16-bit number in AL and AH or just in AX
mov ebx, [num2] ; Does this store the 16-bit number in BL and BH or just in BX
mul ??? ; this register relies on where the 16-bit numbers are stored
print eax
Could someone elaborate a bit on how the multiplying and dividing works? (specifically with 16-bit and 32-bit numbers? Would I need to rotate bits if the values are stored in the lower AL and AH?
Or can one simply mov num1
and num2
into ax
and bx
respectively and then multiply them to get the product in eax
?
A quick glance at the documentation shows that there are 4 possible operand sizes for MUL
. The inputs and outputs are summarized in a handy table:
------------------------------------------------------
| Operand Size | Source 1 | Source 2 | Destination |
------------------------------------------------------
| Byte | AL | r/m8 | AX |
| Word | AX | r/m16 | DX:AX |
| Doubleword | EAX | r/m32 | EDX:EAX |
| Quadword | RAX | r/m64 | RDX:RAX |
------------------------------------------------------