I. Overview of Homework 1 (Stack Machine VM) A. Problems ------------------------------------------ HOMEWORK 1 OVERVIEW Due: Jan. 31 Problems: 1. (10 points) email team membership 2. (100 points) Implement the stack VM in C. Submit on webcourses: - sources.txt file listing all .c files - all source files (.c, .h) - output of the tests ------------------------------------------ 1. what is provided ------------------------------------------ WHAT WE PROVIDE www.cs.ucf.edu/~leavens/COP3402/homeworks/ has: hw1-stack-machine.pdf - VM details - output format - Makefile hw1-tests.zip - test programs .txt files are test programs .out files are expected outputs - Makefile ------------------------------------------ 2. OS interface of the program ------------------------------------------ THE VM AS A UNIX PROGRAM One command line argument: filename - Mandatory argument is a file name This file contains the program Example program files (in hw1-tests.zip): hw1-test0.txt hw1-test1.txt ... Expected outputs (in hw1-tests.zip): hw1-test0.out hw1-test1.out ... Standard input (stdin) for: - CHI instructions execution Standard output (stdout) for: - Output from CHO instruction(s) - Listing of the program read - Trace output from the program's run Standard error (stderr) for: - error messages ------------------------------------------ What would cause an error message? a. Processing inputs ------------------------------------------ SAMPLE CODE FOR PROCESSING FILE NAME ARG #include #include // run the machine on the given file extern void machine(const char *filename); /* Print a usage message on stderr and exit with failure. */ static void usage(const char *cmdname) { fprintf(stderr, "Usage: %s code-filename\n", cmdname); exit(EXIT_FAILURE); } int main(int argc, char *argv[]) { } ------------------------------------------ b. Outputs ------------------------------------------ OUTPUTS OF THE VM The VM prints (on stdout): 0. A heading for the program listing 1. A listing of the program read using opcode mnemonics, 1 instruction per line 2. The message Tracing ... 3. The program's state - registers PC, BP, and SP - stack's values between BP and SP-1 4. The message ==> addr: followed by the PC's value and The instruction being excuted 5. The program's state again Steps 4 and 5 are repeated for each instruction executed (until program halts) ------------------------------------------ c. example ------------------------------------------ SAMPLE INPUT (FILE hw1-test0.txt) 8 2 13 0 ------------------------------------------ ------------------------------------------ BUILDING AND RUNNING THE INPUT Create the exeuctable: gcc -g -std=c17 -Wall \ -o vm `cat sources.txt` Run the VM with the Unix command: ./vm hw1-test0.txt ------------------------------------------ ------------------------------------------ SAMPLE OUTPUT Addr OP M 0 INC 2 1 HLT 0 Tracing ... PC: 0 BP: 0 SP: 0 stack: ==> addr: 0 INC 2 PC: 1 BP: 0 SP: 2 stack: S[0]: 0 S[1]: 0 ==> addr: 1 HLT 0 PC: 2 BP: 0 SP: 2 stack: S[0]: 0 S[1]: 0 ------------------------------------------ B. The ISA ------------------------------------------ STACK MACHINE ISA The instructions manipulate Registers: PC - program counter BP - base pointer SP - stack pointer The top of the stack acts as Example: SUB subtracts the 44 | | <- SP 43 | 7 | 42 | 5 | becomes 44 | | 43 | | 42 | 2 | <- SP ------------------------------------------ ------------------------------------------ IN hw1-test2.out PC: 5 BP: 0 SP: 6 stack: S[0]: 0 S[1]: 0 S[2]: 0 S[3]: 1 S[4]: 5 S[5]: 7 ==> addr: 5 SUB 0 PC: 6 BP: 0 SP: 5 stack: S[0]: 0 S[1]: 0 S[2]: 0 S[3]: 1 S[4]: 2 ------------------------------------------