COS 126: Fall 1996
TOY Programming
Due: Wed. 10/16, 11:59pm

In this assignment you will write three programs in TOY machine language: mod.toy, prime.toy, and squares.toy. As detailed below, these programs are described by giving equivalent C programs. This assignment thus gives you a taste for how a C compiler, like lcc, translates a C program to SPARC machine language. Understanding the general principles of this translation helps deepen your understanding of high-level languages like C.

Programmers rarely write programs in machine language or assembly language, which is a symbolic representation of machine language, because doing so makes the programs dependent on a specific machine. Occasionally, however, assembly language is used for performance reasons, because the programmer has complete control over the machine's resources. The TOY Quick Reference page summarizes the TOY machine language and explains how to run the TOY simulator.

  1. (20%) Write a TOY program, mod.toy, that is functionally equivalent to the following C program.
    int p = 11, n = 55;
    
    int main(void) {
    	if (n%p == 0)
    		printf("%04X\n", p);
    	else
    		printf("%04X\n", 0);
    	return 0;
    }
    This program prints the value of p if p divides n and it prints 0 otherwise. TOY doesn't have a divide or modulus instruction, so you'll have to compute n%p some other way. Explain your algorithm in your readme file. The printf calls in the program above (and those below) mimic the output format of TOY's system call instruction (opcode 4); use this instruction in place of the printf calls. Finally, you don't need to write a main functionor any procedure for that matteryour TOY program should implement only the body of main shown above.

    You don't have to duplicate the code above exactly. You can, for example, rearrange the test so that you test whether or not n%p is nonzero. You may assume that n and p are positive.

    Make sure you test mod.toy with different values of n and p. It's easiest to change n and p if you use memory locations for them; you can then try different values by changing just one or two lines. This program and those below will be graded on correctness and good structure, but not on efficiency.

    Here's an easy way to test mod.toy with other inputs. Suppose you use locations 80 and 81 for n and p and initialize them with the default values shown above. You can overwrite these initializations with additional initializations appended to mod.toy from the standard input, e.g.,
    % cat mod.toy - | /u/cs126/bin/toy
    Toy simulator $Revision: 1.2 $
    80: 0015
    81: 03
    ^D
    0003
    PC = 3C
    R0 = 0000  R1 = 0003  ...  
    

    The "-" argument to cat causes it to read input from the standard input, which is the terminal in the example above. For more information, see the TOY Quick Reference page.

  2. (50%) Write a TOY program, prime.toy, that is functionally equivalent to the following C program.
    int mod(int n, int p) {	return n%p; }
    
    int n = 97;
    
    int main(void) {
    	int i;
    
    	for (i = 2; i <= n/2; i++)
    		if (mod(n,i) == 0)
    			break;
    	if (mod(n,i) == 0)
    		printf("%04X\n", i);
    	else
    		printf("%04X\n", 1);
    	return 0;
    }
    This program prints 1 if n is a prime or its smallest factor. You can assume that n > 2. Write a function to compute mod(n, p); this function is essentially a repacking of your code from part 1. Use a simple function linkage like the one illustrated in slide 9-6, or one of your own design. Describe your linkage conventions in your readme file. For full credit, you must use the jump-and-link and jump-indirect instructions for calls to mod.

    Again, be sure to test your program with different values of n. As the code above suggests, you should make n a "global" variable by assigning it a memory location, and use a register for local variables, like i.

  3. (30%) Finally, write a TOY program, squares.toy, that is functionally equivalent to the following C program.
    int a[100];
    
    int main(void) {
    	int i;
    
    	for (i = 0; i < 100; i++)
    		a[i] = i*i;
    }
    This program fills the array a with the squares of the first 100 integers. The "output" of your TOY program will simply be the dump printed by the TOY simulator, which will include the array a.

Turn in your code and your documentation with the command

/u/cs126/bin/submit 5 readme mod.toy prime.toy squares.toy

You can put comments in your TOY programs by starting a line with anything other than a number, e.g.

# D. Hanson, precept 1

Copyright © 1996 David R. Hanson / drh@cs.princeton.edu
$Revision: 1.2 $ $Date: 1996/10/27 00:49:37 $