Lab 2
Debugging a Machine Code Program
Objectives
- To gain practice debugging machine code using the LC-3 debugger.
What you should do
- Download a copy of
lab2file and save it in your directory that contains the LC-3 Simulator.
Open the file using LC3Edit (use Save As... to rename the file to
lab2.bin
).
This file contains the (buggy) machine code for a program that is
supposed to add 10 (decimal) to every integer in an array. The array should
start at location x4000 and the resulting values should be placed in an
array that starts at location x5000. The array of integers is terminated
by a word containing x0000. Your task is to find and fix the bugs in the
program.
- Using LC3Edit, create the object file
lab2.obj
. Start up the LC-3 Simulator and
open the object file.
- Put some data into memory beginning at location x4000 (these will
be the integers in your array). The data should terminate with
a word that contains the value zero.
- Put a breakpoint at the TRAP HALT instruction. Run the program. When
the program executes correctly, each of your data
values will have been increased by 10 and stored at consecutive locations
beginning at location x5000. Unfortunately, the program's not working yet.
- Now you're on your own. (Hint: there are three bugs in the
program.) Use the tools available to you - remember
these debugging features from Lab 1:
- Breakpoints A breakpoint is just what the name implies - a point
at which you break out of the currently-executing program and are brought
into the debugger. You can then check out the state of various
registers and memory locations to make sure they contain the values they
should contain at that point. If you reach a breakpoint and you find
something amiss, you know that there is a bug in your program in one of
the instructions that executed prior to the breakpoint. If on the other
hand, everything looks OK, you can narrow down your search for bugs in the
instructions that execute after the breakpoint. Branch instructions are
usually good places to put breakpoints; that allows you to stop after
each iteration of a loop, for example, and make sure the loop control
variables and other registers and variables contain the correct values.
To set a breakpoint, double-click on the little gray square to the
left of an instruction (the breakpoint takes you out of the program
before executing that instruction). To remove a breakpoint,
double-click in that same place again (instead of a gray square you will now be double-clicking on a red stop sign). To continue execution after a
breakpoint, click the Run Program button (the second button from the
left).
- Single-step Single-stepping through a program is useful when
you have isolated the bug to a section of your program, but you don't
know exactly which instruction is causing the problem. Single-stepping
allows you to execute your program one instruction at a time. After the
execution of each instruction you can examine the registers and
memory to make sure they contain what you expect them to contain. To
single-step through a program, click the step-over button (the third
button from the left on the toolbar) each time you want to execute an instruction.
(We will use the step-into button later, when we write programs that contain
subroutines.)
- Refer to "LC-3
Simulator Lab Manual" found at the authors' website for more information about using the debugger.
- If you don't find all the bugs, that's OK (try to finish this
exercise on your own time). Turn in your .bin file
lab2.bin
using
web-based turnin.