You are on page 1of 32

Activation block

Usage example
Mariano Rico, UC3M 2010
Statement
• Write a program that uses the function
counter. This function has two parameters:
(1)the address of a zero-terminated string
and (2) a character.
This function returns the number of times (an
integer) that the given character appears in
the string
• In C terminology the funtion signature would
be int counter (char* s, char c)
Calling the function (big picture)
• The main program must
1) Reserve space in the stack to store the result(s)
2) Load the function parameters in the stack (in the
right order)
3) Call the function
4) Download the function parameters from the
stack
5) Get the result from the stack
Calling the function (step by step)
• Initial state
– The stack pointer (%esp) points to the top of the
stack

%esp Initial position


Calling the function (step by step)
1) Reserve space in the stack to store the result(s)
2) Load the function’s parameters in the stack (in the right
order)
3) Call the function
- Execute the funcion
4) Download the function’s parameters from the stack
5) Get the result(s) from the stack

sub $4, %esp Params’


push theletter space
push thestring
call counter %esp Result’s space
add $8, %esp
Initial position
pop %eax
Calling the function (step by step)
1) Reserve space in the stack to store the result(s)
2) Load the function’s parameters in the stack (in the right
order)
3) Call the function
- Execute the funcion
4) Download the function’s parameters from the stack
5) Get the result(s) from the stack

sub $4, %esp %esp thestring Params’


push theletter theletter
space
push thestring
call counter Result’s space
add $8, %esp
Initial position
pop %eax
Calling the function (step by step)
1) Reserve space in the stack to store the result(s)
2) Load the function’s parameters in the stack (in the right
order)
3) Call the function
- Execute the funcion
4) Download the function’s parameters from the stack
5) Get the result(s) from the stack
%esp @RET

sub $4, %esp thestring Params’


push theletter theletter
space
push thestring
call counter Result’s space
add $8, %esp
Initial position
pop %eax
Calling the function (step by step)
1) Reserve space in the stack to store the result(s)
2) Load the function’s parameters in the stack (in the right
order)
3) Call the function
- Execute the funcion
4) Download the function’s parameters from the stack
5) Get the result(s) from the stack
@RET

sub $4, %esp %esp thestring Params’


push theletter theletter
space
push thestring
call counter result Result’s space
add $8, %esp
Initial position
pop %eax
Calling the function (step by step)
1) Reserve space in the stack to store the result(s)
2) Load the function’s parameters in the stack (in the right
order)
3) Call the function
- Execute the funcion
4) Download the function’s parameters from the stack
5) Get the result(s) from the stack
@RET

sub $4, %esp thestring Params’


push theletter theletter
space
push thestring
call counter %esp result Result’s space
add $8, %esp
Initial position
pop %eax
Calling the function (step by step)
1) Reserve space in the stack to store the result(s)
2) Load the function’s parameters in the stack (in the right
order)
3) Call the function
- Execute the funcion
4) Download the function’s parameters from the stack
5) Get the result(s) from the stack
@RET

sub $4, %esp thestring Params’


push theletter theletter
space
push thestring
call counter result Result’s space
add $8, %esp %esp Initial position
pop %eax
1)
The function (big picture)
Save the value of %ebp (as a pointer to the
activation block)
2) Copy the value of %esp to %ebp
3) Allocate space in the stack for local variables
• Initial state
4) Save registries that will be used by the
subroutine
5) Do the subroutine’s job
– The stack is like
6) Store results in the space allocated
7) Restore the registries used by the subroutine this:
8) Move the top of the stack to the activation
block pointer
9) Restore the value of %ebp
(step 3 in the
10) Execute RET
calling program)

