FIT1008 Lecture Notes - Lecture 3: Data Segment
Global&variables
="every"variable"has"a"label"to"identify"it,"unlike"local"variables
For"local"variables,"a"memory"map/diagram"is"required.
Stored"in"data"segment"because"they"have"no"labels
•
Accessible"only"from"within"a"function
•
Several"variables"with"same"name"(diff"scopes)"within"same"function
•
May"have">1"version"of"function's"variables"existing"(recursion)
•
Must"be"allocated"@"function"entry"and"deallocated"at"function"exit
•
Stack"ideal"for"storing"local"vars"(LIFO)"-system/runtime/process"stack
•
System&segment
Register"$sp (stack"pointer)
•
="points"to"top"of"stack,"used"to"alllocate/deallocate" memory"on"the"stack
Contains"address"of"word"of"memory"@"top"of"stack"(i.e."w/"lowest"
address)
○
Value"changes"during"execution"of"a"function
○
At"first,"there"may"already"be"data"on"the"stack
1.
Moves"up"by"the"number"of"function"arguments
Allocates"space"for"variables"by"pushing"necessary"space"onto"the"stack,"
4B"per"word
○
Initialise"space"by"storing"values"in"newly"allocated"space
○
2.
Moves"up"further"by"the"number"of"local"variables
3.
Since"stack"changes"to"accommodate"other"function"info"that"can"change"
during"the"function"execution,"the"locations"of"local"vars"relative"to"$sp"
change
•
Register"$fp"(frame"pointer)
•
="saved"copy"of"fp,"which"is"also"a"copy"of"sp,"and"used"to"access"local"
variables
Since"$sp"depends"on"other"factors,"better"to"access"local"vars"relative"
to"a"saved"copy of"$sp"(i.e."copy"made"before"subtracting"from"$sp"to"
allocate"local"variables)
○
-4($fp)
Start"of"local"variables
$fp
Where"$sp"was"before"allocating"local"variables
4($fp) Saved"$ra"(return"address)
8($fp) Start"of"arguments"of"function
!""#$%&$'()*+($,+-.+*("/0$1/2$+34$152
Thursday," 14"June"2018
12:56
Document Summary
= every variable has a label to identify it, unlike local variables. For local variables, a memory map/diagram is required. Stored in data segment because they have no labels. Several variables with same name (diff scopes) within same function. May have >1 version of function"s variables existing (recursion) Must be allocated @ function entry and deallocated at function exit. Stack ideal for storing local vars (lifo) - system/runtime/process stack. = points to top of stack, used to alllocate/deallocate memory on the stack. Contains address of word of memory @ top of stack (i. e. w/ lowest address) At first, there may already be data on the stack. Moves up by the number of function arguments. Allocates space for variables by pushing necessary space onto the stack, Initialise space by storing values in newly allocated space. Moves up further by the number of local variables.