Download and extract the MP0 files
- If you do not already have a tool to extract zip files,
try downloading a tool like WinZip
- Extract the files to your network drive (P:)
Start a COMMAND shell and locate your files.
- If your prefer the DOS command environment,
- Click: Start -> Run.. type type:
command
- Switch to your drive by typing
P:
- Switch to the mp0 directory by typing
cd mp0
- If your prefer the default UNIX shell environment,
- Click on the the Cygwin icon on the desktop
- If no such icon is on your desktop,
Install Cygwin
- Switch to your drive by typing
cd P:
- Switch to the mp0 directory by typing
cd mp0
Edit the program
with your favorite text editor.
- If you are working at home, you'll need to install your own
text editor.
- EMACS and VIM (VI Improved) are also good editors for assembly.
- The ConTEXT editor,
for example, is a decent Windows-based editor
that provides syntax highlighting and shows line numbers.
- Avoid using Notepad or QuickEdit.
- Scroll through the program, but don't make any changes yet.
- The code is
broken into several sections to illustrate the various components
in an assembly program. This is certainly not the only way to
organize an assembly program, but it gives a good starting point.
Again, you do not need to understand precisely what the
statements are doing at this point, but notice the overall
structure of the program. This will be a good reference for
future machine problems.
- Section 1 defines several constants to be used throughout
the program. Each constant has a value that is associated with a
particular name using the EQU statement.
- Section 2 declares external procedures (meaning code that
has already been written but is in a different file) that will be
used in this program. External procedures are declared with the
EXTERN statement.
- Section 3 defines a special required structure called a
stack segment. You will discover why a stack is necessary in the
future. The stack segment is declared using the SEGMENT stkseg
STACK statement.
- Section 4 declares the beginning of the code segment - that
part of the file which contains the program instructions. The
code segment is declared using the SEGMENT code
statement.
- Section 5 initializes several variables to be used. For
this program, these variables are just sequences of characters
called strings. Variables can be defined using the DB
statement. It is important that variables are not defined in a
part of your program that is executed.
- Section 6 contains the code for the program initialization:
setting up segment registers for the default and stack segments.
Program execution begins at the ..start: label. The program
execution goes immediately into the MAIN procedure (next section)
after completing this section.
- Section 7 contains the code for the main procedure.
From the COMMAND window,
Assemble and link MP0 by typing make. This command
will read Makefile and invoke the following commands:
nasm -g -f obj -o mp0.obj mp0.asm -l mp0.lst
tlink /c /v mp0.obj, mp0.exe, mp0.map, lib306.lib
At this point, you should have just created the executable program
called MP0.EXE.
Run the program by just typing its name, MP0
Try giving the program different input.
Remember: this program is looking for upper-case letters!
Now examine your program by running the debugging tool Turbo Debugger (TD).
TD MP0
Turbo Debugger will read your MP0.EXE program and allow you to step
and trace through the program as it executes.
- If the pulldown menus do not respond to clicking on them,
click on the icon at the top-left of the TD window.
Select properties and unselect the quickedit and
insert modes. Select the option to apply the changes to
the shortcut that opened the window. Close this window and
run TD MP0 again from the command line.
- If the Regs window is not already open, open it.
From TD's View pulldown menu, select the options
to view Registers. Resize
and adjust the locations of the windows on the screen as necessary.
- Click on the function key F8 to execute
the first instruction of your program.
Notice that register AX has changed value.
The first instruction
of the program (MOV AX, CS) puts the value of CS into AX.
Also notice that register IP, the instruction pointer, has
changed value. It always specifies the offset of the next
instruction to be executed. The instruction displayed is the
next one to be executed.
- Click F8 again to execute the second line of code.
Notice that the DS register changes.
- If the Dump window is not already open, open it.
From TD's View pulldown menu, select the options
to view Dump.
Memory addresses are shown on the left in segment:offset format.
Check that the memory segment is DS.
If not, you can right click in the Dump window and click on
Goto.... Type ds:0 and press Enter to display
the start of the data segment.
Each data byte appears in hexadecimal. On the far right side
of the screen are the characters whose ASCII codes are given
by the bytes.
The ASCII contents of the memory should look familiar.
- Click on F8 a few more times to step through your code.
Stop stepping when you reach the input prompt.
- From the program's grade prompt, select a letter grade. Use F8
to step through the rest of the program. You toggle between
the debugger screen and program output by pressing Alt-F5.
- Once the program has terminated; Select Program reset from
TD's Run menu. Run the program again a few times with
different input until you understand how the program is executing.
- Navigate to the View menu, and select CPU. Resize and
adjust the locations of the windows on the screen as necessary.
- Again select Program reset from TD's Run menu.
Right click in the code area of the CPU window, and click on
the Mixed option until it reads No. Click outside the
popup menu to make it disappear.
Now, In TD's CPU window, you will see the machine code
for the same program. In first column, you will
see where each line of code is located in memory
(in segment:offset format). In the second column, you
will see the program bytes in hex. In the third column,
you will see the human-readable opcodes of the program.
Notice that text labels have been replaced
with numeric values (pointers and constants) by the assembler
and linker.
- Again select program reset from TD's Run menu.
Run the program again a few more times with different input.
- To speed up the debugging process, TD allows the programmer to
execute large sections of code between break-points.
Click on the line at offset=00FA and press F2 to set a
breakpoint in your program at the first compare statement.
- Restart your program, then hit F9 to
run through your code until the breakpoint.
- When prompted for a grade, enter B then hit return.
You should continue execution of the program.
- Record the value in register AL.
- In the Dump window, change the value of the byte at offset=0 to
44 (hex). Continue running the program until it terminates.
Notice that the output message is grossly incorrect.
- In the space below, explain what the program is doing.
- Quit TD and return to the COMMAND prompt.
Now return to your text editor to modify mp0.asm.
You need to make the following changes:
- Put your name and the current date at the
beginning of the program
- Add a feature to the program
that prints a message of your choice
when the grade C is selected.
- Assemble and test your program with all possible inputs.
Debug with TD
as necessary until the program works as expected.
Now return to your text editor and add a line to mp0.asm.
- Add a new variable to your program called mystery that
is initialized with the hex values of 0FEh,0C0h.
(This is NASM's method of entering hex values).
Declare this variable in your code at the location
immediately after the line where lib306's kbdine
function is called."
- Re-Assemble your code and run TD.
- Run your program a few more times and observe what happens.
Try Switching between the Module (source) window and
CPU (disassembly) windows.
- Browse the CPU window
record what addresses the mystery bytes
were stored in.
- In the space below, explain how the program behaves differently and why.
- Finally, Move this variable declaration to where it belongs in the code.