Spring 2002 CSCI 255 Lab 10

This lab is scheduled for 23 & 24 April.

Goals and Methods

This week we'll look at:

Getting started

Begin by executing the following commands to create a directory csci/255/lab10 and copy one file into it.

The copied file is an LC-2 program that is missing a single routine. You'll write that routine.

The program

The complete program is based on an age-old problem called the Syracuse problem. All your program really needs to do is compute the Syracuse function:

int SYR(int x) {
  if (x & 1)
    return 3*x + 1 ;
    return x/2 ;

In most computers, it's not hard to computer x/2. They have divide or shift right instructions. The LC-2 has neither. However, we've provided a function DIV2 that does this for you. All you have to do is call it.

When you run the complete program, it will prompt you for a four hexadecimal-digit number. It will then call your Syracuse function (SYR) over and over, first with your input number and thereafter with the result of the last call of the Syracuse function, until the Syracuse function returns a one.

If you write your SYR correctly, a run will look something like this:

Enter a four hexadecimal-digit number: 1c71
  Syracuse test:  1C71
  Syracuse test:  5554
  Syracuse test:  2AAA
  Syracuse test:  1555
  Syracuse test:  4000
  Syracuse test:  2000
  Syracuse test:  1000
  Syracuse test:  0800
  Syracuse test:  0400
  Syracuse test:  0200
  Syracuse test:  0100
  Syracuse test:  0080
  Syracuse test:  0040
  Syracuse test:  0020
  Syracuse test:  0010
  Syracuse test:  0008
  Syracuse test:  0004
  Syracuse test:  0002
  Syracuse test:  0001

It can take a while to get to 1. If your 3*X+1 overflows, you may never get there. By the way, the 3*X+1 will always overflow when X is greater than or equal to 21845 (0x5555).

Rules of engagement

Your program must implement SYR and call DIV2 using the calling register conventions described in the 22 April lecture.

R0Caller must save
R1Caller must save
R2Caller must save
R3Callee must save
R4Callee must save
R5Global pointer
R6Frame pointer
R7Return address

All the code you add to lab10.asm will be at the very end of the file. Do not change any of the code in the first 114 lines of this file.

Going home

Don't delete your lab10.asm.