Understanding ATT Assembly (immediate)

2019-07-27 11:58发布

问题:

lets say i have the following assembly lines

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

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

Basically if I were to convert this code into a C program, would I be initalizing some variables to -1 and 1 because that's how I see it and that's where I think I'm getting confused.

I understand that immediate = "some constant" but what does that mean?

回答1:

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;.



标签: c assembly att