FIT1008 Lecture Notes - Lecture 3: Local Variable, Call Stack

53 views3 pages
Local&variable&out&points&to&the&resulting&string.
The&string&is&in&the&heap,&but&upon&return,&address&pointing&to&string&is&
stored&in&variable&x&of&main&function.
Although&local&variable&out&has&been&destroyed,&can&still&access&address&
pointing&to&string&through&variable&x.
!"#$ for:
Encapsulation of&a&sequence&of&instructions
Can&be&called&repeatedly/reused
Self-contained
Can&call&other&functions
Abstraction
To&pass&params&from&caller&to&function
§
Can&inform&through&return&variables&($v0,&$v1)
To&get&return&value&safely&back&to&caller
§
Hiders of&info
Ensures&user&can't&access/modify&internal&data
%%&'()*+,"-.'/-+0,"
jfoo =&jump&(go)&to&label
jal foo =&jump&to&label&and&link&(remember&origin)
$ra&=&PC&+&4
Used&to&call&function
jr $ra =&jump&to&address&contained&in&register
Used&to&return&to&original&location&of&function&call&to&
continue&from&there
jalr $t0 =&jump&to&register&and&link&(remember&origin)
$ra&=&PC&+&4
Works&but&12"*3+(+-"
Can't&call&other&functions
Limited&to&4&args&($a0-$a3)&and&2&return&values&($v0-$v1)
If&not&enough&registers&to&use&as&function&args,&save&args&on&
stack
If&there&are&some&impt&values,&save/restore&regs&on&stack
Must&only&write&to&"safe"&registers&($v/a/t)
Must&only&use&args,&not&local&variables
$ra is&saved&and&restored&on&stack&(below&$fp)&upon&function&entry/exit
4',/-+0,5/233+,6*/0,7#,-+0,
82.6"*59*:;',/-+0,<*59*=;)*2,$*=.2*59*30/23*72.">
[$fp&points&to&point&just&below&global&variables]
Save&temporary&registers&(if&any)&by&putting&them&on&stack
To&avoid&overwriting&temporary&registers&that&may&be&
needed&untampered&outside&of&the&function
1.
Push&function&args&onto&stack
$sp&points&to&top&arg&now
Alternative&would&be&to&use&registers&$a0-$a3,&but&that&
would&imply&a&maximum&fixed&number&of&args,&so&saving&all&
args&onto&stack&is&better
2.
Call&function&with&jal instruction3.
[callee&start]
Save&$ra and&$fp by&pushing&them&onto&stack
$fp&on&top&of&$ra
$fp&is&where&it&was,&just&below&global&variables
Allows&us&to&recover&the&state&of&both&when&leaving&any&
function&-impt&to&properly&restore&the&stack&and&line&from&
which&code&is&running
4.
Copy&$sp&to&$fp
Both&now&point&to&same&place
Moves&frame&pointer&for&easy&access&to&function&local&
variables&and&args
5.
Allocate&local&variables
$sp&now&top&of&local&variables,&but&$fp&stays&
Local&vars&only&accessible& to&function&while&function&is&
running,&so&they&should&be&in&stack&frame&of&function
6.
[&body&of&function]
Set&register&$v0&to&return value7.
Deallocates&local&variables&by&popping&allocated&space&(i.e.&adding&
space&back&to&$sp)
8.
Restores&$fp&and&$ra&by&popping&their&saved&values
lw $fp, 0($sp)
lw $ra, 4($sp)
9.
Return&with&jr $ra10.
[callee&end]
Clear&function&args&by&popping&allocated&space&for&them11.
Restore&temporary&registers&(if&any)&by&popping&from&stack12.
Use&return&value&in&$v013.
-4($fp) Start&of&local&variables
(local&variables&that&aren't&function&args&are&accessed&with&
negative&offset&from&$fp)
$fp Where&$sp&was&before&allocating&local&variables
4($fp) Saved&$ra&(return&address)
Only&accessed&when&$ra&needs&to&be&restored&to&its&
original&value&after&jal has&executed.
-
8($fp) Start&of&arguments&of&function&-usually&first&parameter
12($fp) Second&parameter&usually
**&this&usual&order&is&a&convention&and&could&be&reversed,&as&
long&as&everyone&followed&the&same&convention&throughout&
function&calls
&23*+,"-#2$*0;*?*5)'.)0"#@*
Jump&and&link&automatically&stores&return&address&in&$ra&so&
that&program&can&return&to&the&point&where&the&function&was&
called&once&the&function&finishes&execution.
Week$3:$functions
Wednesday,& 13&June&2018
20:19
Unlock document

This preview shows page 1 of the document.
Unlock all 3 pages and 3 million more documents.

Already have an account? Log in

Document Summary

To get return value safely back to caller. **jump instructions j foo = jump (go) to label jal foo = jump to label and link (remember origin) Used to call function jr = jump to address contained in register. Used to return to original location of function call to continue from there jalr = jump to register and link (remember origin) Limited to 4 args (-) and 2 return values (-) If not enough registers to use as function args, save args on stack. If there are some impt values, save/restore regs on stack. is saved and restored on stack (below ) upon function entry/exit. Function-calling convention (args -> [function] -> and -> local vars) [ points to point just below global variables] Save temporary registers (if any) by putting them on stack. To avoid overwriting temporary registers that may be needed untampered outside of the function.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents