John Reed
372 uarchhw
03 June 2005
Here is my microcode for the
ijvm for a shift right n opcode ISHR that’s shifts the second from the top word
of the stack by the top word of the stacks number, represented by the 5 least
significant bits, right and propagates the sign bit.
ishr1 OPC = 1 //initialize H to 1
ishr2 OPC = OPC << 8 //shift left 8 100000000
ishr3 OPC = OPC >> 1 //shift right 1 010000000
ishr4 OPC = OPC >> 1 //shift right 1 001000000
ishr5 OPC = OPC >> 1 //shift right 1 000100000
ishr6 OPC = OPC - 1 //subtract 1 000011111
ishr7 H = OPC //copy the bitmask to H
ishr8 MAR = SP = SP - 1; rd //Read in second from top word on stack
ishr9 TOS = H AND
TOS //apply the bitmask and save it in TOS
ishr10 H = -1 //initialize H to -1
ishr11 H = H >> 1 //shift right 1 011111....
ishr12 H = H + 1 //add 1 100000....
ishr13 OPC = H AND MDR //save the highest order bit
ishr14 H = MDR //get the word to shift
ishr15 Z = TOS; if (Z) goto ishr19; else goto ishr16
ishr16 H = H >> 1 //shift the word 1 bit right
ishr17 TOS = TOS - 1; goto ishr15 //loop until counter is 0
ishr18 H = H OR OPC //add the highest order bit back in
ishr19 TOS = MDR = H; wr; goto Main1 //save the result to the top of the stack
Here is my ijvm code to print
out the ascii code for a character typed in.
.main
.var
input
.end-var
L1: IN // request character
input from memory
DUP // duplicate top of stack (inputted char) for comparing
BIPUSH 0x0 // push 0x0 for comparison
IF_ICMPEQ L2 // if no characters are available for input, loop
DUP // make a copy for
second hex
ISTORE input //because return doesn’t go to right place for stack use a variable
BIPUSH 0x04
ISHR //shift over 4 to get the first hex
INVOKEVIRTUAL
findascii
//get the first hex
OUT // print the hex
ILOAD input
INVOKEVIRTUAL findascii //get the second hex
OUT // print the hex
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
//return the ascii value for the hex
value of the 4 least significant bits
.method findascii(n)
.var
temp
.end-var
ILOAD n // load the value to convert
BIPUSH 0xF
IAND // mask off last 4
bits
BIPUSH 0x30 //offset to number 0
IADD
ISTORE temp
BIPUSH 0x39 //ascii value of 9
ILOAD temp
ISUB
//check if number is greater than 9
IFLT AL //check if we need to print a letter
ILOAD temp
GOTO MO
ILOAD temp
IADD //if it is a letter we need to print add 7 to offset of A
MO: IRETURN
.end-method