Presentation on theme: "CS 300 – Lecture 10 Intro to Computer Architecture / Assembly Language Strings and Characters and More."— Presentation transcript:
CS 300 – Lecture 10 Intro to Computer Architecture / Assembly Language Strings and Characters and More
Function Calling Basic ideas: * Parameters passed into function * Return address * Return values * Registers saved / destroyed during the call * Saving other data during the call
The GORY Details So what does a function look like? Initially: * Frame allocation * Save values to the frame (including return address). Usually need to move args there. Save $s0 - $s7 if used later. Finally: * Put return values in proper registers. * Resture $s0 - $s7 * Deallocate the frame * Branch to the return address ("jr") Calling other functions: * Push needed information that is not in the stack frame or a saved register * Perform "jal" * Pop the stack
And Now for the Pentium … _fact: pushl%ebp movl%esp, %ebp subl$8, %esp cmpl$0, 8(%ebp) jneL2 movl$1, -4(%ebp) jmpL1 %ebp = $fp esp = $sp From "gcc –S" See 2.16 for more
And Now for the Pentium … L2:movl8(%ebp), %eax decl%eax movl%eax, (%esp) call_fact imull8(%ebp), %eax movl%eax, -4(%ebp) L1:movl-4(%ebp), %eax leave ret
More About $fp Why have both $sp and $fp? * Stack use varies within a function (why?). Using $fp, we always know where locals are * $fp allows for the creation of chained environments. This is needed in Java for nested objects. * $fp can accept incoming parameters from the stack top Note that you often don't need $fp at all.