Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
601 views
in Technique[技术] by (71.8m points)

x86 - Understanding ATT Assembly (immediate)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...