







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
How function calls are implemented, focusing on the use of stacks, program memory organization, and the intel method for pushing and popping. It also discusses stack protection and local variables in the context of computer architecture.
Typology: Slides
1 / 13
This page cannot be seen from the preview
Don't miss anything!








Computer Architecture
d O^
i^ ti and Organization
l^
t d implemented.
Program instructionsGlobal data
Stack Heap
Program instructionsGlobal data
StackStackHeap
Intel method
copies the value of a register t^ th^
t^ f th
t^
k
onto the top of the stack.– Decrement the stack pointer– Store the register at the address pointed to bythe stack pointer.• A^ POP
remove the value on the top of O e^ o e
e^
a ue o
e op o
stack and put it in a register.– Load the value pointed to by the stack pointerinto the register– Increment the stack pointer
// preserve edx and ecx so previous code is// not disrupted^ push
edx push
ecx // do something using edx and ecx^ pop
ecx pop^
edx
instruction pushes the t^ t^
f^ ll^
i ht^
f th^
i t^
th
contents of all eight of the registers on thestack.
(push all)
instruction pops seven values from the stack and places the values in theregisters (in reverse order of PUSHA).registers (in reverse order of PUSHA).• These instructions provide a quick way tosave the state upon function entry.
){ ,
int r
= a; } • that is called by the main program int x^ = 5;float^ *y = 7.0;float w = &y;thefunc( w, x
);
x^
5 (value of x) 5 (value of x)
x^
5 (value of x)
w^
5 (value of x)address of 7.5 (y)
x^
5 (value of x)
w
5 (value of x)address of 7.5 (y)return address
x^
5 (value of x)
w
thefunc
5 (value of x)address of 7.5 (y)return addresslocal variable r
x^
5 (value of x)
w
thefunc
// param a
5 (value of x)address of 7.5 (y)return address local variable r
t^ t^
l^
ithi
parameters to ensure values are withinrange.• Some processors prohibit instructions frombeing fetched from the stack.
020 sub
whatever
508504 SP^ →
… 030
ret… 100
call sub 100
call sub 102
something
Stack pointer
504
Program Counter
100
020 sub
whatever
508504
ret… 100
call sub
SP^ → 500
102
call sub 102
something
Stack pointer
500
Program Counter
020
020 sub
whatever
508504
ret… 100
call sub
SP^ → 500
102
call sub 102
something
Stack pointer
500
Program Counter
022
020 sub
whatever
508504 SP^ →
… 030
ret… 100
call sub
102
call sub 102
something
Stack pointer
504
Program Counter
102
t^
k b f^
lli^ th
f^
ti
on the stack before calling the function.• Parameters are usually pushed in a right toleft order. The left most parameter is thenon top.• The function can access them using an• The function can access them using anoffset from the stack pointer.• The stack must be popped or incrementedupon return from the function.
020 sub1 mov eax,4[esp]
ret
50C^508
ret… 100
mov^
eax, y
104
push eax 106
mov^
eax x
504500 x^17
mov^
eax, x
10A^
push eax 10C^
call^
sub
110
add^
esp,
Stack pointer
50C
Program Counter
y^43
020 sub1 mov eax,4[esp]
ret
50C^508
ret… 100
mov^
eax, y
104
push eax 106
mov^
eax x
504500 x^17
mov^
eax, x
10A^
push eax 10C^
call^
sub
110
add^
esp,
Stack pointer
50C
Program Counter
y^43
020 sub1 mov eax,4[esp]
ret
50C^508
43
ret… 100
mov^
eax, y
104
push eax 106
mov^
eax x
SP^ →
504 17500 110 x^17
mov^
eax, x
10A^
push eax 10C^
call^
sub
110
add^
esp,
Stack pointer
500
Program Counter
y^43
020 sub1 mov eax,4[esp]
ret
50C^508
43
ret… 100
mov^
eax, y
104
push eax 106
mov^
eax x
SP^ →
504 17500 110 x^17
mov^
eax, x
10A^
push eax 10C^
call^
sub
110
add^
esp,
Stack pointer
504
Program Counter
y^43
020 sub1 mov eax,4[esp]
ret
50C^508
43
ret… 100
mov^
eax, y
104
push eax 106
mov^
eax x
504 17500 110 x^17
mov^
eax, x
10A^
push eax 10C^
call^
sub
110
add^
esp,
Stack pointer
50C
Program Counter
y^43
(i.e. int, char, address,
t^ )^
t^ d i^ th
i t
etc.)^ are returned in the eax register.• For more complex data types
(i.e. objects,
arrays)
the return value is in memory and the eax register contains the address ofthe returned value.the returned value.
ti^
i bl )
th
ll^ t d
automatic variable) are those allocatedwithin a function.• Local variable are allocated on the stack.• When a function returns, the stack spaceis available for other functionsis available for other functions.
void sub1(
int
x^ )
int^
a; int^
b; int^
b; … return;} int main( )
int main( )
int^
x^ =^
sub1( x
020 sub1 mov eax,-4[esp]
ret
(^510) 50C
ret… 100
mov^
eax, x
104
push eax 106
call^
sub
508504500
call^
sub
10A^
add^
esp,
Stack pointer
510
Program Counter
100
020 sub1 mov eax,-4[esp]
ret
(^510) 50C
ret… 100
mov^
eax, x
104
push eax 106
call^
sub
508504500
call^
sub
10A^
add^
esp,
Stack pointer
510
Program Counter
104
020 sub1 mov eax,-4[esp]
ret
(^510) 50C^
17
ret… 100
mov^
eax, x
104
push eax 106
call^
sub
508
10A 504
a 500
b
call^
sub
10A^
add^
esp,
Stack pointer
510
Program Counter
10C