






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







College of Computing
CS3220 | Pro cessor Design Spring 2000
CS3220 Issued: March 13, 2000 Homework 3 Due: March 22, 2000
Purp ose: This homework intro duces the iMIPS pro cessor and translation issues. The primary purp ose of these problems is to serve as a warm-up for Pro ject 3.
Reading: P&H Chapter 3 reviews MIPS instructions P&H Sections 6.1-6.3 review pip elining
Problems: 1. Intro duction to iMIPS: write machine co de
Collab oration: This assignment may b e done in pairs.
To turn in: See individual questions
NOTE: supp ort les for this assignment are available on the web: http://www.cc.gatech.edu/ classe s/AY200 0/cs322 0 spring/homework3/
Problem 1: Intro duction to iMIPS
This problem intro duces the iMIPS machine by having you write some machine co de for it. iMIPS is a pro cessor running a (severe) subset of the MIPS instruction set. In contrast to iJVM, I'm giving you a simulator and a complete circuit implementation for iMIPS up front. Your task is ultimately going to b e to convert iJVM programs into iMIPS programs.
The iMIPS is a register-based machine with 16 registers each 16- or 32-bits wide Instruc- tions all refer to op erands in registers except for LW (load word) and SW (store word), which each access one op erand in memory. The instruction set is MIPS-ish although I've picked op erand mnemonics that mimic their iJVM counterparts.
The machine is describ ed in two sections b elow: the architectural state and the instruction set with examples. Then I'll mention the simulator and hardware and nally get around to the assignment.
Architectural State
memory map: registers: (initial values) 0xffff -------------- | constants | PC: 0x00000 000 | | R0: 0x00000 000 (always) | | R1: 0x-------- ... R2: 0x-------- | | ... | | R14: 0x-------- R15 -> |------------| R15: 0x-------- | | | stack | | | PC "Program Counter" 0x1000 |------------| points to the next | | instruction byte | | ... R15 (by convention) is the stack pointer. | | It points to the next empty location | | and grows toward higher addresses. |------------| | | R14 (by convention) is the link register. PC -> | code | It holds the return address during R14 -> | | a procedure invocation. 0x0000 |------------|
As with iJVM, the simulator is 32-bit and the hardware is 16-bit.
*****************************************************************************/
Note that the layout of instructions { four sections each four bits wide { has the e ect of making them \readable" in hexadecimal: the leftmost digit of a four-digit (16-bit) instruction is the op co de (0-F) and the other three digits are registers or a combination of registers and immediate values.
The instructions marked with (D) in the chart have a delay slot, which means that the e ect of the instruction do es not take place until after the instruction fol lowing that instruction (the following instruction is said to b e \in the delay slot"). The delay slot arises b ecause these instructions require two pip eline stages in the implementation. For instance, LW requires two cycles to read from memory, one to use the ALU to compute the address and a second to actually read the memory.
! Example: you can't use the result of a LW in the very next instruction ! because of the delay slot: ! 0000 81 70! LW (R7 + 0), R1 read MEM32[R7] into R 0002 01 12! ADD R1, R1, R2 oops! gets old value of R1! ! ! To fix the problem above, you could add a NOP (no operation) ! instruction into the delay slot: ! 0000 81 70! LW (R7 + 0), R1 read MEM32[R7] into R 0002 00 00! NOP wait for LW to complete 0004 01 12! ADD R1, R1, R2 gets R1 loaded by LW ! ! Sometimes, though, there are unrelated instructions lying around that ! you can place into the delay slot, making the delay ``free'' ! 0000 81 70! LW (R7 + 0), R1 read MEM32[R7] into R 0002 03 45! ADD R4, R3, R5 unrelated operation 0004 01 12! ADD R1, R1, R2 gets R1 loaded by LW
Similarly, all the control transfer instructions require one extra instruction to change the PC. The e ect is that the instruction fol lowing a control transfer is always executed. This may seem o dd but is normal in rst-generation RISC architectures, e.g. MIPS and SPARC.
! Example: the GOTO instruction executes the following instruction ! regardless:
0012 c0 37! GOTO 12 + 37*2 control transfer 0014 02 13! ADD R1, R2, R3 always executed (delay slot) !... 0060 12 13! SUB R1, R2, R3 destination
Here's an example pro cedure. Note that you could contrive to reorder instructions and ll all the delay slots; I've left them as NOPs for clarity.
! int A(int x) ! { ! int y = B(x); ! ! return(y + C(x, y)); !}
A: ADDI4 R15, 12, R15! reserve space for old R14, x, y. SW R14, (R15 - 12)! save R SW R1, (R15 - 8)! save x ! y unused yet
JAL B, R14! call B(x) note x already in R NOP! delay slot for JAL; y will return in R
ADD R1, R0, R2! move y to R SW R2, (R15 - 4)! save y LW R1, (R15 - 8)! get x in R JAL C, R14! call C(x, y) x in R1, y in R NOP
LW (R15 - 4), R2! get back y for addition NOP! delay slot for LW ADD R1, R2, R1! compute y + C(x, y), leave in R
LW (R15 - 12), R14! get back old PC ADDI4 R15, -12, R15! deallocate stack space (in delay slot!) JR R14! return from A(), result in R NOP! delay slot for JR
Simulator and Hardware
There's an iMIPS simulator in the current version of cs3220-tools: lo ok for msim/msim.c. You can use the simulator to test your machine co de here and, in Pro ject 3, to test your translator.
A hardware implementation of iMIPS for the 10K20 exists and can b e downloaded from the web page.
Assignment
Okay, nally, here's the assignment for this problem: for the purp oses of learning the iMIPS machine, do the following:
A: Write iMIPS symb olic assembly to implement the familiar pro cedure fib(), b elow and then hand-assemble the symb olic as- sembly to bytes. The pro cedure must b e double-recursive, as writ- ten. The easiest way to do this, of course, is to write a .hex le on-line. Then you can also test it with the simulator. Turn in your co de.
/*
Stack state at each step, assuming a method with 3 arguments, 2 locals and an arbitrarily-selected starting position on the stack.
Step: A B C D
0x104c: SP -> 0x1048: value 0x1044: junk 0x1040: SP -> junk 0x103c: oldPC oldPC 0x1038: oldFP oldFP 0x1034: FP -> oldAP FP -> oldAP 0x1030: local4 local 0x102c: SP -> local3 local 0x1028: arg2 arg2 arg 0x1024: arg1 arg1 arg1 SP -> 0x1020: arg0 AP -> arg0 AP -> arg0 value
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ note that during the method invocation, arguments and locals are available accessed at address AP + n * 4.
The FP and AP registers seem annoyingly redundant but they are required b ecause iJVM programs refer to their arguments/lo cals by giving the o set from the AP p oint. ILOAD and ISTORE each take one argument, n, the numb er of words o set from AP. For instance, at p oint (C) in the execution ab ove, a program can load arg2 from lo cations 0x1028 by executing ILOAD 2.
Imagine, however, if you knew S P F P (and S P AP ) at every p oint in the co de. Then, you could de ne new instructions ILOADSP and ISTORESP that load/store based on the numb er of words o set from S P and use those to load/store the arguments/lo cals. For instance, if we knew that the stack at p oint (C) in the execution ab ove has three words on it (ab ove the stack frame), then we could load arg2 using ILOADSP -9.
It gets b etter: knowing the stack depth and allowing rewriting of IRETURN as well, you can eliminate the need for F P and AP entirely, allowing a minimal stack frame (one word) and resulting in an iJVM machine that's smaller and faster.
Linker Program
The linker reads in a .class le (or multiple les, as needed), extracts the metho ds found therein, do es some mild rewriting to map JVM byteco des to iJVM byteco des, links together the metho ds into one address space and then writes out one, statically-linked, iJVM executable in a .hex format.
The linker program (cs3220-tools/linke r/) consists of four .c les:
reader.c co de for reading in .class les. It's entirely uninteresting. analyzer.c p erforms control- ow analysis on a JVM metho d and returns a data structure representing the metho d as a graph of basic blo cks (\BBs"). This is the fundamental data structure used to represent programs in the linker: you'll want to understand the data structure (in analyzer.h) but only need a cursory understanding of how it is constructed (in analyzer.c). rewriter.c do es a little rewriting of basic blo cks: it enlarges iJVM's \vo cabulary" by translating a few JVM instructions into sequences of iJVM instructions. Your main interest in this pro cedure is that it do es a similar thing to what you will do: it makes a pass over all the byteco des in all the basic blo cks (i.e. over the data structure returned by the analyzer) and makes some small mo di cations to the data structure. You don't need to use any of the rewriter co de yourself, but your co de will b e similar. linker.c is the top level. It makes three passes over the program co de: