Presentation is loading. Please wait.

Presentation is loading. Please wait.

In this lecture Global variables Local variables System stack

Similar presentations


Presentation on theme: "In this lecture Global variables Local variables System stack"— Presentation transcript:

1 In this lecture Global variables Local variables System stack
18/09/2018 CMPUT 229

2 Global variables Exist for duration of program
Accessible from any part of program registers too volatile Use memory data segment # ASSEMBLY PROGRAM # Global variables # follow. .data 18/09/2018 CMPUT 229

3 .word allocates 4-byte value
Global variables Initialized global variables are declared with initial value int (32 bits) with .word and value other types according to size /* C PROGRAM */ /* declare var = -42 */ int var = -42; # ASSEMBLY PROGRAM # Global variables # follow. .data var: .word -42 value to store .word allocates 4-byte value 18/09/2018 CMPUT 229

4 .space allocates empty space
Global variables Uninitialized global variables are allocated by specifying size only use .space directive with bytes /* C PROGRAM */ /* declare var = -42 */ int var = -42; /* empty has no initial value. */ int empty; # ASSEMBLY PROGRAM # Global variables # follow. .data var: .word -42 empty: .space 4 number of bytes .space allocates empty space 18/09/2018 CMPUT 229

5 contents (some uninitialized)
Memory diagrams /* global variables */ int n = 42; int i; int *iptr = &i; start of data segment contents (some uninitialized) 0x 0x 0x lower addresses higher addresses variable names 42 n ??? i 0x iptr when variables contain addresses of other variables, helpful to draw arrow (pointer) 18/09/2018 CMPUT 229

6 Local variables Properties of local variables
accessible only within function may have more than one variable with same name (in different functions) may have more than one version of the same function’s variables existing (recursion) Properties of data segment accessible from all of program all labels must be different each location can hold only one discrete value Data segment is not suited to storing local variables 18/09/2018 CMPUT 229

7 Local variables Properties of local variables
must be allocated at function entry must be destroyed at function exit other functions may be called in between, with same rules void A() { int a; /* create a */ B(); /* destroy a */ } void B() { int b; /* create b */ C(); /* destroy b */ } void C() { int c; /* create c */ ... /* destroy c */ } 18/09/2018 CMPUT 229

8 Local variables Properties of local variables
allocation/destruction obeys LIFO (last in, first out) principle like stack data structure stack is ideal data structure for storing local variables allocate a variable by pushing it on the stack destroy a variable by popping it off the stack stack also helpful for storing other function information, for same reason saving registers storing return address passing function arguments 18/09/2018 CMPUT 229

9 System stack Function call/return mechanism is very widespread among computers Most computers provide a stack in memory for programs to use called system stack or runtime stack or stack in MIPS, stack resides in its own segment of memory stack segment, to address 0x7FFFFFFF stack is initialized by operating system user programs push/pop stack as needed instruction set provides operations for doing this Which ? 18/09/2018 CMPUT 229

10 System stack Register $sp (stack pointer) indicates top of stack
contains address of word of memory at top of stack (with lowest address) 18/09/2018 CMPUT 229

11 System stack lower addresses
this is free space for the stack to grow into 0x7FFFB308 0x7FFFB30C 0x7FFFB310 $sp 0x7FFFB310 0x7FFFB314 all of these words are part of the stack 0x7FFFB318 0x7FFFB31C higher addresses 18/09/2018 CMPUT 229

12 to push another word (this one) onto the stack ...
System stack: pushing lower addresses to push another word (this one) onto the stack ... 0x7FFFB308 0x7FFFB30C 0x7FFFB310 $sp 0x7FFFB310 0x7FFFB314 0x7FFFB318 subtract 4 from $sp ... 0x7FFFB31C higher addresses 18/09/2018 CMPUT 229

13 to push another word (this one) onto the stack ...
System stack: pushing lower addresses to push another word (this one) onto the stack ... 0x7FFFB308 0x7FFFB30C 0x7FFFB310 $sp 0x7FFFB30C 0x7FFFB314 0x7FFFB318 subtract 4 from $sp ... 0x7FFFB31C higher addresses then store a value here 18/09/2018 CMPUT 229

14 System stack: popping lower addresses
to pop a word (this one) off the stack ... 0x7FFFB308 ... fetch this word into a register ... 0x7FFFB30C 0x7FFFB310 $sp 0x7FFFB30C 0x7FFFB314 0x7FFFB318 ... then add 4 to $sp 0x7FFFB31C higher addresses 18/09/2018 CMPUT 229

15 System stack: popping lower addresses
to pop a word (this one) off the stack ... 0x7FFFB308 ... fetch this word into a register ... 0x7FFFB30C 0x7FFFB310 $sp 0x7FFFB310 0x7FFFB314 0x7FFFB318 ... then add 4 to $sp 0x7FFFB31C higher addresses 18/09/2018 CMPUT 229

16 Examples programs stack1.a stack6.a 18/09/2018 CMPUT 229


Download ppt "In this lecture Global variables Local variables System stack"

Similar presentations


Ads by Google