Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
9/4/2008
Goals
Assembler Functions
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.
Heap
9/4/2008
Stack Heap
Intel method
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
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
9/4/2008
9/4/2008
9/4/2008
9/4/2008
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
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 call sub1 something
508 SP 504
504 100
020 sub1 whatever 030 ret 100 102 call sub1 something
102
020 sub1 whatever 030 ret 100 102 call sub1 something
102
500 020
500 022
9/4/2008
Passing Parameters
020 sub1 whatever 030 ret 100 102 call sub1 something
508 SP 504 102
504 102
Pass by value parameters can be pushed on the th stack t k before b f calling lli th the f function. 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.
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
sub1( x, y )
17 43 50C 100
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
sub1( x, y )
17 43 50C 104
9/4/2008
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
sub1( x, y )
43
17 43 508 106
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
sub1( x, y )
43
17 43 508 10A
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
sub1( x, y )
43 17
17 43 504 10C
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
sub1( x, y )
43 17 110 17 43 500 020
9/4/2008
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
sub1( x, y )
43 17 110 17 43 500 024
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
sub1( x, y )
43 17 110 17 43 504 110
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
sub1( x, y )
43 17 110 17 43 50C 112
10
9/4/2008
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.
Example
void sub1( int x ) { int a; int b; return; } int main( ) { int x = 17; 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
sub1( x )
510 50C 508 504 500
020 sub1 mov eax,-4[esp] SP 030 ret 100 mov eax, x 104 push eax 106 call sub1 10A add esp,4
sub1( x )
510 50C 508 504 500
510 100
510 104
11
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 17
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 504 500 17 10A
50C 106
508 020
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 504 500 17 10A a b
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 17 10A a b
508 023
50C 10A
12
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 508 504 500 17 10A a b
510 10C
13