Session 13 Stack DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Aside: Spilling Registers What if the callee needs to use more registers than allocated to argument and return values? Callee uses a stack – a last-in-first-out queue One of the general registers, High address $sp 0xffffffff Top of stack $sp($29), is used to address the stack (which “grows” from high address to low address) Add data onto the stack – push $sp = $sp – 4 #put data on stack at new $sp 0x00000000 Low address Remove data from the stack – pop $sp = $sp + 4 #get data from stack at $sp Stacking of Subroutine Environments Procedures are nested, there are multiple calls and returns C is a “leaf” procedure, but B is a nested (non-leaf) procedure The stack grows downward at each new call, shrinks upward at each return A A: CALL B A B B: CALL C C: RET RET A B C A A B Aside: Allocating Space on the Stack The segment of the stack High address (0xffffffff) Saved argument regs (if any) $fp Saved return addr Saved local regs (if any) Local arrays & structures (if any) Low address (0x00000000) containing a procedure’s saved registers and local variables is its procedure frame (aka activation record) $sp The frame pointer ($fp) points to the first word of the frame of a procedure – providing a stable “base” register for the procedure $fp is initialized using $sp on a call and $sp is restored using $fp on a return Local Data on the Stack Local data allocated by callee e.g., C automatic variables Procedure frame (activation record) Used by some compilers to manage stack storage Memory Layout Text: program code Static data: global variables e.g., static variables in C, constant arrays and strings $gp initialized to address allowing ±offsets into this segment Dynamic data: heap E.g., malloc in C, new in Java Stack: automatic storage Chapter 2 — Instructions: Language of the Computer — 6 Using Registers to Implement Procedures Caller Save caller-saved registers $a0-$a3, $t0-$t9 Load arguments in $a0-$a3, rest on stack above $fp Execute jal instruction Callee Setup Allocate memory in frame ($sp = $sp - frame) Save callee-saved registers $s0-$s7, $fp, $ra Create frame ($fp = $sp + frame size) Callee Return Place return value in $v0 and $v1 Restore any callee-saved registers ($fp, $ra, $s0-$s7, …) Pop stack ($sp = $sp + frame size) Return by jr $ra Calling Convention: Steps fp sp First four arguments passed in registers Callee setup: step 1 fp sp Adjust $sp Callee setup: step 2 fp sp Before call: Callee setup: step 3 fp sp ra old fp $s0-$s7 Save registers as needed ra old fp $s0-$s7 Adjust $fp Byte-encoded character sets ASCII: 128 characters 95 graphic, 33 control Latin-1: 256 characters ASCII, +96 more graphic characters Unicode: 32-bit character set Used in Java, C++ wide characters, … Most of the world’s alphabets, plus symbols UTF-8, UTF-16: variable-length encodings Chapter 2 — Instructions: Language of the Computer — 9 §2.9 Communicating with People Character Data Byte/Halfword Operations Could use bitwise operations MIPS byte/halfword load/store String processing is a common case lb rt, offset(rs) Sign extend to 32 bits in rt lbu rt, offset(rs) lhu rt, offset(rs) Zero extend to 32 bits in rt sb rt, offset(rs) lh rt, offset(rs) sh rt, offset(rs) Store just rightmost byte/halfword Chapter 2 — Instructions: Language of the Computer — 10 String Copy Example C code (naive): Null-terminated string void strcpy (char x[], char y[]) { int i; i = 0; while ((x[i]=y[i])!='\0') i += 1; } Addresses of x, y in $a0, $a1 i in $s0 Chapter 2 — Instructions: Language of the Computer — 11 String Copy Example MIPS code: strcpy: addi sw add L1: add lbu add sb beq addi j L2: lw addi jr $sp, $s0, $s0, $t1, $t2, $t3, $t2, $t2, $s0, L1 $s0, $sp, $ra $sp, -4 0($sp) $zero, $zero $s0, $a1 0($t1) $s0, $a0 0($t3) $zero, L2 $s0, 1 0($sp) $sp, 4 # # # # # # # # # # # # # adjust stack for 1 item save $s0 i = 0 addr of y[i] in $t1 $t2 = y[i] addr of x[i] in $t3 x[i] = y[i] exit loop if y[i] == 0 i = i + 1 next iteration of loop restore saved $s0 pop 1 item from stack and return Chapter 2 — Instructions: Language of the Computer — 12