Assembler Function Calls
9/4/2008
Goals
Assembler
Functions
• Understand how function calls are
i l
implemented.
t d
COMP375 Computer Architecture
and
dO
Organization
i ti
Stacks
• Many programming languages
use stacks to pass parameters.
• Many computer architectures
have stack instructions to help
implement these programming
languages.
• Most architectures have stack
pointer register. The stack
pointer always points to the top
item on the stack.
COMP375 Computer Architecture and Organization
Program Memory Organization
Program instructions
Global data
Stack
Heap
1
Assembler Function Calls
9/4/2008
Program Memory Organization
Program instructions
Global data
Stack
Heap
Intel method
Push Example
// preserve edx and ecx so previous code is
// not disrupted
push
edx
push
ecx
// do something using edx and ecx
pop
ecx
pop
edx
COMP375 Computer Architecture and Organization
Pushing and Popping
• A PUSH copies the value of a register
onto
t the
th top
t off the
th stack.
t k
– Decrement the stack pointer
– Store the register at the address pointed to by
the stack pointer.
• A POP
O remove
e o e the
e value
a ue o
on the
e top
op o
of
stack and put it in a register.
– Load the value pointed to by the stack pointer
into the register
– Increment the stack pointer
Intel PUSHA and POPA
• The PUSHA instruction pushes the
contents
t t off allll eight
i ht off the
th registers
i t
on the
th
stack. (push all).
• The POPA instruction pops seven values
from the stack and places the values in the
registers (in reverse order of PUSHA).
• These instructions provide a quick way to
save the state upon function entry.
2
Assembler Function Calls
Function Call Hardware
• All computers have machine language
i t ti
instructions
to
t supportt function
f
ti calls.
ll
• The level of hardware support varies with
modern computers providing more
support.
Intel RET instruction
• The RET or return instruction pops a value
f
from
the
th stack
t k and
d places
l
it in
i th
the program
counter register.
• Since the program counter contains the
address of the next instruction to execute,
this has the effect of branching back to the
calling program.
COMP375 Computer Architecture and Organization
9/4/2008
Intel Call instruction
• The CALL instruction basically pushes the
program counter
t on the
th stack
t k and
d
branches to a new location.
• There are many versions of the Intel CALL
instruction to support different addressing
modes and changes in privileges.
Steps to perform a function call
• Compute any equations used in the
parameters,
t
such
h as x=func(a
f
( + b)
b);
• Push the parameter values on the stack.
• Execute a call instruction to push the
return address on the stack and start
execution at the first address of the
function.
3
Assembler Function Calls
9/4/2008
Upon function entry
• Store the contents of the registers
• Increase the stack pointer to reserve
memory for the local variable.
• Start executing the function code.
Upon function exit
• Reduce the stack by the size of the local
variable.
i bl
• Pop the register values.
• Execute the return instruction to pop the
address from the stack into the program
counter.
counter
Example Function Call
• Consider the function
void thefunc( float &b,
, int a ){
int r = a;
}
Stack for Call
• push x
5 (value of x)
• that is called by the main program
int x = 5;
float y = 7.0;
float *w = &y;
thefunc( w, x );
COMP375 Computer Architecture and Organization
4
Assembler Function Calls
9/4/2008
Stack for Call
• push x
• push w
5 (value of x)
address of 7.5 (y)
Stack Use by Function
• push x
• push w
• call thefunc
• increment stack
5 (value of x)
address of 7.5 (y)
return address
local variable r
COMP375 Computer Architecture and Organization
Stack for Call
• push x
• push w
• call thefunc
5 (value of x)
address of 7.5 (y)
return address
Stack for Call
• push x
• push w
• call thefunc
• increment stack
• get R1,12[sp] // param a
5 (value of x)
address of 7.5 (y)
return address
local variable r
5
Assembler Function Calls
9/4/2008
Stack for Return
• push x
• push w
• call thefunc
• increment stack
• get R1,12[sp] // param a
• decrement stack
5 (value of x)
address of 7.5 (y)
return address
local variable r
Cleanup Stack
• push x
• push w
• call thefunc
•
•
•
•
•
increment stack
get R1,12[sp] // param a
decrement stack
return
decrement stack by 2
5 (value of x)
address of 7.5 (y)
return address
local variable r
COMP375 Computer Architecture and Organization
Stack for Return
• push x
• push w
• call thefunc
•
•
•
•
increment stack
get R1,12[sp] // param a
decrement stack
return
5 (value of x)
address of 7.5 (y)
return address
local variable r
Stack Overflow Attack
• A common security attack
i tto cause a program tto
is
overflow the stack.
• If the program stores a
value into ar[4], it will
right in the data past ar,
the return address.
• Instructions might be
loaded in the rest of the
stack.
5 (value of x)
address of 7.5 (y)
return address
local variable ar[4]
6
Assembler Function Calls
9/4/2008
Stack Protection
• Good programs should check all
parameters
t
to
t ensure values
l
are within
ithi
range.
• Some processors prohibit instructions from
being fetched from the stack.
020 sub1 whatever
…
030
ret
…
100
102
020 sub1 whatever
…
030
ret
…
100
102
call sub1
something
call sub1
something
508
504
SP → 500
102
020 sub1 whatever
…
030
ret
…
Stack pointer
Program Counter
500
020
100
102
COMP375 Computer Architecture and Organization
call sub1
something
508
SP → 504
Stack pointer
Program Counter
504
100
508
504
SP → 500
102
Stack pointer
Program Counter
500
022
7
Assembler Function Calls
9/4/2008
Passing Parameters
020 sub1 whatever
…
030
ret
…
100
102
call sub1
something
508
SP → 504
102
Stack pointer
Program Counter
504
102
sub1( x, y )
020 sub1 mov eax,4[esp]
SP → 50C
…
508
030
ret
504
…
500
100
mov eax, y
104
push eax
x
106
mov eax,
eax x
y
10A
push eax
10C
call sub1 Stack pointer
Program Counter
110
add esp,8
COMP375 Computer Architecture and Organization
17
43
50C
100
• Pass by value parameters can be pushed
on the
th stack
t k before
b f
calling
lli th
the ffunction.
ti
• Parameters are usually pushed in a right to
left order. The left most parameter is then
on top.
• The function can access them using an
offset from the stack pointer.
• The stack must be popped or incremented
upon return from the function.
sub1( x, y )
020 sub1 mov eax,4[esp]
SP → 50C
…
508
030
ret
504
…
500
100
mov eax, y
104
push eax
x
106
mov eax,
eax x
y
10A
push eax
10C
call sub1 Stack pointer
Program Counter
110
add esp,8
17
43
50C
104
8
Assembler Function Calls
9/4/2008
sub1( x, y )
020 sub1 mov eax,4[esp]
50C
…
SP → 508
030
ret
504
…
500
100
mov eax, y
104
push eax
x
106
mov eax,
eax x
y
10A
push eax
10C
call sub1 Stack pointer
Program Counter
110
add esp,8
43
17
43
508
106
sub1( x, y )
020 sub1 mov eax,4[esp]
50C
…
508
030
ret
SP → 504
…
500
100
mov eax, y
104
push eax
x
106
mov eax,
eax x
y
10A
push eax
10C
call sub1 Stack pointer
Program Counter
110
add esp,8
COMP375 Computer Architecture and Organization
43
17
17
43
504
10C
sub1( x, y )
020 sub1 mov eax,4[esp]
50C
…
SP → 508
030
ret
504
…
500
100
mov eax, y
104
push eax
x
106
mov eax,
eax x
y
10A
push eax
10C
call sub1 Stack pointer
Program Counter
110
add esp,8
43
17
43
508
10A
sub1( x, y )
020 sub1 mov eax,4[esp]
50C
…
508
030
ret
504
…
SP → 500
100
mov eax, y
104
push eax
x
106
mov eax,
eax x
y
10A
push eax
10C
call sub1 Stack pointer
Program Counter
110
add esp,8
43
17
110
17
43
500
020
9
Assembler Function Calls
9/4/2008
sub1( x, y )
020 sub1 mov eax,4[esp]
50C
…
508
030
ret
504
…
SP → 500
100
mov eax, y
104
push eax
x
106
mov eax,
eax x
y
10A
push eax
10C
call sub1 Stack pointer
Program Counter
110
add esp,8
43
17
110
17
43
500
024
sub1( x, y )
020 sub1 mov eax,4[esp]
SP → 50C
…
508
030
ret
504
…
500
100
mov eax, y
104
push eax
x
106
mov eax,
eax x
y
10A
push eax
10C
call sub1 Stack pointer
Program Counter
110
add esp,8
COMP375 Computer Architecture and Organization
43
17
110
17
43
sub1( x, y )
020 sub1 mov eax,4[esp]
50C
…
508
030
ret
SP → 504
…
500
100
mov eax, y
104
push eax
x
106
mov eax,
eax x
y
10A
push eax
10C
call sub1 Stack pointer
Program Counter
110
add esp,8
43
17
110
17
43
504
110
Returning Function Results
• Simple return types (i.e. int, char, address,
etc.)
t ) are returned
t
d in
i th
the eax register.
i t
• For more complex data types (i.e. objects,
arrays) the return value is in memory and
the eax register contains the address of
the returned value.
50C
112
10
Assembler Function Calls
9/4/2008
Example
Local Variables
• Local variables (sometimes called
automatic
t
ti variable)
i bl ) are th
those allocated
ll
t d
within a function.
• Local variable are allocated on the stack.
• When a function returns, the stack space
is available for other functions
functions.
sub1( x )
020 sub1 mov eax,-4[esp]
SP →
…
030
ret
…
100
mov eax, x
104
push eax
106
call sub1
10A
add esp,4
510
50C
508
504
500
Stack pointer
Program Counter
COMP375 Computer Architecture and Organization
510
100
void sub1( int x ) {
int a;
int b;
…
return;
}
int main( ) {
int x = 17;
sub1( x );
}
sub1( x )
020 sub1 mov eax,-4[esp]
SP →
…
030
ret
…
100
mov eax, x
104
push eax
106
call sub1
10A
add esp,4
510
50C
508
504
500
Stack pointer
Program Counter
510
104
11
Assembler Function Calls
9/4/2008
020 sub1 mov eax,-4[esp]
…
030
ret
…
100
mov eax, x
104
push eax
106
call sub1
10A
add esp,4
sub1( x )
510
SP → 50C
508
504
500
Stack pointer
Program Counter
020 sub1 mov eax,-4[esp]
…
030
ret
…
100
mov eax, x
104
push eax
106
call sub1
10A
add esp,4
17
50C
106
sub1( x )
510
50C
SP → 508
17
10A
504
a
500
b
Stack pointer
Program Counter
COMP375 Computer Architecture and Organization
508
023
020 sub1 mov eax,-4[esp]
…
030
ret
…
100
mov eax, x
104
push eax
106
call sub1
10A
add esp,4
sub1( x )
510
50C
SP → 508
10A
504
500
Stack pointer
Program Counter
020 sub1 mov eax,-4[esp]
…
030
ret
…
100
mov eax, x
104
push eax
106
call sub1
10A
add esp,4
17
508
020
sub1( x )
510
SP → 50C
17
508
10A
504
a
500
b
Stack pointer
Program Counter
50C
10A
12
Assembler Function Calls
9/4/2008
020 sub1 mov eax,-4[esp]
…
030
ret
…
100
mov eax, x
104
push eax
106
call sub1
10A
add esp,4
sub1( x )
SP → 510
50C
17
508
10A
504
a
500
b
Stack pointer
Program Counter
COMP375 Computer Architecture and Organization
510
10C
13
© Copyright 2026 Paperzz