CSE 378 - Spring 1999

Machine Organization and Assembly Language Programming

Problem Set #5

Due Monday, May 10, 1998

This assignment continues the construction of the Java Virtual Machine (JVM) you began writing in Assignment 3.  Hopefully you designed the code in an extensible fashion, and it is debugged.

You have two alternatives:  extend your JVM code to implement the features in this assignment, or extend Ashish's sample program.  If you use the sample solution, you'll need to answer a few questions so that we know you understand what goes on in that code.  In this case, hand in your answers to the following in class on Monday:

  1. Given an opcode, how is the number of arguments computed? More generally, how does the lookup table work?

  3. Describe (briefly) what each function/procedure in the program does, and what it returns.  (Please don't give a re-hash of the comments in the program!)

You'll need to add the following new bytecodes:
Name Bytecode Instruction Format Operations Description
IALOAD 0x2e IALOAD POP v1, PUSH *(v1) Load from index at top of stack
IASTORE 0x4f IASTORE POP v1, POP v2, *(v1) = v2 Store at index at top of stack
IDIV 0x6b IDIV POP v1, POP v2, PUSH (v2/v1) Integer division (no rounding)
END 0x00 END End program End your program
Plus the original set, whose operations are described in more detail here:
Name Bytecode Instruction Format Operations Description
IADD 0x60 IADD POP v1, POP v2, PUSH (v2+v1) Integer add
ISUB 0x64 ISUB POP v1, POP v2, PUSH (v2-v1) Integer sub
IMUL 0x68 IMUL POP v1, POP v2, PUSH (v2*v1) Integer mul
ILOAD 0x15 ILOAD index PUSH *(index) Load from mem at index
ISTORE 0x36 ISTORE index POP v1; *index = v1 Store to mem at index
BIPUSH 0x10 BIPUSH immed PUSH immed Byte Immediate push
SIPUSH 0x17 SIPUSH immedhi immedlo PUSH immedhi_immedlo Short Immediate push
POP 0x57 POP POP v1 POP value, discard
IFEQ 0x99 IFEQ offsethi offsetlo POP v1; cond. br. to PC+ offsethi_offsetlo Branch if v1==0
IFNE 0x9A IFNE offsethi offsetlo POP v1; cond. br. to PC+ offsethi_offsetlo Branch if v1!=0
IFLT 0x9B IFLT offsethi offsetlo POP v1; cond. br. to PC+ offsethi_offsetlo Branch if v1<0
IFLE 0x9E IFLE offsethi offsetlo POP v1; cond. br. to PC+ offsethi_offsetlo Branch if v1<=0
IFGT 0x9D IFGT offsethi offsetlo POP v1; cond. br. to PC+ offsethi_offsetlo Branch if v1>0
IFGE 0x9C IFGE offsethi offsetlo POP v1; cond. br. to PC+ offsethi_offsetlo Branch if v1>=0
GOTO 0xA7 GOTO offsethi offsetlo Jump to PC+ offsethi_offsetlo Unconditional branch
IRETURN 0xAC IRETURN POP v1 Integer return

JVM Design

Remember that JVM is a stack machine.  This means it has a stack of operands in some portion of memory; and all instructions will deal with operands taken from that stack.  In addition, there is an area of local memory which the JVM can use.   Basically, the stack works similarly to MIPS registers, and the local memory is just like standard RAM.

For the purposes of this assignment, assume that all storage is in 32-bit words; your stack will need to hold up to 256 operands, and memory will need to be able to store up to 256 words.  You'll need to make space in your data segment for these areas.  Note that you'll need to keep track of the JVM's internal stack pointer in order to simulate execution.

Instruction Semantics

Hopefully the description of what each instruction does is clear from the table above.  Here are some more things to keep in mind:

Your Job

  1. You need to write the Execute and Store stages of the JVM.  Most of the instructions should only take a few real MIPS instructions to implement.  You may start with your code from Assignment 3, or from Ashish's sample code.

  2. Make sure your entire JVM is inside a function called jvm.  The function accepts the address of the bytecode program in $a0, the number of instructions in $a1, the address of the top of the stack in $a2, and the address of location 0 for local storage in $a3.  When done, the JVM function should copy the value at the top of the stack to register $v0 and return it.

  3. The final step is to write a Java bytecode program to sum the contents of an array, returning the result as a return value in $v0, and to verify that it runs on your JVM program.

Input format

As in assignment 3, make a file called test.class containing the following lines (in case you are using PCspim, paste these lines at the top of your JVM.s and comment them out before turning in):

        .word    <your array program size>
        .byte    <your array bytecode program>
        .word    14
        .word    1 2 3 4 5 6 7 8 9 99 100 101 500 999

In your main function, you should load the instruction count and the bytecode for your array program to appropriate parameters for your jvm function.  The array size should be loaded as the first word in JVM's local memory and should be followed by all the array values.  Once all this is done, you can call your jvm function to simulate this program.

What to Turn In

Your program should include your name; it is always a good idea to put a comment in your code to tell the reader who wrote the code.

Turn in a hardcopy (double-sided printout) of your program, as well as an electronic submission using the turnin program.  In case you are using Ashish's sample solution, you also need to hand in written answers on Monday.