ECP2046 – Computer Organization and Architecture

LAB: CO1 Duration: 3 hours Introduction to the DEBUG Program

1 Objectives i. To study and understand the mechanisms involed in program execution in a microprocessor unit ii. To understand the concept of physical and logical addresses (Segment::Offset addressing).

2 Software MS-DOS DEBUG program. Please refer to http://foe.mmu.edu.my/Temp/ecp2046/CO1/Debug_Tutorial.pdf for additional examples for the program.

3 Introduction The generic term x86 refers to the instruction set of the most commercially successful CPU architecture in the history of personal computing. It is used in processors from Intel, AMD and others, and derived from the model numbers of the first few generations of processors, backward compatible with Intel's original 16-bit 8086 CPU.

3.1 X86 CPU registers The main tools to write programs in x86 assembly are the processor registers. The registers are like variables built in the processor. Using registers instead of memory to store values makes the process faster and cleaner. Table 1 lists some of the registers in the x86 series of processors.

Table 1: List of registers in x86 CPU series AX Accumulator BX Base register CX Counting register DX Data register DS Data Segment register ES Extra Segment register SS Battery segment register CS Code Segment register BP Base Pointers register SI Source Index register DI Destiny Index register IP Next Instruction Pointer register F Flag register

Of all the registers listed above, AX, BX, CX, and DX are general purpose registers which will be used extensively in the following exercises. These are 16-bit registers, which can also be used as 8-bit registers. To use them as 8-bit registers, it is necessary to refer to them for example as: AH and AL, which are the high and low bytes of the AX register. This nomenclature is also applicable to the BX, CX, and DX registers.

3.2 Physical and logical addresses A physical (absolute) address is the actual physical location in memory. A logical address is the address at which a memory location appears to reside from the perspective of an executing application program.

Logical address may be different from the physical address due to the operation of a memory management unit (MMU) between the CPU and the memory bus. MMU is a computer hardware component responsible for handling accesses to memory requested by the central processing unit (CPU). Its function includes translation of virtual addresses to physical addresses.

An example of the usage of logical address is the Segment:Offset addressing. Segment:Offset addressing was introduced due to the limitation in the capability to address memory contents. Without using Segment:Offset addressing, a CPU with a register of 16-bit long can only address 216 or 65,536 bytes (64 KBytes) of memory directly. This of course, significantly limits the size of usable program. Using Segment:Offset addressing, the CPU is able to group two 16-bit registers together to refer to a physical memory location beyond 64 KBytes. Of course, increasing the register size in the CPU will also increase the addressing capability. However, such approach can be very costly.

The physical address for any combination of Segment and Offset pairs is found by using the formula: Physical address = (Segment value × 1016) + Offset value There is another easier way to compute physical address from the Segment:Offset values. Refer to the following examples: Example 1: The Physical address of the Segment:Offset pair, 0A00:0001 can be computed by inserting a zero at the end of the Segment value ( which is the same as multiplying by 10 16 ) and then adding the Offset value: 0A000 + 0001 ------A001 ------

Example 2: Segment:Offset = 023F:02FF 023F0 + 02FF ------26EF ------

The disadvantage of using Segment:Offset pairs is the fact that there are a large number of these pairs that refer to the same exact memory locations. As an example, every Segment:Offset pair below refers to the same physical address of 7C00 (or 0000:7C00):

0047:7790 0048:7780 0049:7770 004A:7760 004B:7750 004C:7740 0077:7490 0078:7480 0079:7470 007A:7460 007B:7450 007C:7440 01FF:5C10 0200:5C00 0201:5BF0 0202:5BE0 0203:5BD0 0204:5BC0 07BB:0050 07BC:0040

The number of different Segment:Offset pairs vary between memory locations, which can be up to 4,09610 pairs. For the physical addresses from FFF0 to FFFFF, the number of Segment:Offset pairs that refers to the same physical address is fixed to 4,09610 pairs. For Physical addresses 016 through FFEF (0 through 65,51910), the number of different pairs can be computed by following 2 simple steps. First, divide the physical address by 1016 (which shifts the entire hexadecimal digits one place to the right). Second, throw away any fractional remainder and add 1 to the shifted value.

Below is an example of number of available Segment:Offset pairs for physical address 2B10: Example 3: Physical address = 2B10

Step 1: 2B10 / 1016 = 2B1

