Modules and Linking

Purpose

To give you practice in program design:

Due dates

You must hand in answers to the questions at the beginning of your laboratory session on Thursday, October 9. This part of the assignment is worth 15 points.

You must hand in the rationale for your decomposition, and your interface specifications, at the beginning of your laboratory session on Tuesday, October 14, and demonstrate your program to your TA during that period. This part of the assignment is worth 20 points.

Background

In previous assignments, you have been given a decomposition of the problem, with an appropriate interface for each routine, and been asked to code the routines. Here we will specify a behavior to be implemented, and ask you to determine how to decompose that implementation. You will be required to give a rationale for your decomposition, and to specify the interfaces of all modules. You must also choose the tools and testing procedure to use.

There is no ``right'' answer to these questions; any strategy resulting in a valid implementation is correct. Different strategies will have different advantages and disadvantages, and will be more or less complex to carry out. You should discuss various decompositions before beginning the actual coding, trying to see how you can compartmentalize the process to make debugging easier. You should also think about how best to use the simulator, monitor, and MooseLoad to reduce the testing and debugging time.

A 2's-complement calculator

The behavior to be implemented is a simple, four-function calculator for 8-bit, 2's-complement numbers. Its keyboard is the HP keyboard, and its display is the LEDs connected to the MB5's digital output. You are given a C-coded input routine to read from the HP's keyboard. Click here for the interface specification.

Only the keys 0-9, a-f (upper and lower case), +, -, *, /, and = are recognized by the program. There are two 8-bit, 2's-complement registers called acc and buf. The content of either of these registers can be displayed, but the display is simply a ``snapshot'' of that register at a given time.

A state machine provides a way to model the behavior of the calculator program as a function of time: At any given instant, the machine is in a given state. It is then given an input, which causes it to perform some action and then go to some state. This cycle continues, with the machine accepting a sequence of inputs. Each input results in an action and a state transition. Some of the actions involve writing the value of either acc or buf to the MB5's digital output, others involve carrying out 2's-complement arithmetic on one or both of the registers.

The machine has five states:

init:
The initial state, in which the value of acc is irrelevant and the value of buf is 0. 0 has been displayed.
initd:
The state in which the value of acc is irrelevant and buf holds an input value.
oper:
The state in which the value of acc is the previously-entered value, an operator has been entered, and the value of buf is 0.
operd:
The state in which the value of acc is the previously-entered value, an operator has been entered, and buf holds an input value.
equal:
The state in which the value of acc is the previous value, the previous computation has been terminated by =, and the value of buf is 0.
There are three inputs:
digit:
The input character was one of 0-9, a-f (upper and lower case). Additional information is the hexadecimal value represented by the character.
operator:
The input character was one of +, -, *, /. Additional information is the specific operator represented by the character.
equal:
The input character was =. There is no additional information.
There are four actions:
shift:
Shift the buffer left by 4 bit positions and insert the value of the input digit in the least significant 4 bit positions. Display buf.
store:
Move the content of buf to acc. Clear buf.
rator:
Save the input operator, forgetting any previously-saved operator.
doopn:
Set the content of acc to the value of acc op buf, where op is the saved operator. Clear buf. Display acc.
Here is the definition of the state machine:

StateInputs
digitoperatorequal
init shift / initd -- / init -- / init
initd shift / initd store,rator / oper store / equal
oper shift / operd rator / oper -- / equal
operd shift / operd doopn,rator / oper doopn / equal
equal shift / initd rator / oper -- / equal

Each entry of the table has the form ``action(s) / next state''; -- means ``no action''. Thus, for example, when the machine is in state operd and the input is operator, it

  1. sets the content of acc to the value of acc op buf, where op is the saved operator,
  2. clears buf,
  3. displays acc
  4. saves the input operator, forgetting any previously-saved operator, and
  5. goes to state oper.

The MB5 digital output

