top of page

Exercises

When writing each program, assume that the program’s input contains values according to the assumptions described in the program’s description. You may not assume that the memory or the registers contains any other values (except for RAM[98] and RAM[99], which contain 0 and 1, respectively).

1. Write a Vic program (readWritePos.asm) that reads a series of numbers that ends with a 0. For each number in the series, if the number is greater than 0, the program outputs the number. For example, When applied to the input ­-3,2,5,-­1,7,-­2,8,0, the program outputs 2,5,7,8.

2. Write a Vic program (find.asm) that reads a non­zero number (say x), followed by a series of numbers that ends with a zero. If x appears in the series (once or more), the program outputs its first location in the series; otherwise, the program outputs ­-1. For example, if the input is 7,2,1,7,2,5,0, the program outputs 3 (the first location is considerd 1). If the input is 5,3,1,­3,15,7,0, the program outputs ­1.

 

3. Write a program (odd.asm) that reads a single positive number from the input. If the number is odd, the program outputs 1; if the number is even, the program outputs 0. For example, if the number is 5, the program outputs 1; if the number is 8, the program outputs 0.

4. Write a program (multiply.asm) that reads two non­-negative numbers, and outputs their product. For example, if the input is 3,12, the program outputs 36.

5. Write a program (getMem.vic) that reads two numbers: the constant 300, followed by a number (say x) between 0 and 99. The program writes the value of the memory register whose address is x. For example, if x = 90, the program writes the value of RAM[90], i.e. the value of the memory cell whose address is 90. Tips: (i) this is a tricky program; (ii) the program must be written in Vic's native machine language: each command must be written as a 3-digit number;
 (iii) the first input, the constant 300, is needed in order to write this program.

bottom of page