Reputation: 5691
I have just started to learn assembly. This is the dump from gdb for a simple program which prints hello ranjit.
Dump of assembler code for function main:
0x080483b4 <+0>: push %ebp
0x080483b5 <+1>: mov %esp,%ebp
0x080483b7 <+3>: sub $0x4,%esp
=> 0x080483ba <+6>: movl $0x8048490,(%esp)
0x080483c1 <+13>: call 0x80482f0 <puts@plt>
0x080483c6 <+18>: leave
0x080483c7 <+19>: ret
My questions are :
operating system : ubuntu 10, compiler : gcc
Upvotes: 1
Views: 1436
Reputation: 271
In my career I learned several assembly languages, you didn't mention which but it appears Intel x86 (segmented memory model as PaxDiablo pointed out). However, I have not used assembly since last century (lucky me!). Here are some of your answers:
Upvotes: 0
Reputation: 882566
ebp
is used as a frame pointer in Intel processors (assuming you're using a calling convention that uses frames).
It provides a known point of reference for locating passed-in parameters (on one side) and local variables (on the other) no matter what you do with the stack pointer while your function is active.
The sequence:
push %ebp ; save callers frame pointer
mov %esp,%ebp ; create a new frame pointer
sub $N,%esp ; make space for locals
saves the frame pointer for the previous stack frame (the caller), loads up a new frame pointer, then adjusts the stack to hold things for the current "stack level".
Since parameters would have been pushed before setting up the frame, they can be accessed with [bp+N]
where N
is a suitable offset.
Similarly, because locals are created "under" the frame pointer, they can be accessed with [bp-N]
.
The leave
instruction is a single one which undoes that stack frame. You used to have to do it manually but Intel introduced a faster way of getting it done. It's functionally equivalent to:
mov %ebp, %esp ; restore the old stack pointer
pop %ebp ; and frame pointer
(the old, manual way).
Answering the questions one by one in case I've missed something:
To start a new frame. See above.
It isn't. esp
is copied to ebp
. This is AT&T notation (the %reg
is a dead giveaway) where (among other thing) source and destination operands are swapped relative to Intel notation.
See answer to (2) above. You're subtracting 4 from esp
, not the other way around.
It's a parameter being passed to the function at 0x80482f0
. It's not being loaded into esp
but into the memory pointed at by esp
. In other words, it's being pushed on the stack. Since the function being called is puts
(see (5) below), it will be the address of the string you want puts
ed.
The function name in the <>
after the address. It's calling the puts
function (probably the one in the standard library though that's not guaranteed). For a description of what the PLT is, see here.
I've already explained leave
above as unwinding the current stack frame before exiting. The ret
simply returns from the current function. If the current functtion is main
, it's going back to the C startup code.
Upvotes: 5