I have to write assembly level code
It basically uses stacks sorta
It's not in java, and normally I wouldn't ask if I weren't so desperate, and also if javascript questions can be answered here, it seemed ok to ask.
Anyway,
I know that
ILOAD a
loads the variable a
And
BIPUSH 0
ISTORE a
sets a to 0
I know
DUP clones the top of the stack and pushes the clone to the top of the stack
I even found a code for Adding it appears and echoing
// 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: // // [url=http://www.gnu.org/copyleft/gpl.html]The GNU General Public License v3.0 - GNU Project - Free Software Foundation (FSF)[/url] // 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 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 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 IADD 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 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
And
// // echo.jas // // Author // Dan Stone // // Description // Sample assembly program that reads a key press and echoes the result to // standard output. // .main L1: IN // request character input from memory DUP // duplicate top of stack (inputed char) for comparing BIPUSH 0x0 // push 0x0 for comparison IF_ICMPEQ L2 // if no characters are available for input, loop OUT // else, print character GOTO L1 // loop back to beginning of program L2: POP // No key has been pushed, so clear the stack, GOTO L1 // and start over .end-main
They want me to write microinstructions for code for
X + Y - Z
They said:
[1] (35 points) Write an IJVM assembly program that accepts three integer values (say, X, Y, and Z)
from the console and then print the result of the following computation on the console screen:
X + Y – Z
Hints: You can refer to “add.jas” and “echo.jas” sample code. You may do the computation just
ONCE.
I can easily do it for regular java but assembly is a bit confusing.
I know I'm probably asking too much but I figured I'd ask anyway.
Hey, it created a link to a Wikipedia entry on IJVM! Cool!
Well, that might help explain things more! Way to go JPF setup!!!
Well, so much for the link.
Oh well...creating a new link....
IJVM - Wikipedia, the free encyclopedia