lab 5: sdc virtual machine - iit-computer...

6
Illinois Institute of Technology CS 350 S ’17 - Hale Lab 5: SDC Virtual Machine Due Date: Thursday 3/9/2017 11:59PM This lab covers the material in lectures 10-12. You will be creating a virtual (software-based) implementation of a basic, decimal-based Von Neumann machine. You could also call this an emulator. Note that this is a two-part lab. What we’ll end up with is a line-oriented command-line program that reads in initial memory contents from a file and then lets the user enter commands to execute the program’s instructions and inspect registers and memory (much like an interactive debugger such as gdb). Your goal is to fill in all of the missing functionality in lab5.c which is indicated by “FILL ME IN” comments. You should add your own code in these locations. Coding Assignment (100 points total) For this lab, you will be initializing the CPU, the memory (from a file), and the registers. You will then dump out (print) the state of the CPU and registers, and the contents of memory interpreted both as raw decimal data and as decoded instructions. 1. Initialize the CPU, set the general purpose registers (GPRs), the IR, and the PC each to 0. 2. When you run your program command (./lab5), you must provide the file name for an SDC (.sdc) file as an argument. 3. In its simplest form, an SDC data file is a sequence of lines containing one integer per line. E.g., 1234 3456 -4567 0 2568 The first number goes into memory location 00, the second into 01, etc. Your program will stop reading if you hit end-of-file (EOF) or if you run out of memory locations (by trying to read past location 99). Your program will also stop if you read in a “sentinel” value, i.e. one outside the range -9999... 9999. (A sentinel value just tells you that you’ve run out of input.) 1

Upload: doannhi

Post on 03-May-2018

219 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Lab 5: SDC Virtual Machine - IIT-Computer Sciencecs.iit.edu/~khale/class/cs350/s17/handout/lab5.pdf · Lab 5: SDC Virtual Machine Due Date: ... Usage: ./lab5  Your

Illinois Institute of Technology CS 350 S ’17 - Hale

Lab 5: SDC Virtual MachineDue Date: Thursday 3/9/2017 11:59PM

This lab covers the material in lectures 10-12. You will be creating a virtual (software-based)implementation of a basic, decimal-based Von Neumann machine. You could also call this anemulator.

Note that this is a two-part lab. What we’ll end up with is a line-oriented command-lineprogram that reads in initial memory contents from a file and then lets the user enter commandsto execute the program’s instructions and inspect registers and memory (much like an interactivedebugger such as gdb).

Your goal is to fill in all of the missing functionality in lab5.c which is indicated by “FILLME IN” comments. You should add your own code in these locations.

Coding Assignment (100 points total)For this lab, you will be initializing the CPU, the memory (from a file), and the registers. Youwill then dump out (print) the state of the CPU and registers, and the contents of memoryinterpreted both as raw decimal data and as decoded instructions.

1. Initialize the CPU, set the general purpose registers (GPRs), the IR, and the PC each to 0.

2. When you run your program command (./lab5), you must provide the file name for anSDC (.sdc) file as an argument.

3. In its simplest form, an SDC data file is a sequence of lines containing one integer perline. E.g.,

12343456-456702568

The first number goes into memory location 00, the second into 01, etc. Your programwill stop reading if you hit end-of-file (EOF) or if you run out of memory locations (bytrying to read past location 99). Your program will also stop if you read in a “sentinel”value, i.e. one outside the range -9999. . .9999. (A sentinel value just tells you thatyou’ve run out of input.)

1

Page 2: Lab 5: SDC Virtual Machine - IIT-Computer Sciencecs.iit.edu/~khale/class/cs350/s17/handout/lab5.pdf · Lab 5: SDC Virtual Machine Due Date: ... Usage: ./lab5  Your

Illinois Institute of Technology CS 350 S ’17 - Hale

4. The SDC data file can contain comments, whitespace, and blank lines. If a line is blank orbegins with something that isn’t a number, ignore the whole line. If a line begins with anumber, read that number into memory but ignore any text after it. E.g., the followinginput would be equivalent to the simple example above.

