1 of 87

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]

2 of 87

Lecture Outline

  • 4.1 Converting Assembly Language Instructions to Machine Code
  • 4.2 Encoding a Complete Program in Machine Code
  • 4.3 The PC and Its DEBUG Program
  • 4.4 Examining and Modifying the Contents of Memory
  • 4.5 Input and Output of Data

2

CPE 0408330 @ 2009

S. Abed - HU, Jordan

3 of 87

Lecture Outline

  • 4.6 Hexadecimal Addition and Subtraction
  • 4.7 Loading, Verifying and Saving Machine Language Program
  • 4.8 Assembling Instructions with the Assemble Command
  • 4.9 Executing Instructions and Programs with the TRACE and GO command
  • 4.10 Debugging a Program

3

CPE 0408330 @ 2009

S. Abed - HU, Jordan

4 of 87

4.1 Converting Assembly Language Instructions to Machine Code

  • Part of the 80x86 instruction set architecture (ISA)
    • What is the machine instruction length (fixed, variable, hybrid)?
    • What are the sizes of the fields—varying sizes?
    • What are the functions of the fields?

  • 80x86’s register-memory architectures is hybrid length
    • Multiple instruction sizes, but all have byte wide lengths—
      • 1 to 6 bytes in length for 8088/8086
      • Up to 17 bytes for 80386, 80486, and Pentium
    • Advantages of hybrid length
      • Allows for many addressing modes
      • Allows full size (32-bit) immediate data and addresses
    • Disadvantage of variable length
      • Requires more complicated decoding hardware—speed of decoding is critical in modern application

  • Load-store architectures normally fixed length—PowerPC (32-bit), SPARC (32-bit), MIP (32-bit), Itanium (128-bits, 3 instructions)

4

CPE 0408330 @ 2009

S. Abed - HU, Jordan

5 of 87

4.1 Converting Assembly Language Instructions to Machine Code

  • General instruction format for machine code:

5

CPE 0408330 @ 2009

S. Abed - HU, Jordan

6 of 87

4.1 Converting Assembly Language Instructions to Machine Code

  • Byte 1 specification
    • Opcode field (6-bits)
      • Specifies the operation to be performed
    • Register direction bit (D-bit)
      • 1 – the register operand is a destination operand
      • 0 – the register operand is a source operand
    • Data size bit (W-bit)
      • 1 – 16-bit data size
      • 0 – 8-bit data size

6

CPE 0408330 @ 2009

S. Abed - HU, Jordan

7 of 87

4.1 Converting Assembly Language Instructions to Machine Code

  • Byte 2 specification
    • Mode (MOD) field (2-bits)—specifies the type of the second operand

7

CPE 0408330 @ 2009

S. Abed - HU, Jordan

MOD field and R/M field encoding

8 of 87

4.1 Converting Assembly Language Instructions to Machine Code

  • Byte 2 specification
    • Mode (MOD) field (2-bits):
      • Memory mode: 00, 01,10—Register to memory move operation
        • 00 = no immediate displacement (register used for addressing)
        • 01 = 8-bit displacement (imm8) follows (8-bit offset address)
        • 10 = 16-bit displacement (imm16) follows (16-bit offset address)
    • Register mode: 11—register to register move operation
      • 11 = register specified as the second operand

8

CPE 0408330 @ 2009

S. Abed - HU, Jordan

Register (REG) field encoding

9 of 87

4.1 Converting Assembly Language Instructions to Machine Code

  • Byte 2 specification
    • Register (REG) field (3-bit)
      • Identifies the register for the first operand
      • W (1-bit)—data size word/byte for all registers
        • Byte = 0
        • Word =1
    • Register/Memory (R/M) field (3-bit)

9

CPE 0408330 @ 2009

S. Abed - HU, Jordan

Register (REG) field encoding

10 of 87

4.1 Converting Assembly Language Instructions to Machine Code

  • Byte 2 specification
    • Register/Memory (R/M) field (3-bit)specifies the second operand as a register or a storage location in memory
        • Dependent on MOD field
          • Mod = 11 R/M selects a register
            • R/M = 000 Accumulator register
            • R/M= 001 = Count register
            • R/M = 010 = Data Register

10

CPE 0408330 @ 2009

S. Abed - HU, Jordan

11 of 87

