Project 8: Programming in pseudo-assembly

This project will give you practice in writing in an assembly-like language.

1. Introduction and setup

In class we have seen some examples of programs written for our virtual machine. In this project you will try your hand at writing a program yourself.

Copy the code for the virtual machine and disassembler from the course directory.

cp /homes/tvandrun/Public/cs245/proj8/* .

2. Your task

Write a program gcd.vml that computes the greatest common divisor of 180 and the integers from 18 to 24, using the Euclidean algorithm. In particular, implement a program from the following pseudo-code:

for (i = 18; i <= 24; i++)
   print gcd(180, i);


int gcd(int a, int b) {
  if (b == 0) return a;
  else return gcd(b, a mod b);
}

3. Hints

Recall that the file that the virtual machine reads in doesn't look like the examples we've seen in class. we have always looked at the disassembled versions---the actual file executed is just a sequence of numbers. The format is that the first number gives the number of instruction words, and the rest of the numbers are the actual instructions.

You'll probably want to write the program in phases. When I wrote mystery (the program that calculated the factorial of 5), I started by using "labels" instead of instruction positions for jumps.

        LOAD 5 r1
        LOAD 0 r2
        LOAD 1 r3
        LOAD L1 r4
        LOAD L2 r6
        PSH 4 r5
        WRLO 0 r1
        JAL r4
        POP r2
        RELO 0 r5
        PRN r5
        HLT
L1:
        RELO 0 r5
        IF r5 r6
        WRLO 0 r3
        RET
L2:
        SUB r5 r3 r7
        WRLO 1 r5
        WRLO 2 r29
        PSH 4 r8
        WRLO -1 r8
        WRLO 0 r7
        JAL r4
        RELO 0 r7
        RELO -1 r8
        POP r8
        RELO 2 r29
        RELO 1 r5
        MUL r5 r7 r8
        WRLO 0 r8
        RET

Then I counted off positions to determine what the labels were, and also replaced all the instructions and registers with plain numbers. Furthermore, I counted the number of instruction words.

84
        1 5 1
        1 0 2
        1 1 3
        1 31 4
        1 41 6
        14 4 5
        17 0 1
        12 4
        15 2
        16 0 5
        8 5
        9
L1:
        16 0 5
        7 5 6
        17 0 3
        13
L2:
        4 5 3 7
        17 1 5
        17 2 29
        14 4 8
        17 -1 8
        17 0 7
        12 4
        16 0 7
        16 -1 8
        15 8
        16 2 29
        16 1 5
        5 5 7 8
        17 0 8
        13

Finally, I made every number appear on a line by itself (and I got rid of the labels).

84
0
1 
5 
1
1 
0 
2
1 
1 
3
1 
31 
4
1 
41 
6
14 
4 
5
17 
0 
1
12 
4
15 
2
16 
0 
5
8 
5
9
16 
0 
5
7 
5 
6
17 
0 
3
13
4 
5 
3 
7
17 
1 
5
17 
2 
29
14 
4 
8
17 
-1 
8
17 
0 
7
12 
4
16 
0 
7
16 
-1 
8
15 
8
16 
2 
29
16 
1 
5
5 
5 
7 
8
17 
0 
8
13

4. To turn in

Turn in your code by copying your directory (eg, proj8) to a turn-in directory I've made for you.

cp -r proj8 /cslab.all/ubuntu/cs245/turnin/(your user id)

DUE:Monday, Dec 17, 5:00 pm. Remember that you may not use late days for this assignment.


Thomas VanDrunen
Last modified: Fri Dec 14 08:25:14 CST 2012