This is a comment1234 so is this3456 ; this too

-4567 3 5 7 (ignore the 3 5 7)0 note leading space

2568This line is ignored too12345 this sentinel tells us to stop reading1111 so everything after it is ignored

5. Dump (print) the contents of the CPU and memory. Most of the formatted printing isdone for you. You just have to fill in the functions which generate the data to print! Oneof these functions will take the IR and decode the current instruction. Your solution’soutput should match the provided solutions output exactly. Use the techniques mentionedin previous labs for detecting differences. When you print out memory, note that you willskip locations whose contents contain zero.

Here is what it will look like when you run the solution with t1.sdc:

$> ./lab5-soln tests/t1.sdc=== SDC Virtual Machine Part 1 ===

Initial CPU:CPU STATE:==========PC: 00IR: 0000RUNNING: 1R0: 0R1: 0R2: 0R3: 0R4: 0R5: 0R6: 0R7: 0R8: 0R9: 0Encountered sentinel 10000 at location 25. Exiting read loop.MEMORY: @Loc, value, instr (nonzero values only):@ 00 5178 LDM R1, 78

2

Page 3: Lab 5: SDC Virtual Machine - IIT-Computer Sciencecs.iit.edu/~khale/class/cs350/s17/handout/lab5.pdf · Lab 5: SDC Virtual Machine Due Date: ... Usage: ./lab5  Your

Illinois Institute of Technology CS 350 S ’17 - Hale

@ 01 -5278 LDM R2, -78@ 02 6189 ADDM R1, 89@ 03 -6289 ADDM R2, -89@ 04 -2145 ST R1, 45@ 05 1345 LD R3, 45@ 06 3345 ADD R3, 45@ 07 4367 NEG R3@ 08 7810 BR 10@ 09 7009 BR 9@ 10 8112 BRP R1, 12@ 11 7011 BR 11@ 12 -8214 BRN R2, 14@ 13 7013 BR 13@ 14 9011 GETC@ 15 -9199 OUT@ 16 9221 PUTS 21@ 17 9345 DMP@ 18 -9455 MEM@ 19 9500 NOP@ 21 0097 HALT@ 22 0065 HALT@ 23 0048 HALT

Getting the Code Get the lab5 code by logging into fourier and running the following:

$> cp /home/khale/HANDOUT/lab5.tgz .$> tar xvzf lab5.tgzl5/l5/lab5.cl5/tests/l5/tests/t4.sdcl5/tests/t8.sdcl5/tests/t10.sdcl5/tests/t1.sdcl5/tests/t3.sdcl5/tests/t6.sdcl5/tests/t7.sdcl5/tests/t5.sdcl5/tests/t2.sdcl5/tests/t9.sdcl5/test_harnessl5/Makefilel5/lab5-solnl5/lab5.h$> cd l5$> makegcc -Wall -Wno-unused-variable -Wno-unused-but-set-variable -std=c99 -lm -o lab5 lab5.c$> ls

3

Page 4: Lab 5: SDC Virtual Machine - IIT-Computer Sciencecs.iit.edu/~khale/class/cs350/s17/handout/lab5.pdf · Lab 5: SDC Virtual Machine Due Date: ... Usage: ./lab5  Your

Illinois Institute of Technology CS 350 S ’17 - Hale

lab5 lab5.c lab5.h lab5-soln Makefile test_harness tests

You’ll see a few extra files this time. One of them is a header (.h) file. In larger C programs,it is a convention to put function prototypes, structure definitions, typedefs, and #defines inheader files, especially when they could potentially be used in other .c files in the project. Inthe lab5.h header you’ll see the definitions for the structs and typedefs used throughout theprogram and the function prototypes as well.

You can see how to run the program by running it without an argument:

$> ./lab5=== SDC Virtual Machine Part 1 ===Usage: ./lab5 <sdc file>

Your job here is to fill in the functions in lab5.c which have FILL ME IN comments writtenin them. See the comments in the code for further instructions. I’ve also included a test harnessfor you to test your code. You can run it like so:

