# 1 Lecture 3a: Supplemental Notes for Chapter 3 CS 447 Jason Bakos.

## Presentation on theme: "1 Lecture 3a: Supplemental Notes for Chapter 3 CS 447 Jason Bakos."— Presentation transcript:

1 Lecture 3a: Supplemental Notes for Chapter 3 CS 447 Jason Bakos

2 Review of Branch Instructions b –Unconditional branch –Pseudoinstruction beq,, –Branch if equal bgez, label –Branch if greater than or equal 0 bgezal, label –Branch if greater than or equal 0 and link 4r1r2offset 65516 1r11offset 65516 4r10x11offset 65516

3 Review of Branch Instructions bgtz, –Branch if greater than zero blez, –Branch if less than or equal 0 bltzal, label –Branch if less than 0 and link bltz, label –Branch if less than 0 7r10offset 65516 6r10offset 65516 1r10x10offset 65516 1r10offset 65516

4 Review of Branch Instructions bne,, –Branch if not equal beqz, –Branch if equal 0 –Pseudoinstruction bge,, label –Branch if greater than or equal –Pseudoinstruction bgeu,, label –Branch if greater than or equal unsigned –Pseudoinstruction 5r1r2offset 65516

5 Review of Branch Instructions bgt,, –Branch if greater than –Pseudoinstruction bgtu,, –Branch if greater than unsigned –Pseudoinstruction blte,, label –Branch if less than or equal –Pseudoinstruction blteu,, label –Branch if less than or equal unsigned –Pseudoinstruction

6 Review of Branch Instructions blt,, –Branch if less than –Pseudoinstruction bltu,, –Branch if less than unsigned –Pseudoinstruction bnez, label –Branch if not equal zero –Pseudoinstruction

7 Notes on Branch Instructions Offset field represents offset in instruction words (bytes/4) –starting from the next instruction next instruction would be offset 0 can be negative (as can offset in load/store) Branch pseudoinstructions assemble into the equivalent comparison instruction followed by a bgtz And-link instructions load the address of the next instruction into \$31 (\$ra) if the branch is taken

8 Notes on Comparison Instructions There is a comparison instruction for every conditional branch instruction –They work just like the conditional branch instructions, but instead, if the comparison is evaluated true, the destination register is set to the value 1, and 0 otherwise slt and sltu follow the R-type format Comparison instructions aren’t really useful, except for slti and sltiu (unsigned), which are immediate comparison instructions –They follow the I-type format –Immediates are sign-extended!!! –Use these, followed by a bltz to compare a register and immediate and branch based on result seq, sge, sgeu, sgt, sgtu, sle, sleu, sne are pseudoinstructions

9 Jump Instructions j –Unconditional jump (used when b instruction is used) jal –Jump and link (used for procedure call) jalr, –Jump and link register (same as above, but links into r2) jr –Jump register (used to return from a procedure) 2target 626 3target 626 0r10r209 655556 0r100x8 65166

10 Notes on Pseudodirect Addressing j and jal use pseudodirect addressing –To compute the effective branch target address, the MS 6 bits of the PC are added to the address in the instruction

11 A More Robust Procedure Calling Convention We’ll use \$a0-\$a3 to pass parameters to a procedure We’ll use \$v0 and \$v1 for procedure returns We want to save \$s0-\$s7 across procedure calls We want to be able to have multiple levels of procedure calls and we want to be able to call procedures recursively –This means we need to somehow save arguments, state registers, and return addresses for each procedure call

12 A More Robust Procedure Calling Convention Solution: Use a stack! When each procedure is called, push arguments, return address, and saved registers onto a stack NOTE: The stack frames can be FIXED size using this convention To create a better procedure calling convention, we would also allow for variable numbers of arguments and local variable storage on the stack, but we will make this simpler

13 A More Robust Procedure Calling Convention For calling procedures: –Caller must move arguments into argument registers and jal to procedure –Callee must save arguments, return address, and machine state (registers) on stack, and decrement \$sp by 13*4=52 \$sp always points to next stack position stack grows down –Upon completion, callee must set the return value register(s), restore the \$s registers, restore \$ra, increment \$sp by 52, and jump back to the caller \$sp is already initialized when you start your program

14 More Notes You can use whatever procedure calling convention you want in your programs, but I recommend using this one Your programs’ procedures will share a static memory space (no scope) –In order to implement scope, we’d have to allocate local space on the stack

Download ppt "1 Lecture 3a: Supplemental Notes for Chapter 3 CS 447 Jason Bakos."

Similar presentations