Jordan Mackie
Jordan Mackie

Reputation: 2406

What does the x86 line "imulq $44,(%rbx), %rax" do?

I understand that imul is signed multiplication, and the documentation seems to suggest that the three operand syntax is imulq dest, source1, source2. Therefore the line would read as: Multiply the contents of the address stored in %rbx and the value in %rax, then store it in memory location 44, but this surely isn't correct?

Upvotes: 2

Views: 12646

Answers (2)

Daniel Kamil Kozar
Daniel Kamil Kozar

Reputation: 19286

You're using the three-operand variant of the imul instruction, which is defined in the instruction set reference as IMUL r64, r/m64, imm8 and means "Multiply the contents of r/m64 by imm8 and store the result in r64". There is also another variant of this instruction, which takes a 32-bit immediate, but it is impossible to tell which variant will be actually emitted by the assembler in this case.

Now, your assembler seems to be using the AT&T syntax of x86 assembly. It is used by the GNU assembler by default. It is known for specifying the operands of the instruction in reverse order. Thus, imulq $44, (%rbx), %rax will multiply the contents of the memory at address stored in %rbx by 44, and store the result in %rax.

Upvotes: 6

Jordan Mackie
Jordan Mackie

Reputation: 2406

I was reading the order of the operands differently to how the documentation describes it.

It reads as: "Get the signed integer value stored in the memory address in register rbx, multiply it by 44, and put the result in register rax."

Or simply: imulq multiplier1, multiplier2, destination

Upvotes: 0

Related Questions