COP 3402 meeting -*- Outline -*- * Overview of Homework 1 (Stack Machine VM) ** 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 ------------------------------------------ The actual due date is 11:59pm, see webcourses *** 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 ------------------------------------------ The Makefile can be used to build and test the VM on Unix use: make vm # to build the VM using sources.txt make check-outputs # to run the tests on Unix Unix commands: make vm # or gcc -g -std=c17 -Wall =o vm `cat sources.txt` make hw1-test1.out2 # or ./vm hw1-test1.txt >hw1-test1.out2 2>&1 (explain these commands, the first builds the VM, the second runs hw1-test1.txt as the program in the VM, sending the output to hw1-test1.out2, with redirect of stderr into the same place that stdout goes, using 2>&1) make check-outputs (will check your VM against the expected outputs) Hand in the .out2 files produced *** 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 ------------------------------------------ When CHI is executed, a single char is read from standard input Q: What would cause an error message? Division (or MOD) by 0 Trying to write to an illegal (e.g., non-existent) address etc. **** 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[]) { } ------------------------------------------ ... extern void machine(const char *filename); const char *cmdname = argv[0]; --argc; /* 1 non-option argument */ if (argc != 1 || argv[1][0] == '-') { usage(cmdname); } machine(argv[1]); return EXIT_SUCCESS; Note that EXIT_SUCCESS and EXIT_FAILURE are defined in stdlib.h **** 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) ------------------------------------------ **** example ------------------------------------------ SAMPLE INPUT (FILE hw1-test0.txt) 8 2 13 0 ------------------------------------------ This is read by the VM ------------------------------------------ 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 ------------------------------------------ I had to break the gcc command over 2 lines to fit on the slide Using the Makefile it's just make vm ------------------------------------------ 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 ------------------------------------------ Explain the output There are more examples in the hw PDF file and more tests in the tests.zip file Questions about how to access Eustis and how to use C can be the subject of next week's labs ** 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 ------------------------------------------ ... a word-addressed stack of ints (as in C) that grows upward. ... an accumulator ... 2nd from top element from the top element, popping them off the stack, putting the result in the top Try hw1-test2.txt, which does subtraction ------------------------------------------ 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 ------------------------------------------ Questions about any other instructions or specifications?