MIC-1 SIMULATOR Programming in IJVM Computer Architecture Jim Skon Topics Mic-1 simulator programming ◦ http://www.ontko.com/mic1/ ◦ Programming assignment on Moodle Reverse Polish notation Assembly language IJVM instruction set Using the Mic-1 Simulator Runs as a Java Application Simulates a simplified version of the Java Virtual Machine. Can be Downloaded from Moodle. Two Versions ◦ High resolution ◦ Low resolution Using the Mic-1 Simulator Write IJVM “.jas” files using an editor – (www.textpad.com) Start the Mic-1 simulator Load and Assemble the “.jas” file using the IJVM assembler. Use the “File/Assemble/ Load JAS file.” This creates machine code. Program is ready to run. Example: acsii.jas ◦ Prints all characters! Reverse Polish Notation (RPN) Method to write arithmetic expressions ◦ Avoids the use of brackets to define priorities for evaluation of operators Devised by Jan Lucasiewicz ◦ Polish philosopher and mathematician ◦ In his notation, the operators preceded their arguments ◦ The “reverse” places operators after arguments For more info http://wwwstone.ch.cam.ac.uk/documentation/rrf/rpn.ht ml http://en.wikipedia.org/wiki/Reverse_Polish_No tation RPN Example (3 + 5) * (7 -2) Add 3 to 5 Then, subtract 2 from 7 Finally, multiply the two results together Using RPN notation: 3 5 + 7 2 -* RPN Example – Stack based 3 5 + 7 2 -* (read from left to right) Push 3 onto stack Push 5 onto the stack – The stack now contains (3, 5) Use “+” operator – Pop two numbers from stack and push result (8) Push 7 onto the stack Push 2 onto the stack – The stack now contains (8, 7, 2) Use “-” operator – Pop two numbers from stack and push result (5) Use “*” operator – Pop two numbers from stack and push result (40) RPN Example – IJVM Code BIPUSH 3 BIPUSH 5 – The stack now contains (3, 5) IADD – Pop two numbers from stack and push result (8) BIPUSH 7 BIPUSH 2 – The stack now contains (8, 7, 2) ISUB – Pop two numbers from stack and push result (5) IMUL – Pop two numbers from stack and push result (40) Structured Computer Organization Digital logic builds microarchitecture Microarchitecture implements the ISA ISA is in machine language Assembly language allows us to use ISA Assembly Language Uses symbolic names (mnemonics) and symbolic addresses (variables) for the machine language An assembler converts the assembly language into machine language Each statement produces exactly one machine instruction (1:1 mapping) Why Use Assembly? Versus machine language ◦ Easier to remember mnemonics for instructions instead of corresponding machine language ◦ Easier to work with the symbolic addresses instead of numerical values of address Versus high-level language ◦ Access: has access to all features and instructions of the ISA ◦ Performance: Code produced can be much smaller (for low-memory devices) and faster (for speed-critical functions) Undocumented Assembler Instructions ARG : Agree to Run Garbage BDM : Branch and Destroy Memory CMN : Convert to Mayan Numerals DDS : Damage Disk and Stop EMR : Emit Microwave Radiation ETO : Emulate Toaster Oven FSE : Fake Serious Error GSI : Garble Subsequent Instructions GQS : Go Quarter Speed HEM : Hide Evidence of Malfunction IDD : Inhale Dust and Die IKI : Ignore Keyboard Input IJVM Instruction Set IJVM Instruction Set Exercise Write IJVM assembly code for the following C++ code: int x = 0; for (int i = 0; i < 20; i++) x+= i; Basic Program Format .main // all variables declared within .varand .end-var .var x i .end-var // Main program goes next // Program execution is terminated with a HALT statement // See next slide for exercise solution .end-main Solution to Previous Exercise BIPUSH 0 ISTORE x BIPUSH 0 ISTORE I L1: BIPUSH 19 ILOAD i ISUB IFLT L2 ILOAD x ILOAD i IADD ISTORE x IINC i 1 GOTO L1 L2: HALT Input and Output IN ◦ Reads a character from the key buffer ◦ Pushes its value onto the stack ◦ If no key has been pressed, zero will be pushed onto the stack OUT ◦ Pops a word off the stack ◦ Prints it to the standard output text area ◦ Can only output ASCII values Getting a Character from Keyboard GOTO getch L0: ISTORE z HALT getch: IN DUP IFEQ reread GOTO L0 reread: POP GOTO getch Assignment Get the ascii.jas, sum.jas, and echo.jas programs to all work. Write a program to read in a two digit number, and sum the even numbers from 2 to that number. Extra credit if you can print out the answer.