Lehigh University CSE 109 Spring 2012 Specifications of the Stack Oriented Machine (STOM). Memory: 1024 locations, numbered 0, 1, ...,1023, each containing a word consisting of a signed five digit integer in the range -81023 to 81023, inclusive. All (int) arithemetic is done on a stack that starts at memory location 1023 and grows downward (1022, 1021, etc.). When a word is interpreted as an instruction, the first digit must be one of the operations below, and the second, third, fourth, and fifth digits give the memory location to which the operation refers. That is, given the instruction xyyyy, the opcode x=xyyyy/10000 ranges from -5 to 8, and the address yyyy=xyyyy%10000, ranges from 0 to 1023. Operations: opcode 0 read from console and store at address 1 write contents of address to console 2 push contents of address 3 copy contents of top of stack to address 4 pop stack 5 swap top of stack with next entry down, crash if only one entry on stack 6 jump to address 7 jump to address if top of stack is zero. If jump occurs, pop stack, else leave stack alone 8 jump to address if top of stack is negative. If jump occurs, pop stack, else leave stack alone -1 replace top two entries on stack with their sum (two pops, followed by a push of the sum of the two items popped) -2 replace top two entries on stack with their difference (two pops, followed by a push of the second item popped minus the first item popped) -3 replace top two entries on stack with their product (two pops, followed by a push of the product of the two items popped) -4 replace top two entries on stack with their quotient (two pops, followed by a push of the second item popped divided by the first item popped) -5 halt execution Execution: The program starts by executing the instruction in memory location 0. The next instruction is executed, unless a jump has occurred, in which case the instruction to which the jump has occurred is the next instruction. USING MY EMULATOR OF STOM The emulator is located in the file /proj/csc109/bin/stom The call to the file takes the form stom [-v] where is the input file and -v is an optional command that leads to more verbose output. The input file for stom should consist of lines of code, followed by a line only containing the letter 'E', followed, possibly, by lines of input data to be read by the STOM "read" (opcode=0) command. Each line of code (or data if that line is to be interpreted as data rather than code) should contain an integer of up to 5 digits. Anything on a line after the integer is ignored (a good place to put comments). The first line is loaded into memory location 0, the second instruction into memory location 1, etc. Here is a very simple program (it reads two numbers and displays their sum). 10 read (opcode=0) into memory location 10 20010 push contents of location 10 onto stack 10 read again into location 10 20010 push onto stack -10000 add top two items on stack and pop 30010 store result in location 10 40000 clear stack (good housekeeping) 10010 write contents of stack -50000 halt E 6 7