CS-220, Sections 50-51
Laboratory Exercise # 3
2-16-06, Lab Report Due: 2-23-06


Part 1.      

This part of the lab exercise consists of creating, assembling, linking, and running
under control of the 16-bit DEBUG utility, an IBM-PC assembly language program 
whose specifications are given below. In the process you will perform several tasks 
that will familiarize you with using DEBUG.

Write an assembly language program using conventional simple segment definitions 
that does the following:

(I) Define a 32-word long stack segment

(II) Define a data segment that will contain a one byte character named 'char' and 
initialize it to 'B'.
  
(III) Define a code segment that contains a single procedure called main with 80x86 
assembly language instructions that will do the following:

   (A) Move the character into the lower byte of DX register.

   (B) Move the immediate value 2 into the high byte of AX register.

   (C) Issue an interrupt 21h instruction (INT 21h).

   (D) Return to DOS by loading a 4C00h into AX register and executing an INT 21h 
       instruction.


STEPS TO BE PERFORMED:

(1) Start with your solution to Laboratory #1 as a template.  Use the EDIT program 
(or some other editor) to enter and save the program on your diskette. Use another 
name, so that you do not overwrite LAB1.ASM.

(2) Assemble your source program using the ML Assembler/Linker as in Lab 2. You may 
do this either from a DOS Command window or from Visual Studio .NET using the "Build 
16-bit MASM" tool you installed last time (you may have to install it again). You 
should specify that a .LST file be produced. If the program has errors, identify 
them by looking at the .LST file and return to the editor to make the appropriate
corrections. Repeat this procedure until your program assembles without errors.

(3) Run your program from the DOS prompt. The character 'B' should appear on the 
screen.

(4) Either from a DOS command window or from Visual Studio .NET using the "Debug 16-bit 
MASM" tool you installed last time (you may have to install it again, invoke the DEBUG 
program with your executable program (.EXE file) and perform the following steps:

   (a) Unassemble the program;

   (b) Run the program starting at location 0 in the code segment with a break point 
       set at offset 5;

   (c) Display the first 128 bytes of the data segment and find the 'B' character;
       What is the offset part of its address?

   (d) Trace execution of the next two instructions;

   (e) Run the program from there with a breakpoint set at the address of the last 
       INT 21h instruction (use the program's .LST file to determine that address;

   (f) Change the character to be displayed to an 'E' and examine the appropriate memory 
       locations to verify that it was changed. (This should be done from your DEBUG 
       session);

   (g) Reset the instruction pointer register to 5 and examine the register to verify 
       that it was changed;

   (h) Run the program with a break point set at the last INT 21h instruction. What
       happened?

   (i) Using the debugger's assembler (A-command), enter the following code starting at 
       offset 200H in the code segment:

              MOV AX,4395
              ADD AX,21E6
              MOV [210],AX

   (j) Unassemble this new program fragment;

   (k) Run the new program (starting at location 200H) with a breakpoint set after its 
       last instruction; What is now stored in AX register? What are the values of the 
       following flags: Zero, Carry, Sign,Overflow, Parity? Explain how these values came 
       about.

   (l) Display the appropriate two bytes of memory to see if the result was stored;

   (m) We wish to subtract two bytes of data that have been placed in memory. Enter the 
       values 37H and 3AH at locations 400H and 401H in the data segment and enter and 
       assemble the following code beginning location 300H in the code segment:

            MOV AL,[400]
            SUB AL,[401]
            MOV [402],AL

       Run this code fragment with a breakpoint set at the appropriate location (so the 
       computer doesn't go into never-never land). Display the contents of the memory 
       location that contains the result of the subtraction. Verify that the result and 
       the flags are correct.

   (n) Exit the debugger.

You should turn in a listing of the assembled program (.LST file), and a printed copy of 
your debugging session.  Any results or verifications should be highlighted, and a brief, 
written interpretation of what has occured after each DEBUG command should be given on the 
printed copy.

The printed copy of your DEBUG session can be produced by starting Wordpad or Notepad in 
a second window. Copy and Paste pieces of your DOS session into the Wordpad or Notepad 
window, as you work. When finished you should save it (on your diskette) and print a copy 
for the instructor.

[If your DOS Command window does not have a toolbar with 'Copy' and 'Paste' icons, you 
can click the 'System Menu Icon' in the upper left corner of the DOS window title bar. 
That will produce a popup menu with an 'Edit' item. When that is selected, another popup 
menu with the items 'Mark', 'Copy', and 'Paste' will appear. Select 'Mark' to be able to 
highlight text in the DOS Window with the mouse. After the text that you want is 
highlighted, Select 'Copy' (or hit <Enter>) to copy the highlighted text to the Windows 
Clipboard. Then, in the Wordpad or Notepad window do the pasting as usual.] 
      
Turn in the listing and annotated copy of your DEBUG session. Clip them together so they 
don't get separated.


Part 2.

We wish to compute the value of y in the following equation:

y = a*x + (b + x)/c

The values of a, b, c, x, and y are all signed 16-bit integers.

Write an Intel 80x86 assembly language that will define variables (and allocate the 
appropriate amount of memory for) a, b, c, x, and y. The variables a, b, c, and x should 
be given initial values of: 104, -39, 7, and 99, respectively. y should be uninitialized. 
The program should compute and store the value of y. Run the program from DEBUG and copy, 
paste, and print out the part of memory that contains the variables and the results. 
Verify that the result is correct with a hand calculation.

Make up and enter new values of a,b,c, and x, and run the program again. verify with a 
hand calculation that the results computed by your program are correct.


Part 3.

In a certain computer graphics color system, intensities of red, green, and blue for a 
pixel on the screen are stored in a "packed" 16-bit form using the following scheme:

Red: the most significant 5 bits of the 16-bit intensity
Green: The middle 6 bits of the 16-bit intensity
Blue: The least significant 5 bits of the 16-bit intensity

The following shows the scheme:

 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0   Bit numbers
 ------------------------------------------------
| R  R  R  R  R  G  G  G  G  G  G  B  B  B  B  B |
 ------------------------------------------------

Add code and data to the program in Part 2 that will define two 16-bit intensity variables 
(called I1 and I2) and two sets of Red, Green, and Blue values (called R1,G1,B1,R2,G2,B2). 
Each of these should be a byte. The initial value of I1 should be 42,689. The initial 
values of R2, G2, and B2 should be 27, 58, and 13, respectively. The rest of the variables 
should not be initialized. The program should then do the following:

1. Extract the red, green and blue values from I1 and store them in R1, G1, and B1.

2. Compute the packed intensity that corresponds to the R2,G2,B2 values and store the 
result in I2.

Change the original data values (I1, R2, G2, B2), and run the program again. Do this for 
three sets of data and verify with hand calculations that the results are correct.

(HINT: You will need to use bit shifting and OR and/or AND instructions with the appropriate 
masks.)

Run your program from DEBUG and, once again, print out the parts of memory that conatain the 
data and the results.


In addition to printed copies of the DEBUG session with results and data highlighted and 
labeled, you should turn in a readable, well-commented, error-free assembled listing (.LST 
file) of each program. Also turn in a hand calculation of all results to confirm that the 
programs really work correctly.