COMP2300
Tutorial / Laboratory 06 -
PeANUt Assembler
Semester 1, 2008
Week 7 (07 - 11 April)
Note that for this session, there is a submitable laboratory exercise
which is due by 10 am Tuesday 15 April, which will contribute up to 1%
of your assessment (in the Tute/Lab mark). While there are no formal
Preparation Exercises, it is particularly important that
you revise arrays and functions in assembly language, as this will
be a busy session!
Objectives
There are several objectives in this session:
- To become familiar with the PeANUt assembly language,
and how to translate control structures, arrays and functions
into assembler.
- To use image files made of several source files, and
using the command line assemble and join
commands to efficiently create them.
- To learn the general debugging technique of break points.
- To learn how functions are implemented on the assembly language
level, and how the stack is used in a function call.
Tutorial Exercises
- Allocate space in PeANUt assembly language for the following:
float x; int a[4];
- Translate the following C code into assembler:
#define N 4
int i,
x,
a[N];
i = 0;
x = 0;
while (i < N) {
if (a[i] > 0) {
x += a[i];
} else {
x -= a[i];
} // if
i++;
} // while
-
Consider the following C code fragment:
int exp(int x) {
int v = 1;
while (x > 0) {
v = v * 2;
x--;
} // while
return (v);
} // exp()
...
int twox;
twox = exp(3);
Assuming the procedure call convention as defined in lectures:
- Translate the call twox = exp(3); into assembler.
- Define stack offsets for the parameters, return value
and local variable of exp().
- Translate the function exp() into assembler.
Laboratory Exercises
It may be useful to print out these pages if possible, as the
instructions on the use of the PeANUt command line utilities may be
useful for Assignment 2. Also, it may be help you do the exercise more
efficiently.
Preliminaries
- In your comp2300 directory, create a new sub-directory
called lab6.
- Copy the files /dept/dcs/comp2300/public/lab6/* into
your lab6 directory.
- Start up a terminal window and cd to your lab6
directory. Then start up the PeANUt simulator.
PeANUt via the Command Line
The
assemble and
join commands may be used to assemble
and link PeANUt assembly language programs, as described below. It is
also possible to execute PeANUt image files (whether produced by
assembler or mli files) from the command line, e.g. from last week's lab:
which is like executing a C program from the command line. To debug, use:
which prints out a (decimal-oriented)
trace of the execution of each instruction.
The command:
can be used to produce the image file from the command line.
To assemble and link and a single stand-alone assembly language program, such as
lab6a.ass, use the compound command:
assemble lab6a.ass ; join lab6a.rel
The
assemble command produces a
relocatable file
lab6a.rel, and a listings file
lab6a.lst. The
join command takes the relocatable file and produces the
image file lab6a.img. You will find that using the
above command much more efficient than performing the same process via
the PeANUt simulator (especially since you can use the `UpArrow' key to
re-execute previously typed commands).
In the assembly language version of the program lab6a.ass,
the address of msg and main
(see the listing
file lab6a.lst) will be generated during the assembly
process. Look at lab6a.lst and determine which addresses these
labels are at.
Documenting your assembler program with equivalent code in a
higher level language (in this case C) makes it easier to read.
Repetition and Indexing
The
index register, denoted by
XR, may be used to access
array elements. In program with a simple loop which scans across the
elements of an array, we can often use
XR to implement the loop
index. You are to complete
lab6a.ass by manipulating the the
index register and using
indexed mode addressing. i.e. if
XR holds the value of the index
i, then
load
*msg will load into
AC the contents of
mem[message+XR]. Note also that
XR may be incremented using the
incxr instruction
(see the table in PeANUt section 4.2.1.2).
* Add the missing instructions to lab6a.ass, and test
it. When you have it working, comment out the short message and
uncomment the long one. Show your program printing the long message to
your tutor.
The InOut module
Inspect the source code of
InOut.ass through the hyperlink in
lecture
P8. This module contains several macros and some functions for
printing and reading integers. Some macros should be familiar to you
from the lectures. Inspect the source to see what functions are
available, and note what C
stdio library call these correspond
to (for example,
ReadCard(&n) = scanf("%u", &n); the
"%u" means you can't use
ReadCard() to read a negative
integer!). The corresponding relocatable file
InOut.rel is in
/dept/dcs/comp2300/lib.
Functions with Return Values
In this part you are required to complete the code in the main program
findmax.ass (step 1 below) and the function max.ass
(steps 8 to 11 below). In the main program you have to complete the
function call (using the procedure call convention as presented
in the lectures), while in module max.ass you have to complete
the function Max following the C comments.
- Load the program findmax.ass into your text editor and add
the code for the call to Max. Be sure to keep to the
procedure call convention as discussed in the lectures.
- Assemble max.ass and findmax.ass into relocatable
files first (see below), then - using the join command -
link them (again, see the PeANUt Specifications, chapter 4
and appendix F) with the InOut.rel module into one image
file max.img. You have to use command line (shell)
commands for this (using the graphical PeANUt tool does not
work):
assemble max.ass
assemble findmax.ass
join max.rel findmax.rel InOut.rel
It might seem more logical to link findmax.rel first (the
resulting executable will then be called findmax.img).
However, linking max.rel first makes the debugging of
Max() easier, since its code will be easily located at the
top of memory.
Note that the join command will find InOut.rel
using the $PeANUtLib environment variable
(which should point to /dept/dcs/comp2300/lib).
If you need InOut.rel on an external system,
you will need to copy it to that system.
- Load and run max.img in the PeANUt simulator,
entering a line with a single number (e.g. 42), followed by 999
(the sentinel value).
- Set a break point at call 0 (call Max)
instruction.
This can be done by selecting the instruction in the
Main Memory View and using the right mouse button on
that view's menu to select Breakpoint Here. Now the
Breakpoints window will be displayed, with the break
point set on execute mode at this address.
- Try another run, this time with a smaller number, 7. Examine
the stack at the break point and check to see that the parameters
have been pushed on correctly. Now resume execution.
- If you have linked your files in the correct order (as above),
and done the call properly, a 3 should always be output
at this point. Try to explain why this occurs.
Hint: run the program again, (single) stepping
execution from the Break Point, and note the contents of
the stack and memory location for mxn at each step.
- * When you are satisfied with this, check your work with your
tutor.
- Now load the file max.ass into your text editor.
- What stack displacements should be given to
the input parameters and return value?
- Add the (concise) macros (read the PeANUt Specifications,
page 17 if you don't know what a concise macro is) to define the
stack displacements for the function's parameters and return
value.
- Add the code in the body of Max(). Use only one ret
instruction at the end of the function.
- Re-assemble, link and load max.img.
In general, you must go through all 3 steps
(assemble, join and Load IMG) to
propagate any change to a source file through to the PeANUt
machine.
- Repeat step 3 with an input of a single number 42 (followed by
the sentinel 999). Set break points at the beginning and end of
the function and check that the expected values appear at the
slots corresponding to each parameter and return value.
Note also the Memory View tracing on the SP.
- Repeat step 3 with an input of two numbers in ascending order.
- Execute max.img again with an input of two numbers in
descending order, e.g. 42 40. If this works, try a line of
several numbers in random order as a final test. Again, set
break points if needed (you may need to use the
Breakpoints window to clear the previously set break
points first).
- * Demonstrate your working program to you tutor.
- When you are satisfied that you have your program
working, test it for correctness with the command:
previewAutoMark lab6 max.ass
When satisfied, submit it using the submit command:
submit comp2300 lab6 max.ass
submit comp6300 lab6 max.ass
This is due by 10 am Tuesday April 15 (the deadline is strict)
and will contribute up to 1% of your Tute/Lab mark.
Last but not Least:
We would like to encourage you to finish ALL labs and tutes
exercises before or during the semester break - especially if you so
far have mainly worked on the assignment. Assignment 2 will require
you to program in PeANUt assembly language, and having done all
labs will help you a lot when working on the second assignment.
If you have questions about any of the home works or labs please ask
your tutor now!
Last modified: 1/05/2008, 12:49