Machine Problem 5, due November 8

Part of the homework for 22C:60, Fall 2008
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

Submit the source file mp5.a for your solution using the submit command, documented at:
-- http://www.divms.uiowa.edu/help/msstart/submit.html

The Problem

The file mp5data.o defines the external symbol DATA, just like mp4data.o did. This symbol is the address of a single null-terminated character string containing a program in the language of the interpreter you are to write. Consecutive characters in this string are commands to your program. Each command is one character long:

The first batch of commands are familiar, they are the same commands you had to implement for machine problem 4:

z -- home: x = y = 0
u -- move up one step: y = y - 1
d -- move down one step: y = y + 1
l -- move left one step: x = x - 1
r -- move right one step: x = x + 1
( -- save x and y on the stack.
) -- recover the saved values of x and y from the stack.
null -- stop.

The second batch of commands is new:

c -- clear accumulator: a = 0
0 -- a = (a × 10) + 0
1 -- a = (a × 10) + 1
2 -- a = (a × 10) + 2
3 -- a = (a × 10) + 3
 . . .
9 -- a = (a × 10) + 9

As a consequence of the above, c followed by a sequence of digits will load the a register with the decimal value of that digit sequence. The next batch of commands allows us to do arithmetic:

x -- load x register: x = a
y -- load y register: y = a
+ -- add: a = x + y
- -- subtract: a = x - y

As a consequence of the above, for example, (c5y-)x means the same thing as bbbbb. Finally, we add control structures to our language:

> -- skip the next character if a > 0
= -- skip the next character if a = 0
< -- skip the next character if a < 0
[ -- skip forward until you find ] and skip it too.
] -- skip backward until you find [ and then skip forward one place.

As a result, [comment] is a comment, it all gets ignored. The string, -=[*c=] outputs an asterisk only if x equals y. The string, c5>[*(xc1y-)=] outputs 5 asterisks, using a definite loop. The final command remains the same as in MP4:


all others -- print the character at screen location <x,y>.

Recall, of course, that the origin of the Hawk display, <0,0> is at the upper left corner of the screen, so moving down is done by incrementing y. Note, for this assignment, you are not required to use recursion, but there is a recursive implementation of the parenthesis commands that both makes sense and will help you in the next machine problem.

Write a program that interprets the above language and carries out the sequence of commands in the string. You will need to include the directive EXT DATA in your program and link your code to the data using link mp4.o mp4data.o as with the test program for Machine Problem 1, given in the solutions to Homework 6.

Grading Critereia