Search code examples
cassemblyatt

Simplifying Assembly Instruction


I'm trying to convert the following code into a single line using leal.

movl 4(%esp), %eax
sall $2, %eax
addl 8(%esp), %eax
addl $4, %eax

My question is of 3 parts:

  1. Does the '%' in front of the register simply define the following string as a register?
  2. Does the '$' in front of the integers define the following value type as int?
  3. Is leal 4(%rsi, 4, %rdi), %eax a correct conversion from the above assembly? (ignoring the change from 32-bit to 64-bit)

Edit: Another question. would

unsigned int fun3(unsigned int x, unsigned int y)
{
    unsigned int *z = &x;
    unsigned int w = 4+y;        
    return (4*(*z)+w);             
}

generate the above code? I'm unfamiliar with pointers.


Solution

  • 1: if % yes

    2: there is no int or float or bool or char or... in asm. You are dealing with the machine. It means it is a constant

    3: 1 move value in (esp - 4) to eax. esp is the stack pointer, eax is the register used by c function to return values.

    2 shift to left two times. same as multiply by 4

    3 add value in (esp - 8) to value in eax

    4 add 4 to value in eax

    x*4+y+4 = eax x is (esp -4), y is (esp-8)

    leal is the same as, 4+rsi+4*rdi =eax

    so yes it the same in a way.

    That depend on the compiler, but yes that is valid translation. 4*x+y+4