I posted x86 Assembly for C Programmers 1 to reddit and got some great feedback. There were a few things that were brought up, and I’m taking a minute to address some of them (I’m not addressing everything). Thanks to everyone out there who took the time to point out mistakes and make suggestions.
Intel Assembly Comprehensive Cheat Sheet
danukeru from reddit posted a link to this cheat sheet, which lists x86 opcodes, what their abbreviations mean, and their Intel syntax. It is awesome, and I wish I had it earlier.
A little more into lea
This comes from jputnam at reddit as a suggestion.
A quick google search turns up a few articles which are helpful. I especially like this one from the University of Illinois at Urbana-Champaign.
Basically, lea sets a register equal to a value, after computations on that value are completed. Usually, this is an offset from another register. This is different from mov, which is used to set a memory location pointed to by a register equal to a value.
Let’s say we want to set our register ecx equal to ebp+0×12, because that’s where one of our stack variables are, and instead of using [ebp+0x12] everytime we want to refer to that stack variable, we’re going to use ecx (I have no idea why we would ever want to do this, but we’ll pretend). We would use the lea instruction to do this all very neatly in one line.
lea ecx, [ebp+0x12]
If you’re still not entirely cleared up on lea, visit some of the links above. They do an excellent job explaining this instruction.
Explain why eax is set to 0 at the end of main
Another one from jputnam.
It is a common convention to set eax to the return value. Main returns 0. Therefor, we set eax to 0.
If you call a function and want to check its returned value, check eax.
Spend a little more time explaing ebp
This one comes from stevep98 at reddit.
Given this C function:
void function ebp_explanation (int argument)
{
int i;
}
This is a simplistic example of how this function may look on the stack (ditaa is cool):
Image has been accidentally deleted
0×74 is memory we reserve for int i. Normally, we reserve this space by setting esp somewhere beneath 0×74, so when we push things on to the stack they do not overwrite the memory at 0×74.
We then set ebp somewhere consistent on our stack, and we leave it there. Convention is to set ebp to where it is in the above illustration. From there, when we want to refer to certain pieces of memory (IE stack variables), we refer to them by an offset from ebp. If we want to add 1 to int i, it will look like this:
add DWORD PTR [ebp-0x4], 0x1
Like storing the return value in eax, this is a convention, but not a rule. In fact, when we take our same program from the first tutorial, give gcc the -O3 flag, and look at the disassembled code, we will see gcc decides to refer to int i in regards to esp instead of in regards to ebp.
Stack Alignment
jldugger commented on stack alignment.
If you want some more information stack alignment, which I admittedly didn’t do a terrific job of explaining, a simple google search returns these two pages: ( one | two ). They should help clear things up. I’m going to leave it at that for stack alignment.
What’s to come
I won’t know 100% what will come next until it’s written, but these are the next three topics I would like to hit (remember, I’m learning as I’m writing):
- Take one.c (the example program from x86 Assembly for C Programmers 1) compiled with -O2 and -O3, and analyze why the compiler does what it does with different levels/kinds of optimization.
- Begin looking at some more complicated examples, more of the instruction set, and expand our knowledge of the x86 instruction set.
- We will take a C program, break it down in to assembly, and begin optimizing it in assembly. This will be the first installment of writing/modifying the assembly code.
Beyond that, we’ll find out when we get there.
Just wanted to thank you for your tutorial. I found myself staring at assembly and forgetting all I had ever learned about it, and you helped me immensely. I was even able to make a blog post about it http://ritter.vg/n.php?page=code_adventures_clr2
I’m glad you found it useful. I’ve started on part 2, but other things have gotten into the way of me finishing it.
Here’s a very similar to yours but much more complete tutorial in russian. Don’t you know russian by chance? It would be great if it would be traslated to english!
link