CS 135: Team Homework 5: Due October 26th, 12noon

In this assignment you will write a very simple LC3 program that implements the modulo operator (for positive numbers) using a subroutine -- i.e., for inputs N and K, your program will return the number (N Modulo K). You will need the modulo operator for your project 2, so make sure you do this correctly and seek help from the instruction team if you have trouble completing this assignment. Your program must have a “main” and a subroutine “MODULO” with the following specification. Your program must be loaded at address x3000 (i.e., it must have a .ORIG x3000 at the start).

The subroutine MODULO takes its input arguments from registers R0 and R1; it returns the result in register R2. Specifically, the DIVIDEND (i.e., the number N) is passed through register R0 and the DIVISOR (i.e., the number K) is passed using register R1. The output (N Modulo K) is placed in register R2. For example, if the inputs are Dividend = x000F and Divisor= x0004 then the result in R2 is x0004  (since 15 Modulo 4 = 3).

For this assignment, the “main” program will simply read the numbers N (Dividend) and K (the divisor) from memory locations x3100 and x3101 respectively (i.e., the dividend is stored at x3100 and divisor is stored at x3101) and call the MODULO subroutine. It then stores the result (N Modulo K) in memory location x3102. The main program will call your subroutine using the caller-save convention, i.e. before calling MODULO you must store R1 and R0 to predefined memory locations called SaveR0 and SaveR1 before calling your subroutine. You can define SaveR0 and SaveR1 as constants as in figure 9.6(page 227-228) of the book. Upon returning from the subroutine you will retrieve those values from the corresponding memory locations so that at the end of the program, R0 and R1 still retain the values in them before the subroutine was called.

Suppose the Dividend, Divisor and Result are instead stored at address x5100, x5101 and x5102 respectively – will your program still work correctly ?

Suppose you are not given the addresses of Dividend, Divisor, and Result but are only given that there are locations in memory with these labels. Would you need to change your code ?

Hint: Division can be implemented by repeated subtraction. Keep subtracting until number gets smaller than K (the divisor).
For an example on how to write subroutines, you can refer to the examples you did in class this is now posted on the lecture notes webpage.

·         Assume both the input values are positive

·         Run your program on the LC3 simulator for the following two sets of inputs:

§  (a) The value stored at x3100 is x0010 and value stored at x3101 is x0004

§  (b) The value stored at x3100 is x0BCD and value stored at x3101 is x00AB