Download Memory Addressing and Procedure Calls in Computer Architecture and more Slides Computer Science in PDF only on Docsity!
Computer Architecture
CPSC 321
Addressing: Byte vs. word
- Every word in memory has an address, similar to an index in an array
- Early computers numbered words like C numbers elements of an array:
- Memory[0], Memory[1], Memory[2], …
Called the “address” of a word
Computers needed to access 8-bit bytes as well as words (4 bytes/word)
Today machines address memory as bytes, hence word addresses differ by 4 Memory[0], Memory[4], Memory[8],
More Notes about Memory: Alignment
- MIPS requires that all words start at addresses that are
multiples of 4 bytes
Called Alignment: objects must fall on address that is multiple of their size.
0 1 2 3 Aligned
Not Aligned
Bytes in Word
Word Location
Instruction Support for Functions
... sum(a,b);... /* a, b: $s0,$s1 */ } int sum(int x, int y) { return x+y; }
address 1000 add $a0,$s0,$zero # x = a 1004 add $a1,$s1,$zero # y = b 1008 addi $ra,$zero,1016 #$ra= 1012 j sum #jump to sum 1016 ...
2000 sum:add $v0,$a0,$a 2004 jr $ra # new instruction
C
M
I
P
S
Instruction Support for Functions
- Syntax for jr (jump register):
jr register
- Instead of providing a label to jump to, the jr instruction provides a register which contains an address to jump to.
- Very useful for function calls:
- jal stores return address in register ($ra)
- jr jumps back to that address
Nested Procedures
int sumSquare(int x, int y) {
return mult(x,x)+ y;
- Routine called sumSquare; now sumSquare is calling
mult.
- So there’s a value in $ra that sumSquare wants to jump
back to, but this will be overwritten by the call to mult.
- Need to save sumSquare return address before call to
mult.
Memory Organization
0x40 0000
0x1000 0000
0x7fff ffff
Reserved
Stack segment
Dynamic data Static data
Text segment
Data segment
The memory is byte addressed. If you want to step word by word through the memory, then you have to increase your “pointer” each time by 4.
C memory Allocation
∞
Address
Code Program
Static
Variables declared
once per program
Heap
Explicitly created space,
e.g. , malloc(); C pointers
Stack
Space for saved
procedure information
$sp stack pointer
Using the Stack
- So we have a register $sp which always points to the last
used space in the stack.
- To use stack, we decrement this pointer by the amount of
space we need and then fill it with info.
- So, how do we compile this?
int sumSquare(int x, int y) {
return mult(x,x)+ y;
° }
Using the Stack (2/2)
°Compile by hand
sumSquare: addi $sp, $sp, -8 #space on stack sw $ra, 4($sp) #save ret addr sw $a1, 0($sp) # save y add $a1, $a0, $zero # mult(x,x) jal mult # call mult
lw $a1, 0($sp) # restore y
add $v0, $v0, $a1 # mult()+ y
lw $ra, 4($sp) # get ret addr
addi $sp, $sp, 8 # restore stack
jr $ra
Rules for Procedures
- Called with a jal instruction, returns with a jr $ra
- Accepts up to 4 arguments in $a0, $a1, $a2 and $a
- Return value is always in $v0 (and if necessary in $v1)
- Must follow register conventions (even in functions that only
you will call)! So what are they? Return address $ra Arguments $a0, $a1, $a2, $a Return value $v0, $v variables $s0, $s1, … , $s
Register Conventions (1/6)
- Caller: the calling function
- Callee: the function being called
- When callee returns from executing, the caller
needs to know which registers may have changed and which are guaranteed to be unchanged.
° Register Conventions: A set of generally
accepted rules as to which registers will be unchanged after a procedure call (jal) and which may be changed.
Register Conventions (3/6)
- $s0-$s7: No Change. Very important, that’s why they’re called saved registers. If the callee changes these in any way, it must restore the original values before returning.
- $sp: No Change. The stack pointer must point to the same place before and after the jal call, or else the caller won’t be able to restore values from the stack.
- $ra: Change. The jal call itself will change this register.
Register Conventions (4/6)
- What do these conventions mean?
- If function A calls function B, then function A must save any temporary registers that it may be using onto the stack before making a jal call.
- Function B must save any S (saved) registers it intends to use before garbling up their values
- Remember: Caller/callee need to save only temporary / saved registers they are using, not all registers.