: Write an IJVM assembly program that accepts five integer values in HEX (say, A
ID: 3767066 • Letter: #
Question
: Write an IJVM assembly program that accepts five integer values in HEX (say, A,B,C,D, and E) from the console and then print the result of the following computation on the console screen: A – B + C + D - E
I'm supposed to do this by modifying add.jas, which can be found here: http://www.ontko.com/mic1/add.jas
This is what I currently have, except it doesnt work at all. Any help would be greatly appreciated!
// add.jas
//
// mic1 microarchitecture simulator
// Copyright (C) 1999, Prentice-Hall, Inc.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
// Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to:
//
// Free Software Foundation, Inc.
// 59 Temple Place - Suite 330
// Boston, MA 02111-1307, USA.
//
// A copy of the GPL is available online the GNU web site:
//
// http://www.gnu.org/copyleft/gpl.html
// add.jas
//
// Author
// Dan Stone
//
// Description
// Program to input hex 2 numbers, add them, and print the result.
// Illustrates how to invoke methods.
//
// Usage
// To compile: java ijvmasm add.jas
// This will create add.ijvm
//
// To run: java mic1sim mic1ijvm.mic1 add.ijvm
// Click the Run button
// Enter the first number (up to 8 digit hex, using upper-case "A"-"F")
// Press return
// Enter the second number and press return
// The result will be displayed, with leading zeros (always prints 8 digits)
// Repeat as often as desired
// Click on the Stop button
.constant
OBJREF 0x40 // needed for method invokation - see S.C.O. chapter 4
.end-constant
.main // start of program
.var // local variables for main program
a
b
c
d
e
total
.end-var
start: BIPUSH 0x0 // initialize var a and b
DUP
ISTORE a
ISTORE b
ISTORE c
ISTORE d
ISTORE e
BIPUSH 0x20 // print " "
OUT
LDC_W OBJREF // prepare for method call
INVOKEVIRTUAL getnum
ISTORE a // store return value in a
BIPUSH 0x2b // print "+"
OUT
LDC_W OBJREF
INVOKEVIRTUAL getnum
ISTORE b // store return value in b
BIPUSH 0x3d // print "========"
DUP
DUP
DUP
DUP
DUP
DUP
DUP
OUT
OUT
OUT
OUT
OUT
OUT
OUT
OUT
BIPUSH 0xa
OUT
ILOAD a
ILOAD b
ISUB
ISTORE total // add a and b, store in total
LDC_W OBJREF
INVOKEVIRTUAL getnum
ILOAD c
IADD
ISTORE total
LDC_W OBJREF
INVOKEVIRTUAL getnum
ILOAD d
IADD
ISTORE total
LDC_W OBJREF
INVOKEVIRTUAL getnum
ILOAD e
ISUB
ISTORE total
LDC_W OBJREF // push OBJREF
ILOAD total // push total, parameter for method print
INVOKEVIRTUAL print
GOTO start // start over
.end-main
.method getnum()
.var
a
.end-var
BIPUSH 0x0 // initialize a
ISTORE a
geta: IN // read key press
DUP // duplicate key for comparison
BIPUSH 0xa // if key = cr,
IF_ICMPEQ return // return
DUP
BIPUSH 0x30 // if key < "0"
ISUB //
IFLT geta4 // goto geta4 (key is not a hex digit)
DUP
BIPUSH 0x3a // else if key < ":"
ISUB //
IFLT geta2 // goto geta2 (key is numeric character - "0"-"9")
DUP
BIPUSH 0x41 // else if key < "A"
ISUB //
IFLT geta4 // goto geta4 (key is not a hex digit)
DUP
BIPUSH 0x46 // else if key > "F"
SWAP //
ISUB //
IFLT geta4 // goto geta4 (key is not a hex digit)
DUP // else (key is letter - "A"-"F")
OUT // print key
BIPUSH 0x37 // convert key from character to number
ISUB //
GOTO geta3 // goto geta3
geta2: DUP
OUT // print key (numeric character)
BIPUSH 0x30 // convert key from character to number
ISUB
geta3: ILOAD a // shift a left 8 bits
DUP
IADD
DUP
IADD
DUP
IADD
DUP
IADD
IADD // add key to a
ISTORE a
GOTO geta // get next key
geta4: POP // pop invalid character
GOTO geta // get next key
return: OUT // print cr
ILOAD a // load a as return value
IRETURN // return
.end-method
.method print( total ) // print converts a number into a string of
// characters and prints them. All of the characters
// are pushed onto the stack, least significant
// digit first, then popped off and printed.
.var
place
index
.end-var
print: BIPUSH 0x9 // there are 8 nibbles in each integer--setting
// this as nine pushes 10 characters onto the
// stack, thus a total of ten printed digits,
// but setting this less does not remove the
// two leading zeros, just removes significant
// digits
ISTORE index
BIPUSH 0x1 // comparison bit
ISTORE place
print1: BIPUSH 0x0
ILOAD index // index = index - 1
BIPUSH 0x1
ISUB
DUP
IFEQ pall // if index = 0 goto pall
ISTORE index
ILOAD total // else
ILOAD place //
IAND // if 1st bit of current nibble is zero (total & place)
IFEQ print2 // goto print2
BIPUSH 0x1 // else set first bit of character
IADD
print2: ILOAD place // place = place << 1
DUP
IADD
ISTORE place
ILOAD total
ILOAD place
IAND // if 2nd bit of current nibble is zero (total & place)
IFEQ print3 // goto print3
BIPUSH 0x2 // else set second bit of character
IADD
print3: ILOAD place // place = place << 1
DUP
IADD
ISTORE place
ILOAD total
ILOAD place
IAND // if 3rd bit of current nibble is zero (total & place)
IFEQ print4 // goto print4
BIPUSH 0x4 // else set second bit of character
IADD
print4: ILOAD place // place = place << 1
DUP
IADD
ISTORE place
ILOAD total
ILOAD place
IAND // if 4th bit of current nibble is zero (total & place)
IFEQ print5 // goto print5
BIPUSH 0x8 // else set second bit of character
IADD
print5: ILOAD place // place = place << 1
DUP
IADD
ISTORE place
GOTO print1
pall: POP // Pop off leading 0's
POP
BIPUSH 0x9
ISTORE index
pall1: ILOAD index // index = index - 1
BIPUSH 0x1
ISUB
DUP
IFEQ return // if index = 0 return
ISTORE index
DUP
BIPUSH 0xa // else if character < 0xa goto pall1
ISUB
IFLT pall2
BIPUSH 0x37 // else convert character to "A"-"F"
IADD
OUT // print character
GOTO pall1 // goto pall (prepare & print next character)
pall2: BIPUSH 0x30 // convert character to "0"-"9"
IADD
OUT // print character
GOTO pall1 // goto pall1 (prepare & print next character)
return: BIPUSH 0xa // print cr
OUT
IRETURN // no return value
.end-method
Explanation / Answer
.constant
OBJREF 0x40
.end-constant
.main // start of program
.var // local variables for main program
a
b
total
.end-var
start: BIPUSH 0x0 // initialize var a and b
DUP
ISTORE a
ISTORE b
BIPUSH 0x20 // print " "
OUT
LDC_W OBJREF // prepare for method call
INVOKEVIRTUAL getnum
ISTORE a // store return value in a
BIPUSH 0x2a // print "*"
OUT
LDC_W OBJREF
INVOKEVIRTUAL getnum
ISTORE b // store return value in b
BIPUSH 0x3d // print "========"
DUP
DUP
DUP
DUP
DUP
DUP
DUP
OUT
OUT
OUT
OUT
OUT
OUT
OUT
OUT
BIPUSH 0xa
OUT
LDC_W OBJREF // push OBJREF
ILOAD a // push total, parameter for method print
ILOAD b // push total, parameter for method print
// INVOKEVIRTUAL MYMUL
IMULADD
ISTORE total // add a and b, store in total
LDC_W OBJREF // push OBJREF
ILOAD total // push total, parameter for method print
INVOKEVIRTUAL print
GOTO start // start over
.end-main
.method getnum()
.var
a
.end-var
BIPUSH 0x0 // initialize a
ISTORE a
geta: IN // read key press
DUP // duplicate key for comparison
BIPUSH 0xa // if key = cr,
IF_ICMPEQ return // return
DUP
BIPUSH 0x30 // if key < "0"
ISUB //
IFLT geta4 // goto geta4 (key is not a hex digit)
DUP
BIPUSH 0x3a // else if key < ":"
ISUB //
IFLT geta2 // goto geta2 (key is numeric character - "0"-"9")
DUP
BIPUSH 0x41 // else if key < "A"
ISUB //
IFLT geta4 // goto geta4 (key is not a hex digit)
DUP
BIPUSH 0x46 // else if key > "F"
SWAP //
ISUB //
IFLT geta4 // goto geta4 (key is not a hex digit)
DUP // else (key is letter - "A"-"F")
OUT // print key
BIPUSH 0x37 // convert key from character to number
ISUB //
GOTO geta3 // goto geta3
geta2: DUP
OUT // print key (numeric character)
BIPUSH 0x30 // convert key from character to number
ISUB
geta3: ILOAD a // shift a left 8 bits
DUP
IADD
DUP
IADD
DUP
IADD
DUP
IADD
IADD // add key to a
ISTORE a
GOTO geta // get next key
geta4: POP // pop invalid character
GOTO geta // get next key
return: OUT // print cr
ILOAD a // load a as return value
IRETURN // return
.end-method
.method print( total ) // print converts a number into a string of
// characters and prints them. All of the characters
// are pushed onto the stack, least significant
// digit first, then popped off and printed.
.var
place
index
.end-var
print: BIPUSH 0x9 // there are 8 nibbles in each integer--setting
// this as nine pushes 10 characters onto the
// stack, thus a total of ten printed digits,
// but setting this less does not remove the
// two leading zeros, just removes significant
// digits
ISTORE index
BIPUSH 0x1 // comparison bit
ISTORE place
print1: BIPUSH 0x0
ILOAD index // index = index - 1
BIPUSH 0x1
ISUB
DUP
IFEQ pall // if index = 0 goto pall
ISTORE index
ILOAD total // else
ILOAD place //
IAND // if 1st bit of current nibble is zero (total & place)
IFEQ print2 // goto print2
BIPUSH 0x1 // else set first bit of character
IADD
print2: ILOAD place // place = place << 1
DUP
IADD
ISTORE place
ILOAD total
ILOAD place
IAND // if 2nd bit of current nibble is zero (total & place)
IFEQ print3 // goto print3
BIPUSH 0x2 // else set second bit of character
IADD
print3: ILOAD place // place = place << 1
DUP
IADD
ISTORE place
ILOAD total
ILOAD place
IAND // if 3rd bit of current nibble is zero (total & place)
IFEQ print4 // goto print4
BIPUSH 0x4 // else set second bit of character
IADD
print4: ILOAD place // place = place << 1
DUP
IADD
ISTORE place
ILOAD total
ILOAD place
IAND // if 4th bit of current nibble is zero (total & place)
IFEQ print5 // goto print5
BIPUSH 0x8 // else set second bit of character
IADD
print5: ILOAD place // place = place << 1
DUP
IADD
ISTORE place
GOTO print1
pall: POP
POP
BIPUSH 0x9
ISTORE index
pall1: ILOAD index // index = index - 1
BIPUSH 0x1
ISUB
DUP
IFEQ return // if index = 0 return
ISTORE index
DUP
BIPUSH 0xa // else if character < 0xa goto pall1
ISUB
IFLT pall2
BIPUSH 0x37 // else convert character to "A"-"F"
IADD
OUT // print character
GOTO pall1 // goto pall (prepare & print next character)
pall2: BIPUSH 0x30 // convert character to "0"-"9"
IADD
OUT // print character
GOTO pall1 // goto pall1 (prepare & print next character)
return: BIPUSH 0xa // print cr
OUT
IRETURN // no return value
.end-method
.method MYMUL(a,b)
.var
parcial
i
.end-var
BIPUSH 0x0
ISTORE i
BIPUSH 0x0
ISTORE parcial
while:
ILOAD i
ILOAD a
ISUB
IFEQ done
IINC i 0x1
ILOAD parcial
ILOAD b
IADD
ISTORE parcial
GOTO while
done:
return: ILOAD parcial
IRETURN
.end-method
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.