Hi, I'm new here :)

I just started today with ASM, and I have some confusions.

1. The leave command:
    What does it actually? It seems it's multiple functions in one (like movl and popl), but how does it know what to mov and what to   
    pop?

//main.s
_function:
pushl %ebp
movl %esp, %ebp
subl $4, %esp
leave ; what does it do?
ret

//main.c
int function()
{
    int a;
}


2. Return value:
    Yes, I learned C before ASM ^^. Consider the following code:

.globl _main
_main:
movl $12, %ebx
pushl %ebx
call sqr
addl $4, %esp
ret
sqr:
movl 4(%esp), %eax
imull %eax, %eax
ret


  It calculates 12 * 12 using a function. It outputs 144, correct. But how does it know what to output? Where is the return value
  144 stored? In %eax right? And why is %eax filled with %esp? Or is it because %esp is a pointer to the stack, and %ebx (12) is
  pushed on? Why was %ebx used instead of %eax in the first place?

3. Information:
    Why does my code contain this information, and is it used? How?

	.file	"main.c"
.text
.globl _function ; I get this, but not the other 3...
.def _function; .scl 2; .type 32; .endef


4. Strings:
    What is LCO? What is alignment? Any tutorials on this?

LC0:
.ascii "Hello World\0"
.text
.p2align 4,,15


5. What is basically the difference between different optimization options?

Thanks for your answers, point me to some ASM tutorials if you find that neccessary. I couldn't find that many.

I use MingW on Win32 XP.
Posted on 2008-06-05 07:43:03 by Daevius
1. The attachment shows the leave instruction's algorithm written in pseudocode (this 'algorithm' actually consists of 2 simple 'IF-ELSE' statements). It is from "Intel® 64 and IA-32 Architectures Software Developer’s Manual Volume 2A: Instruction Set Reference, A-M". Please read the manual's volume 1 for more detailed explaination.

2. It works as follows:
- stores "12" in ebx
- pushes ebx on the stack. (You can directly push 12 and skip the first step if you want to, but compilers are usually worse than humans in writing good assembly code.).
- calls the procedure. In other words, it pushes the current EIP (current "position") in the stack and jumps to new address labeled "sqr".
- the subprocedure loads eax with a value stored on the stack, offset by 4 (so if the stack pointer now is "32" then it loads eax with a value from address 36). that's because the stack's top holds the return address (pushed by 'call'). so the NEXT 4 bytes are our '12' stored in the beginning.
- imull eax, eax is self-explainatory, i believe. it stores the result in eax, as well.
- return goes back where the stack's top points to and pop the top from the stack. ; pop(); ]
- it adds 4 to esp. this is because of C-calling convention. "Calling conventions" is a quite complicated topic if you're new to asm.
- the last one is ret -- explained already.

4. alignment: if something starts on a memory address divisible by N, then we say that this something is "N-bytes aligned". Proper alignment speeds-up execution mainly because of how the CPUs load their caches.

5. 2 most important groups of optimizations are: optimization for size and optimization for speed. Almost always, when you code something, you have to decide whether you want it small or fast, because these two qualities usually oppose one another.
Attachments:
Posted on 2008-06-05 11:04:53 by ti_mo_n
Thanks alot :)
Posted on 2008-06-08 16:02:43 by Daevius