Homework 1

IBM PC Assembly Language Program - Assignment 1

The following exercise covers the basic steps required to complete course programming assignments. These steps are 1) entering, 2) assembling,  and 3) debugging a program. A sample program to enter and detailed steps follow.

1.    Enter program

Enter the below program using Notepad or any other ASCII text editor. Add your name on the first line in the title. When saving the file, use HW1.ASM as the name. You should be able to copy and paste the following into NotePad or other word processor as an ASCII file (be certain that the extension is ASM and not TXT if saving in NotePad).
Hw1.Asm
Title    YOUR NAME     Home Work 1 - Evaluate U = (R+6-T)*S

DATA     Segment                    ;;Data Segment 
 R        DW           100          ;; R = 100
 S        DW             8          ;; S = 8 
 T        DW             1          ;; T = 1 
 U        DW             ?          ;; U is uninitialized
DATA     Ends                       ;; End of segment DATA

ALGORITHM Segment                   ;; Algorithm Segment 
          Assume   Cs:ALGORITHM, Ds:DATA, Ss:SSeg

HW1     Proc     Far                ;; Procedure HW1
        Mov      Bx, Seg Data       ;; Initialize DS register 
        Mov      Ds, Bx             ;; to address of data segment
        Mov      Ax, R              ;; Ax = R 
        Add      Ax, 6              ;; Ax = R+6
        Sub      Ax, T              ;; Ax = R+6-T
        Mov      Dx, 1234h          ;; Notice value of Dx after Mul below 
        Mul      S                  ;; Dx:Ax = (R+6-T)*S 
        Mov      U,  Ax             ;; U = (R+6-T)*S

        Mov      Ah, 4ch            ;; Terminate process returning
        Int      21h                ;; to parent (Debugger or DOS)
HW1     Endp                        ;; End of procedure HW1
ALGORITHM Ends                      ;; End of segment ALGORITHM
SSeg    Segment        STACK        ;;Stack segment of 254 bytes
         DB            254 dup(?)
SSeg    Ends
End      HW1        ;; End of file, start execution at HW1

2.    Assemble

 
Hw1.Lst
Microsoft (R) Macro Assembler Version 6.14.8444		    08/15/02 15:09:01
YOUR NAME     Home Work 1 - Evaluate U = (R+6-T)*S	     Page 1 - 1

		Title    YOUR NAME     Home Work 1 - Evaluate U = (R+6-T)*S

 0000			DATA     Segment                    ;;Data Segment 
 0000 0064		 R        DW           100          ;; R = 100
 0002 0008	         S        DW             8          ;; S = 8 
 0004 0001		 T        DW             1          ;; T = 1 
 0006 0000		 U        DW             ?          ;; U is uninitialized
 0008		        DATA     Ends                       ;; End of segment DATA

 0000			ALGORITHM Segment                   ;;Algorithm Segment 
			        Assume   Cs:ALGORITHM, Ds:DATA, Ss:SSeg

 0000			HW1     Proc     Far                ;; Procedure HW1
 0000  BB ---- R	        Mov      Bx, Seg DATA       ;; Initialize DS register 
 0003  8E DB		        Mov      Ds, Bx             ;; to address of data segment

 0005  A1 0000 R	        Mov      Ax, R              ;; Ax = R 
 0008  83 C0 06			Add      Ax, 6              ;; Ax = R+6
 000B  2B 06 0004 R		Sub      Ax, T              ;; Ax = R+6-T
 000F  BA 1234			Mov      Dx, 1234h          ;; Notice value of Dx after Mul below 
 0012  F7 26 0002 R		Mul      S                  ;; Dx:Ax = (R+6-T)*S 
 0016  A3 0006 R		Mov      U,  Ax             ;; U = (R+6-T)*S

 0019  B4 4C			Mov      Ah, 4ch            ;; Terminate process returning
 001B  CD 21			Int      21h                ;; to parent (Debugger or DOS)
 001D			HW1     Endp                        ;; End of procedure HW1

 001D			ALGORITHM Ends                      ;; End of segment ALGORITHM  

 0000			SSeg    Segment        STACK        ;;Stack segment of 254 bytes
 0000  00FE [			DB            254 dup(?)
        00
       ]
 00FE			SSeg    Ends

			End      HW1   
3.   Execute
Use the debugger to execute the Hw1.Exe file by entering:
debug hw1.exe

4.   Debugging