%esp @RET
thestring Params’
space
theletter
Result’s
space
Initial
position
1)
The function (step by step)
Save the value of %ebp (as a pointer to the
activation block) counter:
2) Copy the value of %esp to %ebp push %ebp
3) Allocate space in the stack for local variables mov %esp, %ebp
4) Save registries that will be used by the sub $4, %esp
subroutine
push %eax
5) Do the subroutine’s job
push %ebx
6) Store results in the space allocated
7) Restore the registries used by the subroutine
push %ecx
8) Move the top of the stack to the activation movl $0, -4(%ebp)
block pointer mov 8(%ebp), %eax
9) Restore the value of %ebp mov 12(%ebp), %cl
10) Execute RET mov $0, %ebx
loop:
cmpb $0, (%eax, %ebx)
je res
cmpb %cl, (%eax, %ebx)
jne incr %esp %ebp
incl -4(%ebp)
incr: @RET
inc %ebx
jmp loop thestring Params’
res: space
mov -4(%ebp), %eax theletter
mov %eax, 16(%ebp) Result’s
pop %ecx space
pop %ebx Initial
pop %eax position
mov %ebp, %esp
pop %ebp
ret
1)
The function (step by step)
Save the value of %ebp (as a pointer to the
activation block) counter:
2) Copy the value of %esp to %ebp push %ebp
3) Allocate space in the stack for local variables mov %esp, %ebp
4) Save registries that will be used by the sub $4, %esp
subroutine
push %eax
5) Do the subroutine’s job
push %ebx
6) Store results in the space allocated
7) Restore the registries used by the subroutine
push %ecx
8) Move the top of the stack to the activation movl $0, -4(%ebp)
block pointer mov 8(%ebp), %eax
9) Restore the value of %ebp mov 12(%ebp), %cl
10) Execute RET mov $0, %ebx
loop:
cmpb $0, (%eax, %ebx)
je res
cmpb %cl, (%eax, %ebx)
jne incr
%esp %ebp
incl -4(%ebp)
%ebp
incr: @RET
inc %ebx
jmp loop thestring Params’
res: space
mov -4(%ebp), %eax theletter
mov %eax, 16(%ebp) Result’s
pop %ecx space
pop %ebx Initial
pop %eax position
mov %ebp, %esp
pop %ebp
ret
1)
The function (step by step)
Save the value of %ebp (as a pointer to the
activation block) counter:
2) Copy the value of %esp to %ebp push %ebp
3) Allocate space in the stack for local variables mov %esp, %ebp
4) Save registries that will be used by the sub $4, %esp
subroutine
push %eax
5) Do the subroutine’s job
push %ebx
6) Store results in the space allocated
7) Restore the registries used by the subroutine
push %ecx
8) Move the top of the stack to the activation movl $0, -4(%ebp)
block pointer mov 8(%ebp), %eax
9) Restore the value of %ebp mov 12(%ebp), %cl
10) Execute RET mov $0, %ebx
loop:
cmpb $0, (%eax, %ebx) Local var
je res %esp space
cmpb %cl, (%eax, %ebx)
jne incr %ebp %ebp
incl -4(%ebp)
incr: @RET
inc %ebx
jmp loop thestring Params’
res: space
mov -4(%ebp), %eax theletter
mov %eax, 16(%ebp) Result’s
pop %ecx space
pop %ebx Initial
pop %eax position
mov %ebp, %esp
pop %ebp
ret
1)
The function (step by step)
Save the value of %ebp (as a pointer to the
activation block) counter:
2) Copy the value of %esp to %ebp push %ebp
3) Allocate space in the stack for local variables mov %esp, %ebp
4) Save registries that will be used by the sub $4, %esp
subroutine
push %eax
5) Do the subroutine’s job
push %ebx
6) Store results in the space allocated
7) Restore the registries used by the subroutine
push %ecx
8) Move the top of the stack to the activation movl $0, -4(%ebp) %esp %ecx
block pointer mov 8(%ebp), %eax
9) Restore the value of %ebp mov 12(%ebp), %cl %ebx
10) Execute RET mov $0, %ebx
loop: %eax
cmpb $0, (%eax, %ebx) Local var
je res space
cmpb %cl, (%eax, %ebx)
jne incr %ebp %ebp
incl -4(%ebp)
incr: @RET
inc %ebx
jmp loop thestring Params’
res: space
mov -4(%ebp), %eax theletter
mov %eax, 16(%ebp) Result’s
pop %ecx space
pop %ebx Initial
pop %eax position
mov %ebp, %esp
pop %ebp
ret
1)
The function (step by step)
Save the value of %ebp (as a pointer to the
activation block) counter:
2) Copy the value of %esp to %ebp push %ebp
3) Allocate space in the stack for local variables mov %esp, %ebp
4) Save registries that will be used by the sub $4, %esp
subroutine
push %eax
5) Do the subroutine’s job
push %ebx
6) Store results in the space allocated
7) Restore the registries used by the subroutine
push %ecx
8) Move the top of the stack to the activation movl $0, -4(%ebp) %esp %ecx
block pointer mov 8(%ebp), %eax
9) Restore the value of %ebp mov 12(%ebp), %cl %ebx
10) Execute RET mov $0, %ebx
loop: %eax
cmpb $0, (%eax, %ebx) Local var
jelocal
res var = 0
local var = 0 0x00000000 space
cmpb %cl, (%eax, %ebx)
jne incr %ebp %ebp
incl -4(%ebp)
incr: @RET
inc %ebx
jmp loop thestring Params’
res: space
mov -4(%ebp), %eax theletter
mov %eax, 16(%ebp) Result’s
pop %ecx space
pop %ebx Initial
pop %eax position
mov %ebp, %esp
pop %ebp
ret
1)
The function (step by step)
Save the value of %ebp (as a pointer to the
activation block) counter:
2) Copy the value of %esp to %ebp push %ebp
3) Allocate space in the stack for local variables mov %esp, %ebp
4) Save registries that will be used by the sub $4, %esp
subroutine
push %eax
5) Do the subroutine’s job
push %ebx
6) Store results in the space allocated
7) Restore the registries used by the subroutine
push %ecx
8) Move the top of the stack to the activation movl $0, -4(%ebp) %esp %ecx
block pointer mov 8(%ebp), %eax
9) Restore the value of %ebp mov 12(%ebp), %cl %ebx
10) Execute RET mov $0, %ebx
loop: %eax
cmpb $0, (%eax, %ebx) Local var
je res 0x00000000 space
cmpb %cl, (%eax, %ebx)
jne incr %ebp %ebp
incl -4(%ebp)
incr: @RET
inc %ebx
%eax
jmp points
loop
%eax pointsto
tothestring
thestring thestring Params’
res: space
mov -4(%ebp), %eax theletter
mov %eax, 16(%ebp) Result’s
pop %ecx space
pop %ebx Initial
pop %eax position
mov %ebp, %esp
pop %ebp
ret
1)
The function (step by step)
Save the value of %ebp (as a pointer to the
activation block) counter:
2) Copy the value of %esp to %ebp push %ebp
3) Allocate space in the stack for local variables mov %esp, %ebp
4) Save registries that will be used by the sub $4, %esp
subroutine
push %eax
5) Do the subroutine’s job
push %ebx
6) Store results in the space allocated
7) Restore the registries used by the subroutine
push %ecx
8) Move the top of the stack to the activation movl $0, -4(%ebp) %esp %ecx
block pointer mov 8(%ebp), %eax
9) Restore the value of %ebp mov 12(%ebp), %cl %ebx
10) Execute RET mov $0, %ebx
loop: %eax
cmpb $0, (%eax, %ebx) Local var
je res 0x00000000 space
cmpb %cl, (%eax, %ebx)
jne incr %ebp %ebp
incl -4(%ebp)
incr: @RET
inc %ebx
%eax
jmp points
loop
%eax points to
to thestring
thestring thestring Params’
res:
%cl
mov%cl
points
pointsto
to theletter
theletter space
-4(%ebp), %eax theletter
mov %eax, 16(%ebp) Result’s
pop %ecx space
pop %ebx Initial
pop %eax position
mov %ebp, %esp
pop %ebp
ret
1)
The function (step by step)
Save the value of %ebp (as a pointer to the
activation block) counter:
2) Copy the value of %esp to %ebp push %ebp
3) Allocate space in the stack for local variables mov %esp, %ebp
4) Save registries that will be used by the sub $4, %esp
subroutine
push %eax
5) Do the subroutine’s job
push %ebx
6) Store results in the space allocated
7) Restore the registries used by the subroutine
push %ecx
8) Move the top of the stack to the activation movl $0, -4(%ebp) %esp %ecx
block pointer mov 8(%ebp), %eax
9) Restore the value of %ebp mov 12(%ebp), %cl %ebx
10) Execute RET mov $0, %ebx
loop: %eax
cmpb $0, (%eax, %ebx) Local var
je res 0x00000000 space
cmpb %cl, (%eax, %ebx)
jne incr %ebp %ebp
incl -4(%ebp)
incr: @RET
inc %ebx
%eax
jmp points
loop
%eax points to
to thestring
thestring thestring Params’
res:
%cl
mov%cl
points
pointsto to theletter
theletter space
-4(%ebp), %eax theletter
%ebx
mov isisan
%eax,
%ebx index
index(ini
16(%ebp)
an (inival=0)
val=0) Result’s
pop %ecx space
pop %ebx Initial
pop %eax position
mov %ebp, %esp
pop %ebp
ret
1)
The function (step by step)
Save the value of %ebp (as a pointer to the
activation block) counter:
2) Copy the value of %esp to %ebp push %ebp
3) Allocate space in the stack for local variables mov %esp, %ebp
4) Save registries that will be used by the sub $4, %esp
subroutine
push %eax
5) Do the subroutine’s job
push %ebx
6) Store results in the space allocated
7) Restore the registries used by the subroutine
push %ecx
8) Move the top of the stack to the activation movl $0, -4(%ebp) %esp %ecx
block pointer mov 8(%ebp), %eax
9) Restore the value of %ebp mov 12(%ebp), %cl %ebx
10) Execute RET mov $0, %ebx
loop: %eax
cmpb $0, (%eax, %ebx) Local var
je res 0x00000000 space
cmpb %cl, (%eax, %ebx)
jne incr %ebp %ebp
incl -4(%ebp)
incr: @RET
inc %ebx
%eax
jmp points
loop
%eax points to
to thestring
thestring thestring Params’
res:
%cl
mov%cl
points
pointsto to theletter
theletter space
-4(%ebp), %eax theletter
%ebx
mov isisan
%eax,
%ebx index
index(ini
16(%ebp)
an (inival=0)
val=0) Result’s
pop %ecx
Comparison ofofbyte
popComparison
%ebx byte(to(to space
detect Initial
pop %eax the
detect theend
endofofthe the position
string)
mov %ebp, %esp
popstring)
%ebp
ret
1)
The function (step by step)
Save the value of %ebp (as a pointer to the
activation block) counter:
2) Copy the value of %esp to %ebp push %ebp
3) Allocate space in the stack for local variables mov %esp, %ebp
4) Save registries that will be used by the sub $4, %esp
subroutine
push %eax
5) Do the subroutine’s job
push %ebx
6) Store results in the space allocated
7) Restore the registries used by the subroutine
push %ecx
8) Move the top of the stack to the activation movl $0, -4(%ebp) %esp %ecx
block pointer mov 8(%ebp), %eax
9) Restore the value of %ebp mov 12(%ebp), %cl %ebx
10) Execute RET mov $0, %ebx
loop: %eax
cmpb $0, (%eax, %ebx) Local var
je res 0x00000000 space
cmpb %cl, (%eax, %ebx)
jne incr %ebp %ebp
incl -4(%ebp)
incr: @RET
inc %ebx
jmp loop If it reaches the end of thestring
res:
If it reaches the end of Params’
the
thestring
stringthen
thenjump
jumpto
to space
mov -4(%ebp), %eax theletter
mov %eax, res,
res,otherwise…
16(%ebp)
otherwise… Result’s
pop %ecx space
pop %ebx Initial
pop %eax position
mov %ebp, %esp
pop %ebp
ret
1)
The function (step by step)
Save the value of %ebp (as a pointer to the
activation block) counter:
2) Copy the value of %esp to %ebp push %ebp
3) Allocate space in the stack for local variables mov %esp, %ebp
4) Save registries that will be used by the sub $4, %esp
subroutine
push %eax
5) Do the subroutine’s job
push %ebx
6) Store results in the space allocated
7) Restore the registries used by the subroutine
push %ecx
8) Move the top of the stack to the activation movl $0, -4(%ebp) %esp %ecx
block pointer mov 8(%ebp), %eax
9) Restore the value of %ebp mov 12(%ebp), %cl %ebx
10) Execute RET mov $0, %ebx
loop: %eax
cmpb $0, (%eax, %ebx) Local var
je res 0x00000000 space
cmpb %cl, (%eax, %ebx)
jne incr %ebp %ebp
incl -4(%ebp)
incr: @RET
inc %ebx
jmp loop … compares the byte in thestring
res:
… compares the byte in Params’
%clto
%cl tothe
thebyte
bytepointed space
mov -4(%ebp), %eax pointed theletter
mov %eax, by
bythe
theindex
16(%ebp) index Result’s
pop %ecx space
pop %ebx Initial
pop %eax position
mov %ebp, %esp
pop %ebp
ret
1)
The function (step by step)
Save the value of %ebp (as a pointer to the
activation block) counter:
2) Copy the value of %esp to %ebp push %ebp
3) Allocate space in the stack for local variables mov %esp, %ebp
4) Save registries that will be used by the sub $4, %esp
subroutine
push %eax
5) Do the subroutine’s job
push %ebx
6) Store results in the space allocated
7) Restore the registries used by the subroutine
push %ecx
8) Move the top of the stack to the activation movl $0, -4(%ebp) %esp %ecx
block pointer mov 8(%ebp), %eax
9) Restore the value of %ebp mov 12(%ebp), %cl %ebx
10) Execute RET mov $0, %ebx
loop: %eax
cmpb $0, (%eax, %ebx) Local var
je res 0x00000000 space
cmpb %cl, (%eax, %ebx)
jne incr %ebp %ebp
incl -4(%ebp)
incr: @RET
inc %ebx
jmp loop If equal (letter detected) thestring
res:
If equal (letter detected) Params’
go
gototoincr…
incr… space
mov -4(%ebp), %eax theletter
mov %eax, 16(%ebp) Result’s
pop %ecx space
pop %ebx Initial
pop %eax position
mov %ebp, %esp
pop %ebp
ret
1)
The function (step by step)
Save the value of %ebp (as a pointer to the
activation block) counter:
2) Copy the value of %esp to %ebp push %ebp
3) Allocate space in the stack for local variables mov %esp, %ebp
4) Save registries that will be used by the sub $4, %esp
subroutine
push %eax
5) Do the subroutine’s job
push %ebx
6) Store results in the space allocated
7) Restore the registries used by the subroutine
push %ecx
8) Move the top of the stack to the activation movl $0, -4(%ebp) %esp %ecx
block pointer mov 8(%ebp), %eax
9) Restore the value of %ebp mov 12(%ebp), %cl %ebx
10) Execute RET mov $0, %ebx
loop: %eax
cmpb $0, (%eax, %ebx) Local var
je res 0x00000000 space
cmpb %cl, (%eax, %ebx)
jne incr %ebp %ebp
incl -4(%ebp)
incr: @RET
inc %ebx
jmp loop If equal (letter detected) thestring
res:
If equal (letter detected) Params’
go
goto
mov -4(%ebp), toincr…
incr…increase
%eax increasethe
the theletter space
mov %eax, index…
16(%ebp)
index… Result’s
pop %ecx space
pop %ebx Initial
pop %eax position
mov %ebp, %esp
pop %ebp
ret
1)
The function (step by step)
Save the value of %ebp (as a pointer to the
activation block) counter:
2) Copy the value of %esp to %ebp push %ebp
3) Allocate space in the stack for local variables mov %esp, %ebp
4) Save registries that will be used by the sub $4, %esp
subroutine
push %eax
5) Do the subroutine’s job
push %ebx
6) Store results in the space allocated
7) Restore the registries used by the subroutine
push %ecx
8) Move the top of the stack to the activation movl $0, -4(%ebp) %esp %ecx
block pointer mov 8(%ebp), %eax
9) Restore the value of %ebp mov 12(%ebp), %cl %ebx
10) Execute RET mov $0, %ebx
loop: %eax
cmpb $0, (%eax, %ebx) Local var
je res 0x00000000 space
cmpb %cl, (%eax, %ebx)
jne incr %ebp %ebp
incl -4(%ebp)
incr: @RET
inc %ebx
jmp loop If equal (letter detected) thestring
res:
If equal (letter detected) Params’
go
goto
mov -4(%ebp), toincr…
incr…increase
%eax increasethe
the theletter space
mov %eax, index…
index…and
16(%ebp) anddodothe
theloop
loop Result’s
pop %ecx again space
pop %ebx again Initial
pop %eax position
mov %ebp, %esp
pop %ebp
ret
1)
The function (step by step)
Save the value of %ebp (as a pointer to the
activation block) counter:
2) Copy the value of %esp to %ebp push %ebp
3) Allocate space in the stack for local variables mov %esp, %ebp
4) Save registries that will be used by the sub $4, %esp
subroutine
push %eax
5) Do the subroutine’s job
push %ebx
6) Store results in the space allocated
7) Restore the registries used by the subroutine
push %ecx
8) Move the top of the stack to the activation movl $0, -4(%ebp) %esp %ecx
block pointer mov 8(%ebp), %eax
9) Restore the value of %ebp mov 12(%ebp), %cl %ebx
10) Execute RET mov $0, %ebx
loop: %eax
cmpb $0, (%eax, %ebx) Local var
je res 0x00000001 space
cmpb %cl, (%eax, %ebx)
jne incr %ebp %ebp
incl -4(%ebp)
incr: @RET
inc %ebx
jmp loop …otherwise increase the thestring
res:
…otherwise increase the Params’
local var
local%eax
var space
mov -4(%ebp), theletter
mov %eax, 16(%ebp) Result’s
pop %ecx space
pop %ebx Initial
pop %eax position
mov %ebp, %esp
pop %ebp
ret
1)
The function (step by step)
Save the value of %ebp (as a pointer to the
activation block) counter:
2) Copy the value of %esp to %ebp push %ebp
3) Allocate space in the stack for local variables mov %esp, %ebp
4) Save registries that will be used by the sub $4, %esp
subroutine
push %eax
5) Do the subroutine’s job
push %ebx
6) Store results in the space allocated
7) Restore the registries used by the subroutine
push %ecx
8) Move the top of the stack to the activation movl $0, -4(%ebp) %esp %ecx
block pointer mov 8(%ebp), %eax
9) Restore the value of %ebp mov 12(%ebp), %cl %ebx
10) Execute RET mov $0, %ebx
loop: %eax
cmpb $0, (%eax, %ebx) Local var
je res loc var value space
cmpb %cl, (%eax, %ebx)
Copies the
jne incr
Copies the value
valueofofthe
the %ebp %ebp
incl -4(%ebp)
local
localvar
varto
to%eax
%eax
incr: @RET
inc %ebx
jmp loop thestring Params’
res: space
mov -4(%ebp), %eax theletter
mov %eax, 16(%ebp) Result’s
pop %ecx space
pop %ebx Initial
pop %eax position
mov %ebp, %esp
pop %ebp
ret
1)
The function (step by step)
Save the value of %ebp (as a pointer to the
activation block) counter:
2) Copy the value of %esp to %ebp push %ebp
3) Allocate space in the stack for local variables mov %esp, %ebp
4) Save registries that will be used by the sub $4, %esp
subroutine
push %eax
5) Do the subroutine’s job
push %ebx
6) Store results in the space allocated
7) Restore the registries used by the subroutine
push %ecx
8) Move the top of the stack to the activation movl $0, -4(%ebp) %esp %ecx
block pointer mov 8(%ebp), %eax
9) Restore the value of %ebp mov 12(%ebp), %cl %ebx
10) Execute RET mov $0, %ebx
loop: %eax
cmpb $0, (%eax, %ebx) Local var
je res loc var value space
cmpb %cl, (%eax, %ebx)
Copies the
jne incr value stored %ebp %ebp
Copies the value
incl -4(%ebp)
stored
inin%eax
%eax totothe
theresult’s
result’s
incr: @RET
space
inc %ebx
space
jmp loop thestring Params’
res: space
mov -4(%ebp), %eax theletter
mov %eax, 16(%ebp) Result’s
pop %ecx result space
pop %ebx Initial
pop %eax position
mov %ebp, %esp
pop %ebp
ret
1)
The function (step by step)
Save the value of %ebp (as a pointer to the
activation block) counter:
2) Copy the value of %esp to %ebp push %ebp
3) Allocate space in the stack for local variables mov %esp, %ebp
4) Save registries that will be used by the sub $4, %esp
subroutine
push %eax
5) Do the subroutine’s job
push %ebx
6) Store results in the space allocated
7) Restore the registries used by the subroutine
push %ecx
8) Move the top of the stack to the activation movl $0, -4(%ebp) %ecx
block pointer mov 8(%ebp), %eax
9) Restore the value of %ebp mov 12(%ebp), %cl %ebx
10) Execute RET mov $0, %ebx
loop: %eax
cmpb $0, (%eax, %ebx) Local var
je res %esp loc var value space
cmpb %cl, (%eax, %ebx)
jne incr %ebp %ebp
incl -4(%ebp)
incr: @RET
inc %ebx
jmp loop thestring Params’
res: space
mov -4(%ebp), %eax theletter
mov %eax, 16(%ebp) Result’s
pop %ecx result space
pop %ebx Initial
pop %eax position
mov %ebp, %esp
pop %ebp
ret
1)
The function (step by step)
Save the value of %ebp (as a pointer to the
activation block) counter:
2) Copy the value of %esp to %ebp push %ebp
3) Allocate space in the stack for local variables mov %esp, %ebp
4) Save registries that will be used by the sub $4, %esp
subroutine
push %eax
5) Do the subroutine’s job
push %ebx
6) Store results in the space allocated
7) Restore the registries used by the subroutine
push %ecx
8) Move the top of the stack to the activation movl $0, -4(%ebp) %ecx
block pointer mov 8(%ebp), %eax
9) Restore the value of %ebp mov 12(%ebp), %cl %ebx
10) Execute RET mov $0, %ebx
loop: %eax
cmpb $0, (%eax, %ebx) Local var
je res loc var value space
cmpb %cl, (%eax, %ebx)
%esp
jne incr
%ebp %ebp
incl -4(%ebp)
incr: @RET
inc %ebx
jmp loop thestring Params’
res: space
mov -4(%ebp), %eax theletter
mov %eax, 16(%ebp) Result’s
pop %ecx result space
pop Deletes
%ebx local variables
Deletes
pop %eax
local variables Initial
position
mov %ebp, %esp
pop %ebp
ret
1)
The function (step by step)
Save the value of %ebp (as a pointer to the
activation block) counter:
2) Copy the value of %esp to %ebp push %ebp
3) Allocate space in the stack for local variables mov %esp, %ebp
4) Save registries that will be used by the sub $4, %esp
subroutine
push %eax
5) Do the subroutine’s job
push %ebx
6) Store results in the space allocated
7) Restore the registries used by the subroutine
push %ecx
8) Move the top of the stack to the activation movl $0, -4(%ebp) %ecx
block pointer mov 8(%ebp), %eax
9) Restore the value of %ebp mov 12(%ebp), %cl %ebx
10) Execute RET mov $0, %ebx
loop: %eax
cmpb $0, (%eax, %ebx) Local var
je res loc var value space
cmpb %cl, (%eax, %ebx)
jne incr %ebp
incl -4(%ebp)
incr: %esp @RET
inc %ebx
jmp loop thestring Params’
res: space
mov -4(%ebp), %eax theletter
mov %eax, 16(%ebp) Result’s
pop %ecx result space
pop %ebx Initial
pop %eax position
mov %ebp, %esp
pop %ebp
ret
1)
The function (step by step)
Save the value of %ebp (as a pointer to the
activation block) counter:
2) Copy the value of %esp to %ebp push %ebp
3) Allocate space in the stack for local variables mov %esp, %ebp
4) Save registries that will be used by the sub $4, %esp
subroutine
push %eax
5) Do the subroutine’s job
push %ebx
6) Store results in the space allocated
7) Restore the registries used by the subroutine
push %ecx
8) Move the top of the stack to the activation movl $0, -4(%ebp) %ecx
block pointer mov 8(%ebp), %eax
9) Restore the value of %ebp mov 12(%ebp), %cl %ebx
10) Execute RET mov $0, %ebx
loop: %eax
cmpb $0, (%eax, %ebx) Local var
je res loc var value space
cmpb %cl, (%eax, %ebx)
jne incr %ebp
incl -4(%ebp)
incr: @RET
inc %ebx
jmp loop %esp thestring Params’
res: space
mov -4(%ebp), %eax theletter
mov %eax, 16(%ebp) Result’s
pop %ecx result space
pop %ebx Initial
pop %eax position
mov %ebp, %esp
pop %ebp
ret

You might also like