Last week we will explored some of the basics of using the JAM simulator. Today, we will use it for some example programs.
Remember, to start the Jam2000 simulator, first start DrScheme, and then evaluate the expression (load "/home/comp210/Projects/Jam/jam.ss"). Also, here's the JAM reference card again.
Translate the following Jam assembly code into machine code starting at location 2000, and execute it. This program calculates 1 + 2 + 3 + ... + 10; blank lines and labels are solely to improve readability.
; r0 will contain "i", which will count 1..10 ; r1 will contain "max", which is 10 ; r2 will contain "sumSoFar" ; r3 will contain the constant 1 (ldi 0 1) ; i <- 1 (ldi 1 10) ; max <- 10 (ldi 2 0) ; sumSoFar <- 0 (ldi 3 1) ; r3 <- 1 loop: (sub 4 0 1) ; compare i with max (well, compute i-max) (bgz 4 done) ; if i-max > 0, branch to label "done" (add 2 2 0) ; sumSoFar <- sumSoFar + i (add 0 0 3) ; i <- i + 1 (jmpi loop) ; go back and repeat done: (print 2) ; print sumSoFar (halt)
Remember that the JAM simulator really has two parts, the "inspector" which allows you to look at and modify the JAM contents, including entering your program, and the "simulator" which executes your program.
Try modifying the above to sum the cubes of 1..10.
If you have time, look at your class notes and use the in-class example equivalent to (set! x (- x y)). The JAM code was essentially as follows:
; assume x stored at location 100 ; assume y stored at location 200 (ldi 2 100) ; R1 <- 100 (ld 0 1) ; R0 <- Mem[R2] = Mem[100] = x (ldi 1 200) ; R1 <- 200 (ld 1 1) ; R1 <- Mem[R1] = Mem[200] = y (sub 0 0 1) ; R0 <- R0 - R1 = x - y (st 0 2) ; Mem[R2] = Mem[100] = x <- R0 = x - y(Note the reuse of registers, as suggested by students during class.) How do we need to change this code first? (One change: add a halt instruction.) How can you get values into memory to use?
Since we haven't covered the C language in class yet, today we're just concerned with the mechanics of using C, not what programs look like. The process for creating and running a C program is a little different from that used for Scheme.
(Note: The following isn't really C, but Extended C.)
The basic process for writing a C program is as follows:
For example, let's assume your program is called sample.cc. (For a sample, copy ~comp210/public_html/Labs/lab11/sample.cc to your directory.)
You'll find it most convenient if you make sure the emacs buffer is called sample.cc before you start writing. This allows emacs to notice that you're editing a C/C++ program, so that it can give you some syntactic DrScheme-like help. For example, it checks whether each closing paren matches an opening paren, and it helps with indentation.
At the UNIX prompt, type gcc -Wall sample.cc -o sample (Don't kill the editor program -- you'll probably want it again later.) g++ is the GNU C++ Compiler, -Wall tells it to show all warnings, and -o says the next argument is the file name for the resulting program. Without the -o sample, the resulting program will be given the filename a.out.
As an alternative, within emacs you can type Ctrl-c c (or the compiling menu option in emacs) to compile, without exiting the editor. That can be more convenient, but you have less control with the compiler options.
(This is somewhat analogous to the Check Syntax button of DrScheme, but it does more, translating your program to a JAM-like code.)
If there are any errors, go back to editing your program.
At the UNIX prompt, type sample.
(This is basically the same as using the Execute button of DrScheme.)
If there are any errors, go back to editing your program.
The given sample.cc is a very simple working C program. Try ~comp210/public_html/Labs/lab11/sampleerr.cc. You'll see an example of some errors during compilation. Fix them (the code tells you how) to get practice with editing C code.
If you have time, go back and work with the original buggy version of sampleerr.cc.