4.1 Converting Assembly Language Instructions to Machine Code

  • Byte 2 specification
    • MOD = 00,10, or 10 selects an addressing mode for the second operand that is a storage location in memory, which may be the source or destination
      • Dependent on MOD field
      • Mod = 00 R/M
        • R/M = 100 🡪 effective address computed as EA = (SI)
        • R/M= 000 = 🡪 effective address computed as EA = (BX)+(SI)
        • R/M = 110 = 🡪 effective address is coded in the instruction as a direct address EA = direct address

= imm8 or imm16

S

S

S

S

s

11

CPE 0408330 @ 2009

S. Abed - HU, Jordan

12 of 87

4.1 Converting Assembly Language Instructions to Machine Code

  • EXAMPLE

Encode the instruction in machine code

MOV BL, AL

  • Solution:

Byte 1: OPCODE = 100010 (for MOV),

D = 0 (source), W = 0 (8-bit)

  • This leads to BYTE 1 = 100010002 = 8816
  • In byte 2 the source operand, specified by REG, is AL

REG = 000, MOD = 11, R/M = 011

  • Therefore, BYTE 2 = 110000112 = C316

MOV BL, AL = 88C316

12

CPE 0408330 @ 2009

S. Abed - HU, Jordan

13 of 87

MOV BL, AL

14 of 87

4.1 Converting Assembly Language Instructions to Machine Code

  • EXAMPLE

Encode the instruction in machine code

ADD AX, [SI]

  • Solution:

OPCODE = 000000 (for ADD), D = 1 (dest.), W = 1 (16-bit)

  • This leads to BYTE 1 = 000000112 = 0316
  • In byte 2 the destination operand, specified by REG, is AX

REG = 000, MOD = 00, R/M = 100

  • Therefore, BYTE 2 = 000001002 = 0416

ADD AX, [SI] = 030416

14

CPE 0408330 @ 2009

S. Abed - HU, Jordan

15 of 87

ADD AX, [SI]

16 of 87

4.1 Converting Assembly Language Instructions to Machine Code

  • EXAMPLE

Encode the instruction in machine code

XOR CL, [1234H]

  • Solution:

OPCODE = 001100 (for XOR), D = 1 (dest.), W = 0 (8-bit)

  • This leads to BYTE 1 = 001100102 = 3216
  • In byte 2 the destination operand, specified by REG, is CL

REG = 001, MOD = 00, R/M = 110

  • Therefore, BYTE 2 = 000011102 = 0E16

BYTE 3 = 3416

BYTE 4 = 1216

XOR CL, [1234H]= 320E341216

16

CPE 0408330 @ 2009

S. Abed - HU, Jordan

17 of 87

XOR CL, [1234H]

18 of 87

4.1 Converting Assembly Language Instructions to Machine Code

  • EXAMPLE

Encode the instruction in machine code

ADD [BX][DI]+1234H, AX

  • Solution:

OPCODE = 000000 (for ADD), D = 0 (source), W = 1 (16-bit)

  • This leads to BYTE 1 = 000000012 = 0116
  • In byte 2 the destination operand, specified by REG, is AX

REG = 000, MOD = 10, R/M = 001

  • Therefore, BYTE 2 = 100000012 = 8116

BYTE 3 = 3416

BYTE 4 = 1216

ADD [BX][DI]+1234H, AX = 0181341216

18

CPE 0408330 @ 2009

S. Abed - HU, Jordan

19 of 87

ADD [BX][DI]+1234H, AX

20 of 87

4.1 Converting Assembly Language Instructions to Machine Code

  • Additional one-bit field and their functions

  • Instructions that involve a segment register (SR-field)

20

CPE 0408330 @ 2009

S. Abed - HU, Jordan

21 of 87

4.1 Converting Assembly Language Instructions to Machine Code

  • EXAMPLE

Encode the instruction in machine code

MOV WORD PTR [BP][DI]+1234H, 0ABCDH

  • Solution:

This example does not follow the general format

  • From Fig. 3-6 in the text, MOV -> 1100011W, and W = 1 for word-size data
  • BYTE 1 = 110001112 = C716
  • BYTE 2 = (MOD)000(R/M) = 100000112 = 8316
  • BYTE 3 = 3416 BYTE 4 = 1216
  • BYTE 5 = CD16 BYTE 6 = AB16

MOV WORD PTR [BP][DI]+1234H, 0ABCDH

= C7833412CDAB16

21

CPE 0408330 @ 2009

S. Abed - HU, Jordan

22 of 87

MOV WORD PTR [BP][DI]+1234H, 0ABCDH

23 of 87

4.1 Converting Assembly Language Instructions to Machine Code

  • EXAMPLE

