CS 203 - Lab 12 - Fall 2001


Lafayette College > Department of Computer Science > CS 203 > Labs > Lab #12

Summary

For this lab you will write a short program in IJVM assembly language.

Instructions

Write an IJVM program that asks the user to enter two numbers and prints the product of those two numbers. The code to perform the multiplication must be in a method invoked with the INVOKEVIRTUAL instruction. You may assume that the numbers entered are both positive. Extra credit will be given for correctly computing the result when one or both inputs are negative.

You may test your program using my IJVM interpreter and IJVMException.class. To run your assembled program (see the assembler instructions below), use a command like java IJVM myprogram.ijvm -- replace myprogram.ijvm with the name of your program. To allow you to debug your IJVM programs, my interpreter can be run in trace mode. In trace mode, my interpreter will, after each instruction, display the value of PC, SP, LV, and contents of the stack. To run my interpreter in trace mode, specify trace as the second argument. For example, to trace the squaring program from the last lab, enter java IJVM square.ijvm trace.

Construct your input files using my IJVM Assembler, for which you need the following five files: IJVMAssembler.class; Instruction.class; InstructionTable.class; Symbol.class; and SymbolTable.class. The IJVM Assembler takes as input an IJVM assembly language file and produces a file that can be used as input to the IJVM interpreter. The names of the files are given on the command line, so to assemble the file age.src and put the resulting code in age.ijvm, you would issue the command java IJVMAssembler age.src age.ijvm.

The IJVM assembly language file consists of a series of IJVM instructions written using the mnemonics given on page 222, execpt we omit WIDE and add the following.

Opcode Mnemonic Description
1 IPRINT pop the top value off the stack and print it to standard output
2 IREAD read an integer from standard input and push it on the stack
3 SPRINT "string" print string to standard output
4 HALT stop execution of the virtual machine
(Note that there is no IMUL instruction; your code must perform multiplication by repeated addition.)

For example, the following is the code for a program that asks the user to enter an age and then outputs a message depending on that age.

 SPRINT "How old are you?\n"
 IREAD

 DUP
 BIPUSH 18
 ISUB
 IFLT novote

 DUP
 BIPUSH 21
 ISUB
 IFLT nodrink

 SPRINT "You are old enough to vote and drink.\n"

:end
 HALT

:novote
 SPRINT "You can't vote and you can't drink.\n"
 GOTO end

:nodrink
 SPRINT "You can vote but you can't drink.\n"
 GOTO end
Note that for branch instructions, we can specify labels in the assembly code. Methods begin with a line specifying the name of the method, the number of parameters it takes (the unused OBJREF paremeter counts), and the number of local variables it uses. Examples of programs that use methods are in square.src and nim.src.

The IJVM Assembler is very picky about the syntax of the source code file. Please adhere to the following guidelines (and note that error messages from the assembler are not always helpful):

Assignment (due Monday, November 26th at 11:59pm)
Submit your code by attaching it to an e-mail. To send multiple files, attach them all individually to a single e-mail (no tar or pkzip archives, please).