The debugger will display a command prompt of a '-'. The four commands needed are:
  1. u - Unassemble the machine code. Lists the mnemonic that correspond to the machine code. ADD AX, +06 corresponds to 83C006 machine code.
    u
    12C0:0000 BBBF12        MOV     BX,12BF
    12C0:0003 8EDB          MOV     DS,BX
    12C0:0005 A10000        MOV     AX,[0000]
    12C0:0008 83C006        ADD     AX,+06
    12C0:000B 2B060400      SUB     AX,[0004]
    12C0:000F BA3412        MOV     DX,1234
    12C0:0012 F7260200      MUL     WORD PTR [0002]
    12C0:0016 A30600        MOV     [0006],AX
    12C0:0019 B44C          MOV     AH,4C
    12C0:001B CD21          INT     21
    12C0:001D C3            RET
    12C0:001E BFB2E8        MOV     DI,E8B2
  2. r - Register view. Displays the registers and next instruction to execute.
    r
    AX=0000  BX=0000  CX=002D  DX=0000  SP=00FE  BP=0000  SI=0000  DI=000
    DS=12AF  ES=12AF  SS=12C2  CS=12C0  IP=0000   NV UP EI PL NZ NA PO NC
    12C0:0000 BBBF12        MOV     BX,12BF
  3. p - Proceed by executing the next one or more instructions. The registers and next instruction to execute are displayed.
    p2
    
    AX=0000  BX=12BF  CX=002D  DX=0000  SP=00FE  BP=0000  SI=0000  DI=0000
    DS=12AF  ES=12AF  SS=12C2  CS=12C0  IP=0003   NV UP EI PL NZ NA PO NC
    12C0:0003 8EDB          MOV     DS,BX
    
    AX=0000  BX=12BF  CX=002D  DX=0000  SP=00FE  BP=0000  SI=0000  DI=0000
    DS=12BF  ES=12AF  SS=12C2  CS=12C0  IP=0005   NV UP EI PL NZ NA PO NC
    12C0:0005 A10000        MOV     AX,[0000]                          DS:0000=0064
  4. d - Dump memory. The d ds:0 command dumps memory pointed to by the DS register. Memory containing the program data variables has been dumped, variable S has been located and underlined in the following example. 
    d ds:0
                       S
    12BF:0000  64 00 08 00 01 00 00 00-00 00 00 00 00 00 00 00   d...............
    12BF:0010  BB BF 12 8E DB A1 00 00-83 C0 06 2B 06 04 00 BA   ...........+....
    12BF:0020  34 12 F7 26 02 00 A3 06-00 B4 4C CD 21 C3 BF B2   4..&......L.!...
    12BF:0030  E8 B4 2A CD 21 98 51 52-8B F0 D1 E6 03 F0 8B CE   ..*.!.QR........
    12BF:0040  A1 12 83 B6 03 57 E8 A9-25 5F 03 F1 B9 03 00 F3   .....W..%_......
    12BF:0050  A4 B0 00 AA 5A 59 C3 8A-D0 32 F6 B8 23 65 CD 21   ....ZY...2..#e.!
    12BF:0060  C3 80 3E 27 D3 00 75 72-8B 36 D7 E2 32 D2 AC 0A   ..>'..ur.6..2...
    12BF:0070  C0 74 26 3C 2A 75 0F F6-C2 02 74 05 80 CA 01 EB   .t&<*u....t.....

5.    The IP register points to the NEXT instruction to be executed by the CPU at location cs:0005 in the Algorithm. To execute one instruction at a time and witness the effects on registers and memory press:

p
Execute instructions by pressing p until the instruction at address 0008 is displayed as below:
  12C0:0008  83C006      Add Ax, 6
    Address  Instruction Mnemonic    

6.    Locating variables in the data segment

Variables correspond to a memory location, variable S corresponds to data segment location 0002 and 0003, since it is a word it occupies 2 bytes. The location of variable S in the data segment can be determined by examining the Hw1.Lst listing which contains the following information.

Location   Value   Variable
0002       0008        S         DW   8       ;; S = 8
Use the debugger dump command, to examine variable S at location 0002-0003 count across the DATA pane row, starting at 0000 and counting to location 0002. Note that variable S = 0008 but is stored as 08 00 in memory backward as low high byte.:
d ds:0
 
Contents  12BF:0000  17   00   08   00   01   00   00   00
Address            0000 0001 0002 0003 0004 0005 0006 0007
Variable                         S             
7.   Continue to execute each instruction (by pressing p) until the next instruction to execute is at address 0019.
 
AX=0348  BX=12BF  CX=002D  DX=0000  SP=00FE  BP=0000  SI=0000  DI=0000
DS=12BF  ES=12AF  SS=12C2  CS=12C0  IP=0019   NV UP EI PL NZ NA PE NC
12C0:0019 B44C          MOV     AH,4C
8.   Print the debugger screen to the clipboard at this point by:

Alt PrtSc

Paste into WordPad or other word processor to print.

The program's execution effects on data has been completed and results appear in the DATA segment. You will need to locate the memory used by each variable and determine their values. The program has not executed to completion however. Up to this point the program has been executed under the control of the debugger, executing one instruction at a time. To allow the CPU to execute the remaining instructions to completion enter:

 

g - Go execute program. Starts execution at the next instruction. The following executes through the last instruction Int 21h which terminates the program.

g
Program terminated normally
 

q - Quit debugger.

q

9. TURN IN:

  1. Cover sheet with your name, date, Homework 1, Assignment 2. Staple all pages together.
  2. Printout of HW1.LST  from Step 2.
  3. Printout from Step 8 with:
  1. values of variables R-U in the data segment dump circled and identified as R-U as in Step 6.
  2. the FINAL values of variables R-U as the corresponding decimal values (remember that words are stored in reverse byte order in memory, 123416 is stored as 34 12).

Software Download and Use

The Assembler software used in Assignment 2 and the remainder of course homeworks can be downloaded and installed by the following instructions:

  1. Download the assembler MASM32v7.zip. Unzip Install.exe and execute to install. It will install to the directory \masm32.
  2. Download the Microsoft linker LNK563.exe. Execute to expand. It will create a file in the same directory as LNK563.exe named LINK.EXE.
  3. Copy LINK.EXE to \masm32\bin directory.
  4. To assemble program HW1.asm, at the Command prompt enter:
    1. path=\masm32\bin;%path%
    2. ml /Fl hw1.asm

Assembler documentation can be downloaded from this Web site.