Encode the instruction in machine code

MOV [BP][DI]+1234H, DS

  • Solution:

This example does not follow the general format

  • From Fig. 3-6 in the text, MOV -> 10001100, and the instruction is 10001100(MOD)0(SR)(R/M)(DISP)
  • From Fig. 4-5 in the text, we find that for DS, the SR = 11
  • Therefore, the instruction is coded as

MOV [BP][DI]+1234H, DS

= 100011001001101100110100000100102

= 8C9B341216

23

CPE 0408330 @ 2009

S. Abed - HU, Jordan

24 of 87

MOV [BP][DI]+1234H, DS

25 of 87

4.2 Encoding a Complete Program in Machine Code

  • Steps in encoding a complete assembly program:
    • Identify the general machine code format (Fig. 3-6)
    • Evaluate the bit fields (Fig. 4-2,4-3,4-4,4-5)
    • Express the binary-code instruction in hexadecimal form
  • To execute the program, the machine code of the program must be stored in the code segment of memory.
  • The first byte of the program is stored at the lowest address.

25

CPE 0408330 @ 2009

S. Abed - HU, Jordan

ADD AX , ABCDh

26 of 87

4.2 Encoding a Complete Program in Machine Code

  • EXAMPLE

Encode the “block move” program in Fig. 4-6(a) and show how it would be stored in memory starting at address 20016.

  • Solution:

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

27 of 87

4.2 Encoding a Complete Program in Machine Code

27

CPE 0408330 @ 2009

S. Abed - HU, Jordan

28 of 87

4.2 Encoding a Complete Program in Machine Code

28

CPE 0408330 @ 2009

S. Abed - HU, Jordan

29 of 87

4.3 The PC and Its DEBUG Program

  • Using DEBUG, the programmer can issue commands to the microcomputer.
  • Loading the DEBUG program

C:\DEBUG

  • Six kinds of information are entered as part of a command:
    • A command letter
    • An address
    • A register name
    • A file name
    • A drive name
    • Data

29

CPE 0408330 @ 2009

S. Abed - HU, Jordan

-R AX

-D DS:100

-N A:BLK.1

-F 100 11F 22

30 of 87

4.3 The PC and Its DEBUG Program

  • The DEBUG program command set
    • Register: R [register name]
    • Quit: Q
    • Dump: D[address]
    • Enter: E address [list]
    • Fill: F st. address end address list
    • Move: M st. addr. end addr. dest. Addr.
    • Compare: C st. addr. end addr. dest. Addr.
    • Search: S st. address end address list
    • Input: I address
    • Output: O address, byte
    • Hex Add/Subtract: H num1,num2
    • Assemble: A [starting address]
    • Unassemble: U [starting address ending address]
    • Name: N file name]
    • Write: W [st. addr. [drive st. sector no. of sectors]]
    • Load: L [st. addr. [drive st. sector no. of sectors]]
    • Trace: T [=address] [number]
    • Go: G [= starting address [breakpoint address …]]

30

CPE 0408330 @ 2009

S. Abed - HU, Jordan

31 of 87

4.3 The PC and Its DEBUG Program

  • An initial state when with the loading of DEBUG

31

CPE 0408330 @ 2009

S. Abed - HU, Jordan

Status register (Flags) =

0000 0000 0100 0000

Interrupt flag = 1 🡺 interrupt enable

32 of 87

4.3 The PC and Its DEBUG Program

  • Syntax for the REGISTER (R) command

R [REGISTER NAME]

  • e.g.

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

33 of 87

4.3 The PC and Its DEBUG Program

  • EXAMPLE

Verify the initialized state of the 8088 by examining the contents of its registers with the Register command.

  • Solution:

33

CPE 0408330 @ 2009

S. Abed - HU, Jordan

-R (↵)

34 of 87

4.3 The PC and Its DEBUG Program

  • Register mnemonics for the R command

34

CPE 0408330 @ 2009

S. Abed - HU, Jordan

35 of 87

4.3 The PC and Its DEBUG Program

  • Status flag notations

35

CPE 0408330 @ 2009

S. Abed - HU, Jordan

36 of 87

4.3 The PC and Its DEBUG Program

  • EXAMPLE

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.

  • Solution:

36

CPE 0408330 @ 2009

S. Abed - HU, Jordan

-R BX (↵)

BX 0000

:FF00 (↵)

-R BX (↵)

BX FF00

:_ (↵)

_

37 of 87

