Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unfamiliar syntax for the "imul" instruction in x86 assembly

Tags:

c

x86

assembly

I've written a very basic C-function called "multby22", which does exactly what its name implies: it takes a long and returns that long multiplied by 22. (I know it's a pointless function but I wrote it to try and help me with x86 assembly.) So:

long multby22(long x) {
    return 22 * x;
}

When I complied the program and ran "objdump" on the executable, I found the disassembled code for "multby22" to be the following:

080483ff <multby22>:
   80483ff:       55                    push   %ebp              
   8048400:       89 e5                 mov    %esp,%ebp         // Create the stack frame.
   8048402:       8b 45 08              mov    0x8(%ebp),%eax    // Grab the argument and put it into the %eax register.
   8048405:       6b c0 16              imul   $0x16,%eax,%eax   // ???
   8048408:       5d                    pop    %ebp              // Pop the buffer pointer and return.
   8048409:       c3                    ret   

I understand that "imul" is for integer multiplication, but I have been unable to find anything that helps me with this syntax! The closest I have found is:

imul [reg] [reg] [const]

...where the 2nd and 3rd arguments are multiplied together and then placed into the 1st argument, which must be a register. In the assembly I generated, the 1st argument is a constant!

like image 204
Thanizer Avatar asked Feb 11 '13 16:02

Thanizer


People also ask

What does Imul mean in assembly?

Description. The single-operand form of imul executes a signed multiply of a byte, word, or long by the contents of the AL, AX, or EAX register and stores the product in the AX, DX:AX or EDX:EAX register respectively.

What is the use of Imul instruction?

The IMUL instruction allows the multiplication of two signed operands. The operands can be positive or negative. When the operand is a byte, it is multiplied with AL register and when it is a word, it is multiplied with AX register. The operation of MUL and IMUL instructions are same.

What does Movzbl do in assembly?

For MOVZBL, the low 8 bits of the destination are replaced by the source operand. the top 24 bits are set to 0. The source operand is unaffected. For MOVZBW, the low 16 bits of the destination are replaced by the source operand.

What does JG do in assembly?

The command JG simply means: Jump if Greater. The result of the preceding instructions is stored in certain processor flags (in this it would test if ZF=0 and SF=OF) and jump instruction act according to their state.


1 Answers

You found the right instruction. AT&T assembly syntax just happens to be "backwards" from the way everyone else does it. The destination register is on the right.

Your code is multiplying EAX by 22 (0x16) and putting the result into EAX.

If you are trying to compare instructions against the documentation, you should probably look into a disassembler that can output Intel syntax disassembly.

like image 165
Carl Norum Avatar answered Oct 21 '22 13:10

Carl Norum