$> make test# Testing lab5...Test 0 - [FAIL]Test 1 - [FAIL]Test 2 - [FAIL]Test 3 - [FAIL]Test 4 - [FAIL]Test 5 - [FAIL]Test 6 - [FAIL]Test 7 - [FAIL]Test 8 - [FAIL]Test 9 - [FAIL]Test 10 - [FAIL]0 out of 11 test cases passed

Notice that the code will initially fail all the test cases. Your job is to make it pass them all.These are example .sdc files, and I’ve included them in a separate directory named tests.You are free to add your own test programs in .sdc files in that directory. For grading, we willbe using other tests in addition to the ones in this directory, so make sure you pass them all.

A good strategy here to get started is to start with the main() function and work your waydown based on the functions that are called from there.

Hand-in InstructionsMake sure to put your name on your submission. Submissions without names will begiven zero points! For code, this means put a comment at the top of your C file with yourname on it.

For the code, you must hand it in digitally. I’ve made this a bit easier this time. Once you’rehappy with your code, in the directory where your code is (l5), run the following on fourier:

$> make handin

4

Page 5: Lab 5: SDC Virtual Machine - IIT-Computer Sciencecs.iit.edu/~khale/class/cs350/s17/handout/lab5.pdf · Lab 5: SDC Virtual Machine Due Date: ... Usage: ./lab5  Your

Illinois Institute of Technology CS 350 S ’17 - Hale

Late handins If you’re turning in your code late, you’ll need to e-mail it to me after havingcreated a .tgz file from it on fourier. Run the following in your l5 directory on fourier:

[you@fourier] tar cvzf ‘whoami‘-lab5.tgz lab5.c lab5.h Makefile

Note that those are backticks, not quotes surrounding the whoami command. whoami is acommand that prints out your username.

You’ll then want to use scp or Filezilla or the equivalent to get that file off of fourier ontoyour local machine and send it to me as an e-mail attachment.

Programming Notes• You’re welcome to add functions that aren’t already present in the skeleton (e.g. helper

functions).

• To read in a line of text and see what’s in it, we can’t use scanf because it ignores lineends. The skeleton uses fgets to read a line from the data file (as a string of characters)into a buffer; then it uses sscanf to do a formatted read of the buffer.

• First, the skeleton uses fgets to read a line of text from the data file into a buffer. fgetsreturns a pointer to char. If the pointer equals NULL, we hit end-of-file. (If it successfullyreads data, fgets returns a pointer to the buffer you used.)

• The usual case is that fgets copies characters from the file into the buffer until it seesthe end of the line (’\n’). (It does copy the ’\n’ into the buffer.) There’s also a safetyfeature: We give fgets the length of the buffer; if fgets reaches the end of the bufferbefore seeing the ’\n’, it stops, so as not to overrun the end of the buffer. This is a safetyfeature because a standard way to attack a program is to fill memory with evil code bywriting way past the end of a buffer.

• After fgets reads characters into the buffer, we can use sscanf to read data from thebuffer. Instead of scanf(format, &var1, ...), which reads data from standardinput, we use sscanf(string, format, &var1, ...) to read data from thestring.

• Like scanf, sscanf returns the number of items that that particular call managed toread, so we can tell whether or not the read found everything. E.g.,x=sscanf(buffer, "%d %d", &y, &z); tries to read two integers from thestring buffer into variables y and z. It sets x to 2,1, or 0, depending on whether it setsboth y and z or just y or neither y nor z.

• Note: Just because scanf or sscanf doesn’t find what it’s looking for (e.g., by notfinding an integer when reading with %d), that doesn’t always mean you hit end-of-file orend-of-string; it might be that there was more input but it just didn’t look like an integer.

5

Page 6: Lab 5: SDC Virtual Machine - IIT-Computer Sciencecs.iit.edu/~khale/class/cs350/s17/handout/lab5.pdf · Lab 5: SDC Virtual Machine Due Date: ... Usage: ./lab5  Your

Illinois Institute of Technology CS 350 S ’17 - Hale

SDC ISA Reference

Note here that the mnemonic for BRC will actually be either BRN or BRP depending on the sign.

6