x86 assembly multiply and divide instruction operands, 16-bit and higher
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 |
------------------------------------------------------
StartingGroovy
Started with basic Java & Groovy. Aiming to learn more about programming in general as well as C++.
Updated on July 14, 2022Comments
-
StartingGroovy almost 2 years
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
andnum2
intoax
andbx
respectively and then multiply them to get the product ineax
? -
StartingGroovy about 12 yearsI looked at that, and was aware of where the value is stored after the
mul
. I'm wondering if, when I store the value ineax
(prior tomul
) if it stores it in the lower bits (AL and AH) rather than the higher bits (AX). If it does store them in the lower bits, am I able to rotate them to AX and then just multiply AX and BX and print out the answer (I believe it would be in EAX)? -
Carl Norum about 12 yearsIf you store a value in
eax
it's in all ofeax
. I'm not sure I understand. I think you might be confusing which parts ofeax
areah
,al
, andax
, too. You can see the chart in volume 1 of the same documentation. In particular,ax
refers to the same 16 bits asah:al
, not the "upper" 16-bits ofeax
. -
StartingGroovy about 12 yearsAh you are correct, that is what I was confused about. So essentially what I would want to do is multiply
AX
byBX
and my product should be inEAX
? -
Carl Norum about 12 yearsClose, but the product will be in
DX:AX
. Check the table in my answer. -
StartingGroovy about 12 yearsSo multiplying two 16-bit numbers doesn't make a 32-bit number? (This would also be why I was confused with the chart. I thought multiplying two 16-bit numbers would make a 32-bit number)
-
Carl Norum about 12 yearsSure it does - DX is 16 bits, and AX is 16 bits. That makes DX:AX a 32-bit number. 16+16=32, right?
-
StartingGroovy about 12 yearsWow, sometimes I think I'm half blind when trying to read documentation. I assumed that meant it stored the values in AX
OR
DX not both. Last question before I accept your answer: So if I want to print this out, would I move DX into EAX rotate left and then move AX in as well and print EAX? -
Carl Norum about 12 yearsAX is already there - it's the bottom 16 bits of EAX. You would need to move the DX half up into the top half of EAX, yes.