140 likes | 226 Views
Lecture 3a: Supplemental Notes for Chapter 3. CS 447 Jason Bakos. Review of Branch Instructions. b <label> Unconditional branch Pseudoinstruction beq <r1>, <r2>, <label> Branch if equal bgez <r1>, label Branch if greater than or equal 0 bgezal <r1>, label
E N D
Lecture 3a:Supplemental Notes for Chapter 3 CS 447 Jason Bakos
Review of Branch Instructions • b <label> • Unconditional branch • Pseudoinstruction • beq <r1>, <r2>, <label> • Branch if equal • bgez <r1>, label • Branch if greater than or equal 0 • bgezal <r1>, label • Branch if greater than or equal 0 and link
Review of Branch Instructions • bgtz <r1>, <label> • Branch if greater than zero • blez <r1>, <label> • Branch if less than or equal 0 • bltzal <r1>, label • Branch if less than 0 and link • bltz <r1>, label • Branch if less than 0
Review of Branch Instructions • bne <r1>, <r2>, <label> • Branch if not equal • beqz <r1>, <label> • Branch if equal 0 • Pseudoinstruction • bge <r1>, <r2>, label • Branch if greater than or equal • Pseudoinstruction • bgeu <r1>, <r2>, label • Branch if greater than or equal unsigned • Pseudoinstruction
Review of Branch Instructions • bgt <r1>, <r2>, <label> • Branch if greater than • Pseudoinstruction • bgtu <r1>, <r2>, <label> • Branch if greater than unsigned • Pseudoinstruction • blte <r1>, <r2>, label • Branch if less than or equal • Pseudoinstruction • blteu <r1>, <r2>, label • Branch if less than or equal unsigned • Pseudoinstruction
Review of Branch Instructions • blt <r1>, <r2>, <label> • Branch if less than • Pseudoinstruction • bltu <r1>, <r2>, <label> • Branch if less than unsigned • Pseudoinstruction • bnez <r1>, label • Branch if not equal zero • Pseudoinstruction
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
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
Jump Instructions • j <label> • Unconditional jump (used when b instruction is used) • jal <label> • Jump and link (used for procedure call) • jalr <r1>, <r2> • Jump and link register (same as above, but links into r2) • jr <r1> • Jump register (used to return from a procedure)
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
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
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
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
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