Understanding ATT Assembly (immediate)

后端 未结 1 1882
谎友^
谎友^ 2020-12-21 23:41

lets say i have the following assembly lines

movl   $-1, %edi
movl   $1, %edx

What exactly am I storing into %edi/%edx registers.

B

相关标签:
1条回答
  • 2020-12-22 00:23

    There are four ways to load something into a register:

    1. Immediate value - in AT&T assembler, that's using a $number, and it loads that particular value (number) into the register. Note that number doesn't have to be a numeric value, it could be, for example, movl $printf, %eax - this would load the address of the function printf into register eax.

    2. From another register, movl %eax, %edx - we now have eax value copied into edx.

    3. From a fixed memory location, movl myvar, %eax - the contents of myvar is in eax.

    4. From a memory location in another register, movl (%eax), %edx - now, edx has whatever 32-bit value is at the address in eax. Of course, assuming it's actually a "good" memory location - if not, we have a segfault.

    If this was C code, the code may loook a bit like this:

    1)

    int x = 42; 
    
    int (*printfunc)(const char *fmt, ...) = printf;
    

    2)

    int x = 1;  
    int y = 2; 
    ..., 
    x = y;     // movl  %eax, %edx
    

    3)

    int x = myvar;
    

    4)

    int x = *myptr;
    

    Edit: Almost everything that is a "source" for a move instruction can also be a source for arithmetic operations, such as add $3, %eax will be the equivalent in C of x += 3;.

    0 讨论(0)
提交回复
热议问题