Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to Computer Architecture

Similar presentations


Presentation on theme: "Intro to Computer Architecture"— Presentation transcript:

1 Intro to Computer Architecture
CS 300 – Lecture 9 Intro to Computer Architecture / Assembly Language Function Calling

2 Homework 4 Any questions?

3 Hardware Support Dynamic allocation is usually handled using specific registers. On the MIPS, dynamic allocation is handled by convention. On other processors, these conventions are "wired in".

4 Representing Stacks / Heaps
A single register is usually enough to represent the current stack / heap. * Stack: points to the top of the stack. Stacks usually grow down (MIPS) * Heap: points to the next available word in the heap. Usually grows up. The Java heap is "managed" – you never need to "free" unused stuff.

5 Sharing Memory On the MIPS, memory is allocated to a process as follows: Stack Unused Heap Static memory allocation Stack Pointer ($sp) If the pointers cross, you're out of memory Heap Pointer ($gp) code, constants, static vars, system stuff

6 Examples How do we malloc()? Push a value on the stack? Pop?
Some languages are "safe" – you always check for dynamic memory collisions. Others are unsafe. Why would you ever want to use an unsafe language???

7 Function Calling Basic ideas: * Parameters passed into function
* Return address * Return values * Registers saved / destroyed during the call * Saving other data during the call What should the hardware do? The software?

8 Mips Register Conventions
The MIPS made the following choices: * 4 argument registers (common to caller and callee): $a0 - $a3 * 2 return registers (common to caller and callee): $v0, $v1 * $ra holds the return address (set by the call instruction: jal) * Return via “jr” instruction Note that $ra is "special"

9 MIPS Conventions Temporary registers ($t0 - $t9) are not preserved during a call Registers $s0 - $s7 are callee saves registers The information stored during the lifetime of a function lives in a "stack frame" – a chunk of continuous memory on the stack. The register $fp points to the current frame.

10 Example Factorial: f(x) = if x <= 1 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

11 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


Download ppt "Intro to Computer Architecture"

Similar presentations


Ads by Google