CPE 408330� Assembly Language and�Microprocessors��Chapter 4: Machine Language Coding and� the DEBUG Software Development� Program of the PC�����
[Computer Engineering Department,
Hashemite University, © 2008]
Lecture Outline
2
CPE 0408330 @ 2009
S. Abed - HU, Jordan
Lecture Outline
3
CPE 0408330 @ 2009
S. Abed - HU, Jordan
4.1 Converting Assembly Language Instructions to Machine Code
4
CPE 0408330 @ 2009
S. Abed - HU, Jordan
4.1 Converting Assembly Language Instructions to Machine Code
5
CPE 0408330 @ 2009
S. Abed - HU, Jordan
4.1 Converting Assembly Language Instructions to Machine Code
6
CPE 0408330 @ 2009
S. Abed - HU, Jordan
4.1 Converting Assembly Language Instructions to Machine Code
7
CPE 0408330 @ 2009
S. Abed - HU, Jordan
MOD field and R/M field encoding
4.1 Converting Assembly Language Instructions to Machine Code
8
CPE 0408330 @ 2009
S. Abed - HU, Jordan
Register (REG) field encoding
4.1 Converting Assembly Language Instructions to Machine Code
9
CPE 0408330 @ 2009
S. Abed - HU, Jordan
Register (REG) field encoding
4.1 Converting Assembly Language Instructions to Machine Code
10
CPE 0408330 @ 2009
S. Abed - HU, Jordan
4.1 Converting Assembly Language Instructions to Machine Code
= imm8 or imm16
S
S
S
S
s
11
CPE 0408330 @ 2009
S. Abed - HU, Jordan
4.1 Converting Assembly Language Instructions to Machine Code
Encode the instruction in machine code
MOV BL, AL
Byte 1: OPCODE = 100010 (for MOV),
D = 0 (source), W = 0 (8-bit)
REG = 000, MOD = 11, R/M = 011
MOV BL, AL = 88C316
12
CPE 0408330 @ 2009
S. Abed - HU, Jordan
MOV BL, AL
4.1 Converting Assembly Language Instructions to Machine Code
Encode the instruction in machine code
ADD AX, [SI]
OPCODE = 000000 (for ADD), D = 1 (dest.), W = 1 (16-bit)
REG = 000, MOD = 00, R/M = 100
ADD AX, [SI] = 030416
14
CPE 0408330 @ 2009
S. Abed - HU, Jordan
ADD AX, [SI]
4.1 Converting Assembly Language Instructions to Machine Code
Encode the instruction in machine code
XOR CL, [1234H]
OPCODE = 001100 (for XOR), D = 1 (dest.), W = 0 (8-bit)
REG = 001, MOD = 00, R/M = 110
BYTE 3 = 3416
BYTE 4 = 1216
XOR CL, [1234H]= 320E341216
16
CPE 0408330 @ 2009
S. Abed - HU, Jordan
XOR CL, [1234H]
4.1 Converting Assembly Language Instructions to Machine Code
Encode the instruction in machine code
ADD [BX][DI]+1234H, AX
OPCODE = 000000 (for ADD), D = 0 (source), W = 1 (16-bit)
REG = 000, MOD = 10, R/M = 001
BYTE 3 = 3416
BYTE 4 = 1216
ADD [BX][DI]+1234H, AX = 0181341216
18
CPE 0408330 @ 2009
S. Abed - HU, Jordan
ADD [BX][DI]+1234H, AX
4.1 Converting Assembly Language Instructions to Machine Code
20
CPE 0408330 @ 2009
S. Abed - HU, Jordan
4.1 Converting Assembly Language Instructions to Machine Code
Encode the instruction in machine code
MOV WORD PTR [BP][DI]+1234H, 0ABCDH
This example does not follow the general format
MOV WORD PTR [BP][DI]+1234H, 0ABCDH
= C7833412CDAB16
21
CPE 0408330 @ 2009
S. Abed - HU, Jordan
MOV WORD PTR [BP][DI]+1234H, 0ABCDH
4.1 Converting Assembly Language Instructions to Machine Code
Encode the instruction in machine code
MOV [BP][DI]+1234H, DS
This example does not follow the general format
MOV [BP][DI]+1234H, DS
= 100011001001101100110100000100102
= 8C9B341216
23
CPE 0408330 @ 2009
S. Abed - HU, Jordan
MOV [BP][DI]+1234H, DS
4.2 Encoding a Complete Program in Machine Code
25
CPE 0408330 @ 2009
S. Abed - HU, Jordan
ADD AX , ABCDh
4.2 Encoding a Complete Program in Machine Code
Encode the “block move” program in Fig. 4-6(a) and show how it would be stored in memory starting at address 20016.
MOV AX, 2000H ;LOAD AX REGISTER
MOV DS, AX ;LOAD DATA SEGMENT ADDRESS
MOV SI, 100H ;LOAD SOURCE BLOCK POINTER
MOV DI, 120H ;LOAD DESTINATION BLOCK POINTER
MOV CX, 10H ;LOAD REPEAT COUNTER
NXTPT: MOV AH, [SI] ;MOVE SOURCE BLOCK ELEMENT TO AH
MOV [DI], AH ;MOVE ELEMENT FROM AH TO DEST. BLOCK
INC SI ;INCREMENT SOURCE BLOCK POINTER
INC DI ;INCREMENT DESTINATION BLOCK POINTER
DEC CX ;DECREMENT REPEAT COUNTER
JNZ NXTPT ;JUMP TO NXTPT IF CX NOT EQUAL TO ZERO
NOP ;NO OPERATION
26
CPE 0408330 @ 2009
S. Abed - HU, Jordan
4.2 Encoding a Complete Program in Machine Code
27
CPE 0408330 @ 2009
S. Abed - HU, Jordan
4.2 Encoding a Complete Program in Machine Code
28
CPE 0408330 @ 2009
S. Abed - HU, Jordan
4.3 The PC and Its DEBUG Program
C:\DEBUG
29
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-R AX
-D DS:100
-N A:BLK.1
-F 100 11F 22
4.3 The PC and Its DEBUG Program
30
CPE 0408330 @ 2009
S. Abed - HU, Jordan
4.3 The PC and Its DEBUG Program
31
CPE 0408330 @ 2009
S. Abed - HU, Jordan
Status register (Flags) =
0000 0000 0100 0000
Interrupt flag = 1 🡺 interrupt enable
4.3 The PC and Its DEBUG Program
R [REGISTER NAME]
32
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-R AX (↵)
AX 0000
:_
:00FF (↵)
_
;This alter the content of AX
If [REGISTER NAME] is empty 🡺 display the values of all registers
4.3 The PC and Its DEBUG Program
Verify the initialized state of the 8088 by examining the contents of its registers with the Register command.
33
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-R (↵)
4.3 The PC and Its DEBUG Program
34
CPE 0408330 @ 2009
S. Abed - HU, Jordan
4.3 The PC and Its DEBUG Program
35
CPE 0408330 @ 2009
S. Abed - HU, Jordan
4.3 The PC and Its DEBUG Program
Issue commands to the DEBUG program on the PC that causes the value in BX to be modified to FF0016 and then verify that this new value is loaded into BX.
36
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-R BX (↵)
BX 0000
:FF00 (↵)
-R BX (↵)
BX FF00
:_ (↵)
_
4.3 The PC and Its DEBUG Program
Use the Register command to set the parity flag to even parity. Verify that the flag has been changed.
37
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-R F (↵)
NV UP EI PL NZ NA PO NC -PE (↵)
-R F (↵)
NV UP EI PL NZ NA PE NC - (↵)
4.4 Examining and Modifying the�Contents of Memory
38
CPE 0408330 @ 2009
S. Abed - HU, Jordan
4.4 Examining and Modifying the�Contents of Memory
The DUMP command allows us to examine the contents of a memory location or a block of consecutive memory location.
D [ADDRESS]
39
-D (↵)
-D 1342:100 (↵)
-D DS:100 (↵)
-D 100 (↵)
Command | Meaning |
- D | Display 128 bytes starting from DS:0100 |
- D 1373:200 | Display 128 bytes starting from 1373:200 |
- D 1F0 | Display 128 bytes starting from DS:1F0 |
- D 200 300 | Display memory locations from DS:200 to DS:300 |
- D CS:200 212 | Display memory locations from CS:200 to CS:212 |
4.4 Examining and Modifying the�Contents of Memory
40
CPE 0408330 @ 2009
S. Abed - HU, Jordan
Address of the first
byte of data
ASCII version of the
memory data
16 bytes of data per line,
128 bytes per dump
4.4 Examining and Modifying the�Contents of Memory
Issue a dump command to display the contents of the 32 bytes of memory located at offset 030016 through 031F16 in the current data segment.
41
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-D 300 31F (↵)
4.4 Examining and Modifying the�Contents of Memory
Use the Dump command to examine the 16 bytes of memory just below the top of the stack.
42
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-D SS:FFEE FFFD (↵)
4.4 Examining and Modifying the�Contents of Memory
E ADDRESS [LIST]
e.g.
43
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-E DS:100 (↵)
-1342:0100 FF. _ (Space bar to continue)
-1342:0100 FF. FF._
-E DS:100 (↵)
-1342:0100 FF. _ (↵) (Return to end)
-E DS:100 FF FF FF FF FF (↵)
Used to browse group of locations and enter a values for specific one
4.4 Examining and Modifying the�Contents of Memory
Start a data entry sequence by examining the contents of address DS:100 and then, without entering new data, depress the “-” key. What happen?
Entering “-” causes the display of previous byte storage location.
44
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-E DS:100 (↵)
1342:0100 FF. _
4.4 Examining and Modifying the�Contents of Memory
Enter ASCII data to the memory.
45
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-E DS:200 “ASCII” (↵)
or
-E DS:200 ‘ASCII’ (↵)
4.4 Examining and Modifying the�Contents of Memory
The FILL command fills a block of consecutive memory locations all with the same data.
F STARTING_ADDRESS ENDING_ADDRESS LIST
46
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-F 100 11F 22 (↵)
Issue two fill commands to fill memory locations with different values
4.4 Examining and Modifying the�Contents of Memory
Initialize all storage locations in the block of memory from DS:120 through DS:13F with the value 3316 and the block of storage locations from DS:140 to DS:15F with the value 4416.
47
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-F 120 13F 33 (↵)
-F 140 15F 44 (↵)
4.4 Examining and Modifying the�Contents of Memory
The MOVE command allows us to copy a block of data from one part of memory to another part. Note that the source locations is not affected
M START_ADDRESS END_ADDRESS DEST_ADDRESS
48
-M 100 11F 200 (↵)
|
|
|
|
|
|
|
|
|
|
Source starting address (100)
Destination starting address (11F)
|
|
|
|
|
|
|
|
|
|
Destination starting address (200)
4.4 Examining and Modifying the�Contents of Memory
Fill each storage location in the block of memory from address
DS:100 through DS:11F with the value 1116. Then copy this
block of data to a destination block starting at DS:160.
49
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-F 100 11F 11 (↵)
-M 100 11F 160 (↵)
4.4 Examining and Modifying the�Contents of Memory
The COMPARE command allows us to compare the contents of two blocks of data to determine if they are or are not the same.
C START_ADDRESS END_ADDRESS DEST_ADDRESS
50
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-C 100 10F 120 (↵)
If the two locations are equal 🡪 don’t display anything.
If the two locations are different 🡪 display each location with it’s content
4.4 Examining and Modifying the�Contents of Memory
51
CPE 0408330 @ 2009
S. Abed - HU, Jordan
Results produced when unequal data are found with a COMPARE command
|
|
|
|
|
|
|
|
|
|
DS:100
DS:10F
|
|
|
|
|
|
|
|
|
|
DS:120
4.4 Examining and Modifying the�Contents of Memory
The SEARCH command can be used to scan through a block of data in memory to determine whether or not it contains specific data.
S START_ADDRESS END_ADDRESS LIST
52
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-S 100 17F 33 (↵)
4.4 Examining and Modifying the�Contents of Memory
53
CPE 0408330 @ 2009
S. Abed - HU, Jordan
4.5 Input and Output of Data
The INPUT command read data from an input port of the 64K byte-wide ports of 8088 I/O.
I ADDRESS
54
-I 61 (↵)
4D
4.5 Input and Output of Data
The OUTPUT command write data to an output port of the 64K byte-wide ports of 8088 I/O.
O ADDRESS BYTE
55
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-O 61 4F (↵)
4.6 Hexadecimal Addition and�Subtraction
The HEXADECIMAL command provides the ability to add and subtract hexadecimal numbers.
H NUM1 NUM2
56
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-H ABC0 0FFF (↵)
BBBF 9BC1
-H BBBF A (↵)
BBC9 BBB5
*Both number and results are limited to four hexadecimal digits.
4.6 Hexadecimal Addition and�Subtraction
Use the H command to find the negative of the number 000916.
57
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-H 0 9 (↵)
0009 FFF7
4.6 Hexadecimal Addition and�Subtraction
If a byte of data is located at physical address 02A3416 and the data segment register contains 015016, what value must be loaded into the source index register such that DS:SI points to the byte storage location?
58
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-H 2A34 1500 (↵)
3F34 1534
4.7 Loading, Verifying and Saving�Machine Language Program
MOV BL, AL
59
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-E CS:100 88 C3 (↵)
-D CS:100 101 (↵)
1342:0100 88 C3
4.7 Loading, Verifying and Saving�Machine Language Program
The UNASSEMBLE command converts machine code instructions to their equivalent assembly language source statement.
U [STARTING_ADDRESS [ENDING_ADDRESS] ]
60
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-U CS:100 101 (↵)
1342:0100 88C3 MOV BL, AL
Hexadecimal
Instruction
Why CS not DS ?
4.7 Loading, Verifying and Saving�Machine Language Program
Use a sequence of commands to load, verify loading, and unassemble the machine code instruction 0304H. Load the instruction at address CS:200.
61
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-E CS:200 03 04 (↵)
-D CS:200 201 (↵)
-U CS:200 201 (↵)
ADD AX, [SI]
Why CS not DS ?
4.7 Loading, Verifying and Saving�Machine Language Program
The WRITE command gives the ability to save data stored in memory on a diskette.
W [START_ADDRESS [DRIVE START_SECTOR NUM_SECTOR] ]
62
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-W CS:200 1 10 1 (↵)
-W 200 1 10 1 (↵)
Drive B
1 Sector = 512 Byte
* Be caution in saving program in a disk, especially the hard drive.
4.7 Loading, Verifying and Saving�Machine Language Program
The LOAD command gives the ability to reload memory from a diskette.
L [START_ADDRESS [DRIVE START_SECTOR NUM_SECTOR] ]
63
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-L CS:300 1 10 1 (↵)
-U CS:300 301 (↵)
1342:300 301 ADD AX, [SI]
4.7 Loading, Verifying and Saving�Machine Language Program
Enter the machine code of the block move program. The program is to be loaded into memory starting at address CS:100. Verify, unassemble, and save the code.
64
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-E CS:100 B8 00 20 8E D8 BE 00 01 BF 20 01 B9 10
00 8A 24 88 25 46 (↵)
-D CS:100 117(↵)
-U CS:100 117(↵)
-W CS:100 1 100 1 (↵)
4.7 Loading, Verifying and Saving�Machine Language Program
65
CPE 0408330 @ 2009
S. Abed - HU, Jordan
4.7 Loading, Verifying and Saving�Machine Language Program
The NAME command, along with the WRITE command, gives the ability to save a program on the diskette under a file name.
N FILE NAME
(BX CX) = number of bytes
Because of programs are small 🡺 set BX = 0000H
N FILE NAME
L [STARTING ADDRESS]
66
CPE 0408330 @ 2009
S. Abed - HU, Jordan
4.7 Loading, Verifying and Saving�Machine Language Program
Save a machine code program into a file.
67
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-N A:BLK.1 (↵) ; Give a file name in disk A
-R CX (↵) ; Give a program size of 1816 bytes
CX XXXX
:18
-R BX (↵)
BX XXXX
:0 (↵)
W CS:100 (↵) ; Save the program in disk A
4.7 Loading, Verifying and Saving�Machine Language Program
Reload a program into memory.
68
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-N A:BLK.1 (↵) ; Give a file name in disk A
-L CS:100 (↵) ; Load the program name BLK.1 in disk A
C:\DOS>REN A:BLK.1 BLK.EXE (↵) ; Rename the file
C:\DOS>DEBUG A:BLK.EXE (↵) ; Load the program directly into memory
C:\DOS>A:BLK.EXE (↵) ; Run the program
4.8 Assembling Instructions with the Assemble Command
The ASSEMBLE command let us automatically assemble the instructions of a program.
A [STARTING_ADDRESS]
69
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-A CS:100 (↵)
1342:0100 _
1342:0100 ADD [BX+SI+1234], AX (↵)
1342:0104 _
-D CS:100 103 (↵)
The program will be saved in memory starting from this location
Your instruction
4.8 Assembling Instructions with the Assemble Command
Assemble a complete program with the ASSEMBLE command.
70
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-A CS:200 (↵)
0B35:0200 MOV AX, 2000 (↵)
0B35:0203 MOV DS, AX (↵)
0B35:0205 MOV SI, 100 (↵)
. . . .
. . . .
0B35:0217 NOP (↵)
0B35:0218 (↵)
4.8 Assembling Instructions with the Assemble Command
71
CPE 0408330 @ 2009
S. Abed - HU, Jordan
4.8 Assembling Instructions with the Assemble Command
72
CPE 0408330 @ 2009
S. Abed - HU, Jordan
4.9 Executing Instructions and Programs with the TRACE and GO command
The TRACE command provides the programmer with the ability to execute the program one instruction at a time.
T [=STARTING_ADDRESS] [NUMBER]
73
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-T =CS:100 (↵) // executes one instruction
-T (↵) // executes one instruction starting at CS:IP
-T =CS:100 3 (↵) // executes three instructions
Important
The address of the first instruction
4.9 Executing Instructions and Programs with the TRACE and GO command
Load and trace a program.
74
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-L CS:100 1 10 1 (↵)
-U 100 101 (↵)
-R AX (↵)
AX 0000
:1111 (↵)
-R SI (↵)
SI 0000
:1234 (↵)
-E DS:1234 22 22 (↵)
-T =CS:100 (↵)
4.9 Executing Instructions and Programs with the TRACE and GO command
75
CPE 0408330 @ 2009
S. Abed - HU, Jordan
4.9 Executing Instructions and Programs with the TRACE and GO command
The GO command is typically used to run programs that are already working or to execute programs in the later stages or debugging.
G [=STARTING_ADDRESS [BREAKPOINT_ADDRESS_LIST] ]
76
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-G =CS:200 217 (↵)
-G =CS:100 (↵)
-G (↵) // start execution from CS:IP
The list of addresses that the execution will stop on them and display internal registers information (maximum 10 breakpoints )
4.9 Executing Instructions and Programs with the TRACE and GO command
Use GO command to execute a program and examine the result.
77
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-N A:BLK.EXE (↵) ; Define the program file to be loaded
-L CS:200 (↵) ; Load the program at CS:200
-R DS (↵)
DS 1342
:2000 (↵) ; Define the data segment address
-F DS:100 10F FF (↵) ; Fill memory with FF
-F DS:120 12F 00 (↵) ; Fill memory with 00
-R DS (↵)
DS 2000
:1342 ; Store data segment with 134216
4.9 Executing Instructions and Programs with the TRACE and GO command
78
CPE 0408330 @ 2009
S. Abed - HU, Jordan
-R (↵) ; Show data register status
-U CS:200 217 (↵) ; Unassemble the program
-G =CS:200 20E (↵) ; Execute the program to CS:20E
-G =CS:20E 215 (↵) ; Execute the program to CS:215
-D DS:100 10F (↵) ; Display memory at DS:100
-D DS:120 12F (↵) ; Display memory at DS:120
-G =CS:215 217 (↵) ; Execute the program to CS:217
-D DS:100 10F (↵) ; Display memory at DS:100
-D DS:120 12F (↵) ; Display memory at DS:120
4.9 Executing Instructions and Programs with the TRACE and GO command
79
CPE 0408330 @ 2009
S. Abed - HU, Jordan
4.9 Executing Instructions and Programs with the TRACE and GO command
80
CPE 0408330 @ 2009
S. Abed - HU, Jordan
4.9 Executing Instructions and Programs with the TRACE and GO command
81
CPE 0408330 @ 2009
S. Abed - HU, Jordan
4.10 Debugging a Program
82
CPE 0408330 @ 2009
S. Abed - HU, Jordan
4.10 Debugging a Program
83
CPE 0408330 @ 2009
S. Abed - HU, Jordan
Debug Command Examples (1)
Write command(s) to before the following operations :
S. Abed - HU, Jordan
CPE 0408330 @ 2009
84
Debug Command Examples (2)
S. Abed - HU, Jordan
CPE 0408330 @ 2009
85
Debug Command Examples (3)
MOV AX,6789h
MOV BX,1111h
INC AH
MOV [1234h], AL
MOV [BX], AH
S. Abed - HU, Jordan
CPE 0408330 @ 2009
86
2, 5, 10, 15, 20, 23, 24, 26, 28, 30
87
CPE 0408330 @ 2009
S. Abed - HU, Jordan
H.W. #4