



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Assignment; Class: Systems Software; Subject: Computer Programming; University: University of Central Florida; Term: Fall 2007;
Typology: Assignments
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Homework #1 (P-Machine) Due Sept 17, 2007 by 11:59 p.m. The P-machine: In this assignment you have to implement an interpreter for a virtual machine known as the P-machine (PM/0). The interpreter must describe a stack machine, consisting of a store named โstackโ organized as a stack and a โcodeโ store that contains the instructions. The CPU has four registers: register โbpโ points to the base of the current activation record in the stack, register โspโ points to the top of the stack, a program counter (pc) and an instruction register (ir). The ISA of the PM/0 has 22 instructions and the instruction format has three components <OP, L, M>: OP is the operation code. L indicates the lexicographical level. M depending of the operators it indicates:
Instruction Set Architecture (ISA) : There are 13 arithmetic and decision instructions that work on implicit operands at the top of the stack. These instructions use the operator OPR to indicate that the instruction is an arithmetic or decision operation, and the modifier M to select the instruction to be executed. For example, to multiply the two elements on top of the stack we can write OPR 0,4. ISA: 01 - LIT 0, M Push constant value (literal) M onto stack 02 โ OPR ( to be defined later) 03 โ LOD L, M Push from location at offset M from frame L levels up. 04 โ STO L, M Store in location at offset M from frame L levels up. 05 โ CAL L, M Call procedure at M (generates new block mark and pc ๏ M ). 06 โ INC 0, M Allocate M locals (increment sp by M), first three are SL , DL , RA. 07 โ JMP 0, M pc๏ M ; 08 โ JPC 0, M Jump to M if top of stack element is 0 and decrement sp by one. 09 โ WRT 0, 0 ( print (stack[sp]) and sp ๏ sp โ 1 02 - OPR: RTN 0,0 Return operation (i.e. return from subroutine) OPR 0,1 NEG(-stack[sp]) OPR 0,2 ADD (sp ๏ sp โ 1 and stack[sp] ๏ stack[sp] + stack[sp + 1]) OPR 0,3 SUB (sp ๏ sp โ 1 and stack[sp] ๏ stack[sp] - stack[sp + 1]) OPR 0,4 MUL (sp ๏sp โ 1 and stack[sp] ๏ stack[sp] * stack[sp + 1]) OPR 0,5 DIV (sp ๏ sp โ 1 and stack[sp] ๏ stack[sp] div stack[sp + 1]) OPR 0,6 ODD (stack[sp] ๏ stack mod 2) or ord(odd(stack[sp])) OPR 0,7 MOD (sp ๏ sp โ 1 and stack[sp] ๏ stack[sp] mod stack[sp + 1]) OPR 0,8 EQL (sp ๏ sp โ 1 and stack[sp] ๏ stack[sp] = = stack[sp + 1]) OPR 0,9 NEQ (sp ๏ sp โ 1 and stack[sp] ๏ stack[sp] != stack[sp + 1]) OPR 0,10 LSS (sp ๏ sp โ 1 and stack[sp] ๏ stack[sp] < stack[sp + 1]) OPR 0,11 LEQ (sp ๏ sp โ 1 and stack[sp] ๏ stack[sp] <= stack[sp + 1]) OPR 0,12 GTR (sp ๏ sp โ 1 and stack[sp] ๏ stack[sp] > stack[sp + 1])
stack[2] =0; stack[3] =0; Instructions The simulator of the virtual machine must be written in C. You must turn in by email: Source code, an example running in the virtual machine showing initial state of the stack and the stack state after the execution of each instruction. (see example) This example shows how to print the stack after the execution of each instruction: const n = 13; var i,h; procedure sub; const k = 7; var j,h; begin j:=n; i:=1; h:=k; end; begin i:=3; h:=0; call sub; end. INPUT FILE: It must be three integers and your program must print out: 7 0 10 7 0 2 6 0 5 we recommend to use as input format: 1 0 13 4 0 3 struct { 1 0 1 int op; /* opcode 4 1 3 int l; /* L 1 0 7 int m; /* M 4 0 4 }instruction; 2 0 0 6 0 5 1 0 3 4 0 3 1 0 0 4 0 4
OUTPUT FILE:
Print out the execution of the program in the virtual machine, showing the stack and registers pc, bp, and sp: