CS270 Colorado State University ------------------- Register management R0-R3 -- 4 temporary registers, no convention specified R4 -- points to first address in global data section R5 -- frame pointer, callee-saved R6 -- stack pointer, should always point at top entry in stack R7 -- return address, callee-saved dynamic link ---------------- LC3 main routine LC3sim doesn't expect one therefore must - set up initial stack pointer (R5) - do not need to ==================== MIPS ==================== Appendix A from Patterson and Hennessy book, written by Jim Larus http://www.cs.colostate.edu/~cs270/Programs/cod-appa.pdf A.1 through A.5 A.10 - MIPS details A.11 - Short Conclusion MARS tutorial part 1 (http://courses.missouristate.edu/KenVollmar/MARS/tutorial.htm) ------------------------- Goals while covering MIPS - Overview of MIPS - Basic commands and short example programs. - Run-time stack operations in MIPS. - Comparison of MIPS with LC3 - MARS IDE ------------- MIPS Overview ------------- All machine code instructions are exactly 32 bits long. ----------------------------- three-address instruction set Arithmetic and logical instructions have 3 operands. Example: add $t2, $t1, $t0 The contents of registers $t1 and $t0 are added, and the result is placed in register $t2. Instruction format for arithmetic and logic instructions bits 31 26 25 21 20 16 15 11 10 0 6-bit 5-bit 5-bit 5-bit opcode reg reg reg more opcode bits 31 26 25 21 20 16 15 0 6-bit 5-bit 5-bit 16-bit opcode reg reg immediate ------------------ add.s example .text .globl main main: li $t0, 7 li $t1, 8 add $t2, $t1, $t0 # HALT li $v0, 10 syscall ------------------ other arithmetic and logical instructions addi and andi mul not or rem NOTE: 16 bits for immediates ----------------------------- load and store instructions The only instructions that access memory to get operands are load and store instructions. Examples: lw $t6, 4($t7) Effective address given by base displacement addressing mode (second operand). 4 + contents of register $t7 is the address. Memory reference is to 1 word (32 bits) at that address. The word read is placed into register $t6. sb $t2, 0($s2) Effective address given by base displacement addressing mode (second operand). 0 + contents of register $s2 are the address. The byte in the least significant byte of register $t2 is written to that address. Notes: The first operand is always register mode. The second operand is always base displacement mode. Instruction format for memory operations 31 26 25 21 20 16 15 0 6-bit 5-bit 5-bit 16-bit immediate opcode reg reg ------------------ memory.s example ------------------ -------------------- control instructions There are 2 types of control instructions. branch conditional or unconditional Branch decision (to take the branch or not) is based on a comparison between the values in 2 registers. (NOT condition codes!) An offset from the instruction following the branch is placed into the instruction. jump -- unconditional A portion of an absolute address is placed into the instruction. Examples: beq $t0, $t1, label1 Branch if EQual. If the contents of registers $t0 and $t1 are the same (equal), then go to label1. jal proc4 Jump And Link. A procedure call instruction. The return address is placed into register $ra. Then address proc4 is placed into the PC. Instruction format for branches (same as memory operations) 31 26 25 21 20 16 15 0 6-bit 5-bit 5-bit 16-bit offset opcode reg reg Instruction format for jumps 31 26 25 0 6-bit 26-bit address for target opcode -------------------- system calls (TRAPs) Many more useful system calls available in MIPS. print integer read integer file read, write, and close allocate memory on the heap etc. System calls take parameters in $v0 and $a0. $v0 contains the code that indicates which system call. $a0 contains any other parameter that might be needed. After system call, any return value will be in $v0. Example: li $v0, 5 syscall move $t0, $v0 -------------------------- .text .globl main main: li $t0, 3 loop: beq $t0, $0, endloop move $a0, $t0 jal foo addi $t0, $t0, -1 j loop endloop: # HALT li $v0, 10 syscall # What does foo do? foo: li $v0, 1 syscall la $a0, newline .data newline: .asciiz "\n" .text li $v0, 4 syscall jr $ra -------------------------- Study question How can you modify the above example so that the following output is generated? -8 -6 -4 -2 Some notes: la is not really a MIPS R2000 instruction. It is an example of and instruction that is allowed in assembly language code, but is translated into MIPS R2000 code by the assembler. This makes the job of writing this assembly language code easier. For this la instruction, the assembler produces (if symbol array is assigned address 0x00aa0bb0) lui $11, 0x00aa # $11 gets value 0x 00aa 0000 ori $11, $11, 0x0bb0 # $11 gets value 0x 00aa 0bb0 The MIPS architecture does this with LOTS of instructions. ------------------------------------------- Implementing LC3 calling convention in MIPS General - stack pointer ($sp) always points at top location in stack Caller before call - push arguments onto stack right-to-left after call - pop return value off the stack - pop arguments off the stack Callee - save and restore $ra and $fp - place return value at top of stack on top of arguments - do NOT modify global data pointer Full details of callee, one possible implementation of the above calling convention beginning of function (prologue) - allocate memory for return value - save and restore $ra and $fp, push $ra and $fp onto stack - set the frame pointer so that it will point at first local - allocate space for locals end of function (epilogue) - write return value in return value slot - pop local values off the stack - restore frame pointer ($fp) by popping it off stack --------------------- Comparing MIPS to LC3 LC3 MIPS ----------------------------------------------------------- addressability address space Labels Comments Initializing register to integer values Copying values between registers function call Allocate spot for return value. push return address set up new frame pointer load parameter n into R0 finishing the program unconditional branching conditional branching returning printing an integer printing a string [LEA R0, SUMSTR la $a0, L1 TRAP x22 li $v0, 4 ; rest of instructions syscall SUMSTR .STRINGZ "sum(" .data L1: .asciiz "\n" .text ] read an integer from standard in --------------------- main for MIPS and LC3 --------------- Study Exercises In Appendix A from the architecture book (written by Jim Larus and linked to PA5), what kind of loop is in Figure A.4 and what are the loop's bounds? Using MARS, write and test an adding machine program that repeatedly reads in integers and adds them into a running sum. The program should stop when it gets an input that is 0, printing out the sum at that point. Use the system calls described on pages A-48 and A-49 in the Appendix A from the architecture book. ------------------------ mstrout@cs.colostate.edu, 11/9/08 Some of the content of these notes was copied from http://pages.cs.wisc.edu/~smoler/x86text/lect.notes/MIPS.html.