1

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?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
ShadyBears
  • 3,955
  • 13
  • 44
  • 66
  • 3
    Looks like you already understand it correctly. Immediate is a constant embedded into code. Here we have two constants, -1 and 1. – Anton Kovalenko Feb 15 '13 at 18:00
  • 6
    Note that this is not the same, necessarily, as initializing a variable in `C`. You are actually loading a value into a register. – RageD Feb 15 '13 at 18:01
  • 2
    An immediate is a constant, like you say. It's called an immediate because it's encoded into the actual instruction (rather than being from memory). – slugonamission Feb 15 '13 at 18:25
  • 3
    AT&T style syntax does everything arseways. You're better off with Intel's syntax. The fewer the number of people who understand AT&T syntax the sooner it dies. – James Feb 15 '13 at 18:43
  • Unfortunately, I have to stick with what my school is teaching me :( – ShadyBears Feb 15 '13 at 19:01

1 Answers1

4

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

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • Just that the inmediate value can also be used in other contexts, e.g., in an addition or other operation. – vonbrand Feb 15 '13 at 18:34
  • You can also use `lea` to load an immediate value or a value from another register (both need to be represeted as a memory address) into a register: 1) `lea eax,number` 2) `lea edx,[eax]` 3) & 4) can't do those with `lea`. – nrz Feb 15 '13 at 21:08