Step 2: 2B1 + 1 = 2B2 (or 69010) Segment:Offset pairs

3.3 Using the DEBUG program In this section, we will learn to use the DEBUG program to execute assembly language program on an x86 compatible microprocessor. A summary of the common commands that may be used with DEBUG is shown in Table 1 below.

3.4 Procedure: 1. Click the Start button on Windows’ taskbar, select Run, type “cmd” in the corresponding dialog box and press Enter () to invoke MS-DOS command prompt. 2. In the command prompt, type “debug” and press Enter to invoke the DEBUG program. 3. The DEBUG prompt (-) will be displayed, indicating that DEBUG has been executed and is now waiting for a command. 4. Type in “?” (without the double quotes) at the DEBUG prompt and press Enter. This will display a summary of the available commands and their syntax, as shown in Table 2. 5. These commands allow DEBUG to load data, examine or modify the state of the microprocessor’s internal registers and memory, and execute a program. The following exercises will focus only on some of the commands. Table 2: DEBUG program commands set Command Syntax assemble A [address] compare C range address dump D [range] enter E address [list] fill F range list go G [=address] [addresses] hex H value1 value2 input I port load L [address] [drive] [firstsector] [number] move M range address name N [pathname] [arglist] output O port byte proceed P [=address] [number] quit Q register R [register] search S range list trace T [=address] [value] unassembled U [range] write W [address] [drive] [firstsector] [number] allocate expanded memory XA [#pages] deallocate expanded memory XD [handle] map expanded memory pages XM [Lpage] [Ppage] [handle] display expanded memory status XS 6. The exercises will mainly focus on some of the commands. These commands and their functions are detailed in Table 3. 7. To quit from the DEBUG program, type “Q” (with the double quotes) in the DEBUG prompt and press Enter. This will terminate DEBUG and return to the command prompt.

Table 3: Commands and their functions Command Function Register Examine or modify the contents of an internal register Quit Exit from the DEBUG program Dump Dump the contents of memory to the display Enter Examine or modify the contents of memory Unassembl Unassemble the machine code into its equivalent assembler instructions e Assemble Assemble the instruction into machine code and store in memory Trace Trace (single-step) the assembled instructions Go Execute the instructions down through the breakpoint address

4 Exercise 1 In this exercise, we will assemble a program which adds together both 8-bit numbers at AL (lower byte of AX register) and BL (lower byte of BX register), subtract 24H from the result and multiply it with 04H. The instructions used in this program are describe in Table 4 below. Table 4: Description of instructions used in Exercise 1 Instruction Descriptions ADD BL, AL Add BL to AL, store result in AL

SUB AL, 24 Subtract 2416 from AL, store result in AL

MOV CL, 04 Store 416 in CL MUL CL Multiply CL to AL, store result in AX

4.1 Procedure: 1. Ivoke the DEBUG program. 2. Using the Register (R) command, change the contents of register AX to 0035H and BX to 0068H as follows:

-R AX AX 0000 :0035 -R BX BX 0000 :0068 -R IP IP 0100 : 3. Using the Assemble (A) command, assemble the instructions to perform required arithmetic operation as follows:

-A CS:0100 XXXX:0100 ADD BL,AL XXXX:0102 SUB AL,24 XXXX:0104 MOV CL,04 XXXX:0106 MUL CL XXXX:0108  *XXXX indicates that the contents of CS may vary from machine to machine.

4. Using the Unassemble (U) command, the instructions previously assembled into the memory can be verified as shown below:

-U CS:0100 0106

5. This command will display the instructions assembled between address CS:0100 to CS:0106 in 3 columns: 1st column shows the logical addresses of the instructions; 2nd column shows the machine codes of each instruction; 3rd column shows the assembled instructions. 6. Record down the output of the Unassemble command in Table 5. Table 5: Output of the Unassemble command Address Machine Codes Instruction

7. The Trace (T) command can then be used to step through the assembled instructions by executing one instruction at a time and the contents of the internal registers after the execution of each instruction are automatically displayed. 8. Before executing the instructions, use the Register (R) instruction to check the contents of the microprocessor’s internal registers and record down the contents of AX, BX, CX and IP registers in Row 1 of Table 6. 9. Execute the first assembled instruction (ADD BL, AL) by using the Trace (T) command as follows:

-T =CS:0100

10. Check the contents of AX, BX,CX and IP registers after executing the instruction and record down their contents in Row 2 of Table 6.

11. Execute the next instruction (SUB AL, 24) by issuing the Trace (T) command as follows:

-T  This form of the Trace command is used to execute the next instruction.

12. Check and record down the contents of AX, BX, CX and IP registers in the next row of Table 6. 13. Repeat Step (11) to execute the two remaining instructions and record down the contents of AX, BX, CX and IP in Table 6. Table 6: Contents of IP, AX, BX and CX after each Trace command Row No. IP AX BX CX 1 2 3 4 5 6

5 Exercise 2 Dump is one the commands from the DEBUG program. It displays the contents of a block of memory. The Memory locations near the beginning of Segment C000 should display information about the kind of video card installed on your PC. The example below shows the video card information of a system obtained from the dump command using logical address C000:0010.

-d C000:0010 C000:0010 24 12 FF FF 00 00 00 00-60 00 00 00 00 20 49 42 $...... `.... IB C000:0020 4D 20 43 4F 4D 50 41 54-49 42 4C 45 20 4D 41 54 M COMPATIBLE MAT C000:0030 52 4F 58 2F 4D 47 41 2D-47 31 30 30 20 56 47 41 ROX/MGA-G100 VGA C000:0040 2F 56 42 45 20 42 49 4F-53 20 28 56 31 2E 32 20 /VBE BIOS (V1.2 C000:0050 29 00 87 DB 87 DB 87 DB-87 DB 87 DB 87 DB 87 DB )...... C000:0060 50 43 49 52 2B 10 01 10-00 00 18 00 00 00 00 03 PCIR+...... C000:0070 40 00 12 10 00 80 00 00-38 37 34 2D 32 00 FF FF @...... 874-2... C000:0080 E8 26 56 8B D8 E8 C6 56-74 22 8C C8 3D 00 C0 74 .&V....Vt"..=..t

The dump command will be used in the following exercise.

5.1 Procedure

1. Invoke the DEBUG program. 2. Use the dump command to display the video card information on your system. 3. Use the dump command to verify that logical addresses 0007:7B90 and 07C0:0000 is pointing to the same physical address by looking at the memory contents. 4. Calculate the physical address of logical address 0C10:0010. Verify your answer using the dump command by looking at the memory contents. Assuming XXXX is your physical address, type: d 0000:XXXX to access the memory location. 5. Calculate the number of logical address available for physical address 6B00. Provide your answer in hexadecimal and decimal forms. 6. Calculate the number of Segment:Offset pairs that point to the same physical location as the Segment:Offset pair 0030:0000. Include Segment:Offset pair 0030:0000 in your calculation. Provide your answer in hexadecimal and decimal forms. Hint: Verification can be done by comparing the contents of memory locations to see whether they are the same or not. 6 Exercise 3

In this exercise, you are required to complete the assembly program for 8-bit unsigned integer multiplication.

Figure 1: Flow chart for 8-bit unsigned integer multiplication

6.1 Procedure 1. Assume the following: AH = Register A AL = Multiplier, Q BL = Multiplicand, M CX = Count 2. Complete the following assembly program for 8-bit unsigned integer multiplication of multiplier = 25H and multiplicand = 24H. ORG 0100 CLC MOV AH, 00 MOV AL, _____ MOV BL, _____ MOV CX, _____

MOV DL, AL AND DL, _____ JZ 0113 ADD _____, BL RCR _____, 1 CLC LOOP 010A MOV DX, AX

3. Verify your answer by keying in the assembly program into the DEBUG program and stepping through the program. Record the register content change for each step until the program reaches the end by completing the table below: Table 7: Contents of the registers in each step Steps BL AH AL CX DL 1 2 3 . . . .

4. What is the final value in register DX? 5. What is the product of 25H and 24H (Show manual calculation)? Is the calculated value the same as the one in register DX?

7 Report writing guidelines Your report must contain the following items/sections: Report cover. Please follow the report cover format in the FOE lab webpage. You can download the template at http://foe.mmu.edu.my/Temp/ecp2046/lab_report_cover.doc  Brief introduction of the experiment. You may include experiment motivations/objectives and a short description on the DEBUG program. Please use your own words.  Experiment results and discussion. Please include the answers to exercise questions. You can also describe your observations of this experiment.  Conclusion. Conclude your lab report with a brief summary on knowledge and skills that you have acquired from this experiment.