The MC68000 uses memory-mapped I/O (see page 32 of the text): peripheral devices are addressed by the CPU in the same manner as memory is addressed. Thus the digital output device, an 8-bit driver for the 8 LEDs at the bottom of the MB5, can be controlled by moving a byte to location $B00001. Each 0-bit will turn the corresponding LED on, and each 1-bit will turn the corresponding LED off. The LEDs therefore display the complement of the transmitted byte.

Although the digital output device looks like a memory location, it really isn't one. However, your code may treat it like a memory location, so an operation like

addi.b  #$01,$B00001
does what you might expect. Just be careful that you access this location with byte instructions. A word or long will cause an error since the LEDs are mapped to an odd memory address.

The digital output device can be used in conjunction with MooseLoad for debugging a program, using a technique analogous to that of inserting print statements into a C program. Suppose that you have linked the program for MooseLoad, and downloaded it via TeraTerm (i.e. you are not using the XRAY monitor). Suppose further that you put the following instructions into your program:

move.b  #$fe,$B00001
trap    #5
The first instruction will cause the LEDs to display a binary ``1'', and the second will enter the MooseLoad debug mode. If each such sequence of instructions that you insert sends a different byte to the digital output, you can tell exactly where you are in your program. (The trap #5 instruction causes your program to jump to MooseLoad's debug mode.)

Once in debug mode, MooseLoad allows you to display and modify registers, return to your program, or go to the main MooseLoad menu. From the main menu, you can display and modify memory, restart your program at the beginning, or return to debug mode and then go back to the instruction following the trap.

You will need to consult the map file and program listings produced when you built your program in order to find out where things are in memory. This involves hexadecimal arithmetic, and an understanding of the listings themselves.

While MooseLoad may appear primitive, its capabilities are quite representative of the those found in any monitor or single board development system. Often it is easier to use these simple capabilities than it is to live with the restrictions and complexity of the XRAY monitor.

Task

Click here for a zip file containing the C-coded character input module and its interface. You must use this module unchanged to read from the HP's keyboard. It will deliver characters only after you have typed Enter. Thus you can send one character at a time to your program by typing Enter after each character, or you can type a number of characters on the same line.

You cannot use backspace characters to correct typing mistakes. If you study the state table given above, however, you will see that it really isn't necessary -- if you make a mistake, simply type the correct characters. Numbers can be corrected by typing more digits, because only the last two digits typed are held in buf. Operators can be corrected by typing another operator, because only the last operator typed is remembered.

MooseLoad uses the same strategy for dealing with numbers: Although it won't accept backspaces, it only keeps the last 8 digits you type.

Implement the 2's-complement calculator calculator using some combination of C and assembly language modules, in conjunction with the C-coded character input module. Your implementation must be made up of at least two source files in addition to inchar.c.

You will need to create a Makefile to build your program. Look back at the Makefiles provided to you for previous assignments. They all follow the same pattern, and you should be able to select the components you need without knowing much about how make works. If you are having problems, or would like to know more about make, click here.

You have been introduced to several ways of building and testing programs on the laboratory machines. Choose one that is appropriate and refer back to an earlier assignment for detailed instructions if necessary.

Questions

  1. (1 point each) What value should be displayed in the MB5's LEDs after each of the following lines? (Assume that the entire line has been typed, followed by Enter. Further assume that the program is in state init at the beginning of the line. Use 1 to indicate ``LED on'' and 0 to indicate ``LED off''.)
    1. 1+2+3
    2. 2+3*4=
    3. 2f3+4=
    4. 7*-2=
    5. 1+2=*3=
    6. 1+2=4*3=

  2. (5 points) Consider Example 6.3, on page 187 of the text. How might one use a jump table like the one described there in implementing the calculator?

  3. (4 points) Click here to see a Makefile from a previous assignment. LNKFLAGS specifies the flags that the linker will use when linking the program. Notice that the flags required to use the XRAY monitor are different than the flags required to use MooseLoad. Briefly explain the differences. (Chapter 10 of the on-line Assembler/Linker/Librarian User's Guide and Reference explains the meanings of the linker flags.)

Demonstration

You must demonstrate your module to your TA on one of the machines in the Microlab.
Instructor Revision 1.38 (2007/08/27 00:15:44)