Understanding Function Call Implementation and Computer Architecture, Slides of Computer Science

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

2012/2013

Uploaded on 03/22/2013

dhrupad
dhrupad 🇮🇳

4.4

(17)

213 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
AssemblerAssemblerAssemblerAssembler
FunctionsFunctions
Computer Architecture
dO i ti
and Organization
Goals
Understand how function calls are
il td
i
mp
l
emen
t
e
d
.
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
Most
architectures
have
stack
pointer register. The stack
pointer always points to the top
item on the stack.
Program Memory Organization
Program instructions
Global data
Stack
Heap
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Understanding Function Call Implementation and Computer Architecture and more Slides Computer Science in PDF only on Docsity!

AssemblerAssemblerAssemblerAssemblerFunctionsFunctions

Computer Architecture

d O^

i^ ti and Organization

Goals

  • Understand how function calls arei^

l^

t d implemented.

Stacks

  • Many programming languagesuse stacks to pass parameters. • Many computer architectureshave stack instructions to helpimplement these programminglanguages.• Most architectures have stackMost architectures have stackpointer register. The stackpointer always points to the topitem on the stack.

Program Memory Organization

Program instructionsGlobal data

Stack Heap

Program Memory Organization

Program instructionsGlobal data

StackStackHeap

Intel method

Pushing and Popping

• A^ PUSH

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

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

Intel PUSHA and POPA

  • The

PUSHA

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)

  • The

POPA

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.

Upon function entry

  • Store the contents of the registers• Increase the stack pointer to reservememory for the local variable.• Start executing the function code.

Upon function exit

  • Reduce the stack by the size of the local i blvariable. - Pop the register values.• Execute the return instruction to pop theaddress from the stack into the programcountercounter.

Example Function Call

  • Consider the function^ void thefunc( float &b, int a

){ ,

int r

= a; } • that is called by the main program int x^ = 5;float^ *y = 7.0;float w = &y;thefunc( w, x

);

Stack for Call

  • push

x^

5 (value of x) 5 (value of x)

Stack for Call

  • push

x^

5 (value of x)

  • push

w^

5 (value of x)address of 7.5 (y)

Stack for Call

  • push

x^

5 (value of x)

  • push

w

  • call thefunc

5 (value of x)address of 7.5 (y)return address

Stack Use by Function

  • push

x^

5 (value of x)

  • push

w

  • call

thefunc

  • increment stack

5 (value of x)address of 7.5 (y)return addresslocal variable r

Stack for Call

  • push

x^

5 (value of 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

Stack Protection

  • Good programs should check all

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

Passing Parameters

  • Pass by value parameters can be pushedth

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

sub1( x, y ) SP^ →

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

sub1( x, y ) SP^ →

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

sub1( x, y )

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

sub1( x, y )

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

sub1( x, y ) SP^ →

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

Returning Function Results• Simple return types

(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.

Local Variables

  • Local variables (sometimes calledt^

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.

Example

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

sub1( x ) SP^ →

(^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

sub1( x ) SP^ →

(^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

sub1( x ) SP^ →

(^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