4.3 The PC and Its DEBUG Program

  • EXAMPLE

Use the Register command to set the parity flag to even parity. Verify that the flag has been changed.

  • Solution:

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 - (↵)

38 of 87

4.4 Examining and Modifying the�Contents of Memory

  • The commands provided for use in examining and modifying the memory:
    • DUMP
    • ENTER
    • FILL
    • MOVE
    • COMPARE
    • SERACH

38

CPE 0408330 @ 2009

S. Abed - HU, Jordan

39 of 87

4.4 Examining and Modifying the�Contents of Memory

  • DUMP Command (D)

The DUMP command allows us to examine the contents of a memory location or a block of consecutive memory location.

D [ADDRESS]

  • e.g.

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

40 of 87

4.4 Examining and Modifying the�Contents of Memory

  • DUMP Command (D)

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

41 of 87

4.4 Examining and Modifying the�Contents of Memory

  • EXAMPLE

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.

  • Solution:

41

CPE 0408330 @ 2009

S. Abed - HU, Jordan

-D 300 31F (↵)

42 of 87

4.4 Examining and Modifying the�Contents of Memory

  • EXAMPLE

Use the Dump command to examine the 16 bytes of memory just below the top of the stack.

  • Solution:

42

CPE 0408330 @ 2009

S. Abed - HU, Jordan

-D SS:FFEE FFFD (↵)

43 of 87

4.4 Examining and Modifying the�Contents of Memory

  • ENTER Command (E)

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

44 of 87

4.4 Examining and Modifying the�Contents of Memory

  • EXAMPLE

Start a data entry sequence by examining the contents of address DS:100 and then, without entering new data, depress the “-” key. What happen?

  • Solution:

Entering “-” causes the display of previous byte storage location.

44

CPE 0408330 @ 2009

S. Abed - HU, Jordan

-E DS:100 (↵)

1342:0100 FF. _

45 of 87

4.4 Examining and Modifying the�Contents of Memory

  • EXAMPLE

Enter ASCII data to the memory.

  • Solution:

45

CPE 0408330 @ 2009

S. Abed - HU, Jordan

-E DS:200 “ASCII” (↵)

or

-E DS:200 ‘ASCII’ (↵)

46 of 87

4.4 Examining and Modifying the�Contents of Memory

  • FILL Command (F)

The FILL command fills a block of consecutive memory locations all with the same data.

F STARTING_ADDRESS ENDING_ADDRESS LIST

  • e.g.

46

CPE 0408330 @ 2009

S. Abed - HU, Jordan

-F 100 11F 22 (↵)

Issue two fill commands to fill memory locations with different values

47 of 87

4.4 Examining and Modifying the�Contents of Memory

  • EXAMPLE

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.

  • Solution:

47

CPE 0408330 @ 2009

S. Abed - HU, Jordan

-F 120 13F 33 (↵)

-F 140 15F 44 (↵)

48 of 87

4.4 Examining and Modifying the�Contents of Memory

  • MOVE Command (M)

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

  • e.g.

48

-M 100 11F 200 (↵)

Source starting address (100)

Destination starting address (11F)

Destination starting address (200)

49 of 87

4.4 Examining and Modifying the�Contents of Memory

  • EXAMPLE

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.

  • Solution:

49

CPE 0408330 @ 2009

S. Abed - HU, Jordan

-F 100 11F 11 (↵)

-M 100 11F 160 (↵)

50 of 87

4.4 Examining and Modifying the�Contents of Memory

  • COMPARE Command (C)

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

  • e.g.

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

51 of 87

4.4 Examining and Modifying the�Contents of Memory

  • COMPARE Command (C)

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

52 of 87

4.4 Examining and Modifying the�Contents of Memory

  • SEARCH Command (S)

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

  • e.g.

52

CPE 0408330 @ 2009

S. Abed - HU, Jordan

-S 100 17F 33 (↵)

53 of 87

4.4 Examining and Modifying the�Contents of Memory

  • SEARCH Command (S)

53

CPE 0408330 @ 2009

S. Abed - HU, Jordan

54 of 87

4.5 Input and Output of Data

  • INPUT Command (I)

The INPUT command read data from an input port of the 64K byte-wide ports of 8088 I/O.

I ADDRESS

  • e.g.

  • The contents of the port at I/O address 006116 are 4D16

54

-I 61 (↵)

4D

55 of 87

4.5 Input and Output of Data

  • OUTPUT Command (O)

