Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 300 – Lecture 10 Intro to Computer Architecture / Assembly Language Strings and Characters and More.

Similar presentations


Presentation on theme: "CS 300 – Lecture 10 Intro to Computer Architecture / Assembly Language Strings and Characters and More."— Presentation transcript:

1 CS 300 – Lecture 10 Intro to Computer Architecture / Assembly Language Strings and Characters and More

2 Function Calling Basic ideas: * Parameters passed into function * Return address * Return values * Registers saved / destroyed during the call * Saving other data during the call

3 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

4 Aside: MIPS Register Convention NameRegister Number UsagePreserve on call? $zero0constant 0 (hardware)n.a. $at1reserved for assemblern.a. $v0 - $v12-3returned valuesno $a0 - $a34-7argumentsyes $t0 - $t78-15temporariesno $s0 - $s716-23saved valuesyes $t8 - $t924-25temporariesno $gp28global pointeryes $sp29stack pointeryes $fp30frame pointeryes $ra31return addr (hardware)yes

5 Example Factorial: f(x) = if x <= 0 then 1 else x*f(x-1) fact: addi $sp, $sp, -8 # Make frame sw $ra, 4($sp) # save ra, a1 sw $a0, 0($sp) slti $t0, $a0, 1 # a0 < 1? beq $t0, $zero, L1 addi $v0, $zero, 1 # return 1 addi $sp, $sp, 8 jr $ra

6 Factorial L1: addi $a0, $a0, -1 jal fact lw $a0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8 mul $v0, $a0, $v0 jr $ra

7 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

8 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

9 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.


Download ppt "CS 300 – Lecture 10 Intro to Computer Architecture / Assembly Language Strings and Characters and More."

Similar presentations


Ads by Google