The OUTPUT command write data to an output port of the 64K byte-wide ports of 8088 I/O.

O ADDRESS BYTE

  • e.g.

  • This command causes the value 4F16 to be written into the byte-wide output port at address 006116

55

CPE 0408330 @ 2009

S. Abed - HU, Jordan

-O 61 4F (↵)

56 of 87

4.6 Hexadecimal Addition and�Subtraction

  • HEXADECIMAL Command (H)

The HEXADECIMAL command provides the ability to add and subtract hexadecimal numbers.

H NUM1 NUM2

  • e.g.

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.

57 of 87

4.6 Hexadecimal Addition and�Subtraction

  • EXAMPLE

Use the H command to find the negative of the number 000916.

  • Solution:

  • FFF716 is the negative of 916 expressed in 2’s complement form.

57

CPE 0408330 @ 2009

S. Abed - HU, Jordan

-H 0 9 (↵)

0009 FFF7

58 of 87

4.6 Hexadecimal Addition and�Subtraction

  • EXAMPLE

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?

  • Solution:

  • This shows that SI must be loaded with the value 153416.

58

CPE 0408330 @ 2009

S. Abed - HU, Jordan

-H 2A34 1500 (↵)

3F34 1534

59 of 87

4.7 Loading, Verifying and Saving�Machine Language Program

  • An example to load an instruction

MOV BL, AL

  • The machine code is 88C316

59

CPE 0408330 @ 2009

S. Abed - HU, Jordan

-E CS:100 88 C3 (↵)

-D CS:100 101 (↵)

1342:0100 88 C3

60 of 87

4.7 Loading, Verifying and Saving�Machine Language Program

  • UNASSEMBLE Command (U)

The UNASSEMBLE command converts machine code instructions to their equivalent assembly language source statement.

U [STARTING_ADDRESS [ENDING_ADDRESS] ]

  • e.g.

60

CPE 0408330 @ 2009

S. Abed - HU, Jordan

-U CS:100 101 (↵)

1342:0100 88C3 MOV BL, AL

Hexadecimal

Instruction

Why CS not DS ?

61 of 87

4.7 Loading, Verifying and Saving�Machine Language Program

  • EXAMPLE

Use a sequence of commands to load, verify loading, and unassemble the machine code instruction 0304H. Load the instruction at address CS:200.

  • Solution:

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 ?

62 of 87

4.7 Loading, Verifying and Saving�Machine Language Program

  • WRITE Command (W)

The WRITE command gives the ability to save data stored in memory on a diskette.

W [START_ADDRESS [DRIVE START_SECTOR NUM_SECTOR] ]

  • e.g.

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.

63 of 87

4.7 Loading, Verifying and Saving�Machine Language Program

  • LOAD Command (L)

The LOAD command gives the ability to reload memory from a diskette.

L [START_ADDRESS [DRIVE START_SECTOR NUM_SECTOR] ]

  • e.g.

  • e.g.

63

CPE 0408330 @ 2009

S. Abed - HU, Jordan

-L CS:300 1 10 1 (↵)

  • The reloading of the instruction can be verified by U command

-U CS:300 301 (↵)

1342:300 301 ADD AX, [SI]

64 of 87

4.7 Loading, Verifying and Saving�Machine Language Program

  • EXAMPLE

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.

  • Solution:

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 (↵)

65 of 87

4.7 Loading, Verifying and Saving�Machine Language Program

65

CPE 0408330 @ 2009

S. Abed - HU, Jordan

66 of 87

4.7 Loading, Verifying and Saving�Machine Language Program

  • NAME Command (N)

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

    • The BX, CX registers must be updated to identify the size of the program that is to be saved in the file.

(BX CX) = number of bytes

Because of programs are small 🡺 set BX = 0000H

    • After BX, CX registers have been initialized, the write command is used to saved the program.
    • To reload the program, the command sequence is

N FILE NAME

L [STARTING ADDRESS]

66

CPE 0408330 @ 2009

S. Abed - HU, Jordan

67 of 87

4.7 Loading, Verifying and Saving�Machine Language Program

  • EXAMPLE

Save a machine code program into a file.

  • Solution:

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

68 of 87

4.7 Loading, Verifying and Saving�Machine Language Program

  • EXAMPLE

Reload a program into memory.

  • Solution:

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

69 of 87

4.8 Assembling Instructions with the Assemble Command

  • ASSEMBLE Command (A)

The ASSEMBLE command let us automatically assemble the instructions of a program.

A [STARTING_ADDRESS]

  • e.g.

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

70 of 87

4.8 Assembling Instructions with the Assemble Command

  • EXAMPLE

Assemble a complete program with the ASSEMBLE command.

  • Solution:

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 (↵)

71 of 87

4.8 Assembling Instructions with the Assemble Command

  • Assemble a program with ASSEMBLE command

71

CPE 0408330 @ 2009

S. Abed - HU, Jordan

72 of 87

4.8 Assembling Instructions with the Assemble Command

  • Unassemble a program with UNASSEMBLE command (the reverse operation of assemble)

72

CPE 0408330 @ 2009

S. Abed - HU, Jordan

73 of 87

4.9 Executing Instructions and Programs with the TRACE and GO command

  • TRACE Command (T)

The TRACE command provides the programmer with the ability to execute the program one instruction at a time.

T [=STARTING_ADDRESS] [NUMBER]

  • e.g.

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

74 of 87

4.9 Executing Instructions and Programs with the TRACE and GO command

  • EXAMPLE

Load and trace a program.

  • Solution:

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 (↵)

75 of 87

4.9 Executing Instructions and Programs with the TRACE and GO command

75

CPE 0408330 @ 2009

S. Abed - HU, Jordan

76 of 87

4.9 Executing Instructions and Programs with the TRACE and GO command

  • GO Command (G)

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] ]

  • e.g.

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 )

77 of 87

4.9 Executing Instructions and Programs with the TRACE and GO command

  • EXAMPLE

Use GO command to execute a program and examine the result.

  • Solution:

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

78 of 87

4.9 Executing Instructions and Programs with the TRACE and GO command

  • Solution (continued) :

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

79 of 87

4.9 Executing Instructions and Programs with the TRACE and GO command

79

CPE 0408330 @ 2009

S. Abed - HU, Jordan

80 of 87

4.9 Executing Instructions and Programs with the TRACE and GO command

80

CPE 0408330 @ 2009

S. Abed - HU, Jordan

81 of 87

4.9 Executing Instructions and Programs with the TRACE and GO command

81

CPE 0408330 @ 2009

S. Abed - HU, Jordan

82 of 87

4.10 Debugging a Program

  • Errors in a program are also referred to as bugs; the process of removing them is called debugging.
  • Two types of errors
    • Syntax error
    • Execution error
  • A syntax error is an error caused by not following the rules for coding or entering an instruction. These types of errors are typically identified by the microcomputer and signalled to user with an error message
  • In the DEBUG environment, the TRACE command is usually used to debug execution errors.

82

CPE 0408330 @ 2009

S. Abed - HU, Jordan

83 of 87

4.10 Debugging a Program

  • Review of the DEBUG commands
    • Register: R [register name]
    • Quit: Q
    • Dump: D[address]
    • Enter: E address [list]
    • Fill: F st. address end address list
    • Move: M st. addr. end addr. dest. Addr.
    • Compare: C st. addr. end addr. dest. Addr.
    • Search: S st. address end address list
    • Input: I address
    • Output: O address, byte
    • Hex Add/Subtract: H num1,num2
    • Assemble: A [starting address]
    • Unassemble: U [starting address ending address]
    • Name: N file name]
    • Write: W [st. addr. [drive st. sector no. of sectors]]
    • Load: L [st. addr. [drive st. sector no. of sectors]]
    • Trace: T [=address] [number]
    • Go: G [= starting address [breakpoint address …]]

83

CPE 0408330 @ 2009

S. Abed - HU, Jordan

84 of 87

Debug Command Examples (1)

Write command(s) to before the following operations :

  1. Display the content of 10 words of extra segment starting from offset 12EC followed by 10 words from Stack segment starting at offset 3DC8

S. Abed - HU, Jordan

CPE 0408330 @ 2009

84

85 of 87

Debug Command Examples (2)

  1. Store the value 8765h inside code segment in an offset that starts with 9090h

S. Abed - HU, Jordan

CPE 0408330 @ 2009

85

86 of 87

Debug Command Examples (3)

  1. Execute the following code and check the results

MOV AX,6789h

MOV BX,1111h

INC AH

MOV [1234h], AL

MOV [BX], AH

S. Abed - HU, Jordan

CPE 0408330 @ 2009

86

87 of 87

  • Solve the following problems from Chapter 4 from the course textbook:

2, 5, 10, 15, 20, 23, 24, 26, 28, 30

87

CPE 0408330 @ 2009

S. Abed - HU, Jordan

H.W. #4