Sie sind auf Seite 1von 12

Laboratory Manual for ES C263 Microprocessor Programming & Interfacing J P Misra S Mohan S Gurunarayanan

Educational Development Division Birla Institute of Technology and Science, Pilani 2006

LAB-2 Introduction to Debug


Introduction This lab will help in observing the contents of registers of the microprocessor and the memory locations that the processor can address. DOS program named Debug will allow you to view memory, to enter programs in memory and to trace their execution. Starting Debug There are two methods to start the debug command Method 1 : At the DOS prompt type debug and press enter. This will give a hyphen (-) which means the debug is waiting for one of its one letter command. Method 2: At the DOS prompt type debug \path\filename and press enter. Debug will then load itself into memory along with the file that is specified in the path\filename field of the command line and put the first byte of the file at offset 100H of the work area. Rules of Debug command It does not distinguish between lower case and upper case letters. It assumes that all numbers are in Hexadecimal unlike Microsoft assembler, which assumes numbers to be decimal. Segment and offset are specified as segment: offset. Spaces in commands are used only to separate parameters 8086 Register Set : General Purpose Registers AX (AH,AL) BX (BH,BL) CX (CH,CL) DX (DH,DL) Accumulator : Main arithmetic register Base : Generally used as a memory base or offset Counter : Generally used as a counter for loops Data : General 16-bit storage, division remainder

Offset Registers IP Instruction pointer : Current instruction offset SP Stack pointer : Current stack offset BP Base pointer : Base for referencing values stored on stack SI Source index : General addressing, source offset in string operations DI Destination index : General addressing, destination in string operations

Segment Registers CS SS DS ES Code segment Stack segment Data segment Extra segment : Segment to which IP refers : Segment to which SP refers : General addressing, usually for program's data area : General addressing, destination segment in string operations

Flags Register (Respectively bits 11,10,9,8,7,6,4,2,0) OF Overflow flag - Indicates a signed arithmetic overflow occurred DF Direction flag -Controls incr. direction in string operations (0=inc, 1=dec) IF Interrupt flag -Controls whether interrupts are enabled TF Trap flag : Controls debug interrupt generation after instructions SF Sign flag : Indicates a negative result or comparison ZF Zero flag : Indicates a zero result or an equal comparison AF Auxiliary flag : Indicates adjustment is needed after BCD arithmetic PF Parity flag : Indicates an even number of 1 bits CF Carry flag : Indicates an arithmetic carry occurred Note : The 8086 CPU addresses memory, using SEGMENT: OFFSET scheme (explained later), which produces 20 bit physical address using 16 bit address. Debug Commands A D E G P Q R T U ? Assembles symbolic instructions into machine code display the contents of an area of memory in hex format Enter data into memory beginning at specific location Run the executable program in the memory (Go) Proceed, Execute a set of related instructions Quit the debug session Display the contents of one or more registers in hex format Trace the execution of on instruction Unassemble machine code into symbolic code Debug Help

R, the Register command: examining and altering the content of registers In DEBUG, the register command R allows you to examine and/or alter the contents of the internal CPU registers. R <register name> The R command will display the content of all registers unless the optional <register name> field is entered. In which case, only the content of the selected register will be displayed. The R command also displays the instruction pointed to by the IP. The R command without the register name field, responds with three lines of information. The 3

first two lines show you the programmers model of the 80x86 microprocessor. (The first line displays the contents of the general-purpose, pointer, and index registers. The second line displays the current values of the segment registers, the instruction pointer, and the flag register bits). The third line shows the location, machine code, and Assembly code of the next instruction to be executed. Question 1 When DEBUG is first invoked, what are the values in the general-purpose registers? What is the reason for setting [IP] = 0100 ? Recall that if the optional <register name> field is specified in the R command, DEBUG will display the contents of the selected register and give you an opportunity to change its value. Just type a <return> if no change is needed, e.g. - r CX CX 0000 : FFF - r CX CX 0FFF : Note that DEBUG pads input numbers on the left with zeros if fewer than four digits are typed in. A, the Assemble command The assemble command is used to enter Assembly language instructions into memory. A <starting address> The starting address may be given as an offset number, in which case it is assumed to be an offset into the code segment. Otherwise, CS can also be specified explicitly. (eg. A 100 and A CS:100 will achieve the same result). When this command is entered, DEBUG will begin prompting you to enter Assembly language instructions. After an instruction is typed in and followed by <return>, DEBUG will prompt for the next instruction. This process is repeated until you type a <return> at the address prompt, at which time DEBUG will return you to the debug command prompt. Use the A command to enter the following instructions starting from offset 0100H. -A cs:100 xxxx:0100 mov ax,1 xxxx:0103 mov bx,2 xxxx:0106 mov cx,3 xxxx:0109 add ax,bx xxxx:010B add ax,cx xxxx:010D jmp 100 xxxx:010F <enter> where xxxx specifies the address of the code segment. Please note that the second instruction starts at xxxx:0103. This implies that first instruction is three bytes long.

U, the Unassemble command: looking at machine code The unassemble command displays the machine code in memory along with their equivalent Assembly language instructions. The command can be given in either format shown below: U <starting address> <ending address> U 100 10D xxxx:0100 B80100 mov ax,1 xxxx:0103 BB0200 mov bx,2 xxxx:0106 B90300 mov cx,3 xxxx:0109 01D8 add ax,bx xxxx:010B 01C8 add ax,cx xxxx:010D EBF1 jmp 100 Note: If the ending address is not specified then the U command unassembles 32 bytes beginning from the starting address. U <starting address> <L number of bytes in hex> U 100 L 0d Type in this command, check the result T, the Trace command: single-step execution The trace command allows you to trace through the execution of your programs one or more instructions at a time to verify the effect of the programs on registers and/or data. Ex1: T < =starting address> < number of instructions> T =100 5 If you do not specify the starting address then you have to set the IP using the R command before using the T command Ex2: Now try using r command to set IP to 100 and then execute T 5 Ex3: Now try using r command to set IP to 100 and then execute T Now trace through the above sequence of instructions that you have entered and examine the registers. G, the Go command The go command instructs DEBUG to execute the instructions found between the starting and stop addresses. G < = starting address > < stop address > Ex: Execute the following command and explain the result G = 100 10d Caution: the command G= 100 10e will cause the system to hang. Explain why? Often, it is convenient to pause the program after executing a few instructions, thus effectively creating a break point in the program. For example, the command g 106 tells the DEBUG to start executing the next instruction(s) and pause at offset 0106H. Try executing the sequence of instructions you entered using several options of the G command. 5

The main difference between the GO and TRACE commands is that the GO command lists the register values after the execution of the last instruction while the TRACE command does so after each instruction execution. Before proceeding to study the next command, you should reset DEBUG so that it points to the first instruction of your program. EXAMINING THE FLAG REGISTER Exit and enter DEBUG again so that we start with the default flag bits. Use the A command to enter the following program fragment beginning at address offset 0100H. mov al, 9c mov dh, 64 add al, dh Notice when DEBUG was first entered, the flag bits, except for the interrupt bit, are all cleared. So the original values are: [NV UP EI PL NZ NA PO NC]. Execute the three instructions using either the G or the T command. When the above three instructions have been executed, the flag register bits become: [NV UP EI PL ZR AC PE CY] Note: 1001 1100 + 0110 0100 = 0000 0000 . Explanation of the affected flag bits: Carry bit is set (from NC to CY ) since there is a carry beyond data bit D7. Parity bit is set (from PO to PE ) since there is an even numbers of 1s ending in al. Auxiliary bit is set (from NA to AC ) since there is a carry from data bits D3 to D4. Zero bit is set (from NZ to ZR ) since the result is zero. Sign bit is cleared (it was PL) since the result in binary representation is non-negative. Now exit, re-enter DEBUG and run the two program segments (i) and (ii) given below. Remember to begin each program at address-offset 0100H. Use the U command to ensure that the programs have been entered correctly before executing them. Trace each program and analyze the flag bits that are being affected. (i) mov ax, 34f5 add ax, 95eb (ii) mov bx, aaaa add bx, 5556 Question 2. What are the final values in AX and BX after running each program segment? List the flag the flag bits that have changed and explain how they have changed.

EXAMINING MEMORY LOCATIONS D, the dump command: examining the contents of memory The dump command is used to examine the contents of memory. The display area consists of three parts. To the left is the Hex address of the left most displayed byte. The wide area in the center is the hex representation of the displayed area. To the right is the ASCII representation of the displayed area. D < starting address > < end address > D < starting address > < L number of bytes > Question 3 Use the U and D commands to look at one of your program segments using the <L number of bytes > option. What is the difference between the two commands? COM (serial) ports and LPT (printer) ports of your PC In an 80x86-based PC, there can be as many as 4 COM ports and 4 LPT ports present. When the PC is turned on, DOS tests for each of the COM and LPT port. If the COM ports are installed, their I/O port addresses (similar to memory address) are written to memory locations 0040:0000 - 0040:0007. If the LPT ports are installed, their I/O port addresses are written to memory locations 0040:0008 - 0040:000F; otherwise zeros will be found in these memory locations. Question 4: Give the appropriate DEBUG commands that will find out the number of COM and LPT ports installed in your PC. List their port addresses. ROM BIOS The ROM BIOS of a PC is placed at memory addresses starting from F000:E000. The 8-byte memory starting at address F000:FFF5 shows the date when the ROM BIOS was programmed. Question 5 : Give the command to look at these 8-bytes. On what date was the ROM BIOS of your PC programmed? Normally D Command displays 128 bytes but in this case it displayed only 11 bytes. Explain? E, the Enter Command E address You can modify the content of any memory location in the data segment using the E command **************

BIRLA INSTITUTE OF TECHNOLOGY & SCIENCE,PILANI ES C263 Microprocessors Prog. & Interfacing LAB-3 Addressing Modes
Introduction The 80x86 processors let you access memory in many different ways. The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access variables, arrays, records, pointers, and other complex data types. Mastery of the 80x86 addressing modes is the first step towards mastering 80x86 assembly language. Various addressing modes supported by Intel 8086 processor are 1) Register Addressing 2) Immediate Addressing 3) Direct Addressing 4) Register Indirect Addressing 5) Register relative Addressing 6) Based Indexed Addressing 7) Based Indexed Displacement Addressing For all the Addressing modes consider the 8086 mov instruction Syntax : mov destination ,source Register Addressing Most 8086 instructions can operate on the 8086's general-purpose register set. By specifying the name of the register as an operand to the instruction, you may access the contents of that register. MOV AX, BX ;COPIES THE VALUE FROM BX INTO AX MOV DL, AL ;COPIES THE VALUE FROM AL INTO DL MOV SI, DX ;COPIES THE VALUE FROM DX INTO SI MOV AX, AX ;YES, THIS IS LEGAL! Immediate Addressing Source operand can be immediate data (8 bit or 16 bit data ). MOV AH,12H MOV CX,1234 H note that changes done to 8-bits of the 16-bit register does not affect the other 8-bits of the register

Segment : Offset scheme : All the addressing following this will follow the Segment:offset scheme to calculate the effective address. For given segment:offset pair ,effective address is calculated by Effective address = (segment value * 16 ) + Offset value Segment register is multiplied by 16 (or shifted one hexadecimal byte to the left)(or add an extra 0 to the end of the hex number) and then the value in an Offset register is added to it. Example : F000 : FFFD F0000 + FFFD -----FFFFD or 1,048,573(decimal) Here's another example: 923F:E2FF 923F0 + E2FF -----A06EF or 657,135(decimal) The segment and offset are grouped in the default way, but the programmer can do segment overriding to use them as they expect CS : IP - Next Instruction SS : SP ,BP -Stack Pointer, Base Pointer DS : SI,DI -Source Index, Destination Index Segment Override A segment override prefix allows any segment register (DS, ES, SS, or CS) to be used as the segment when evaluating addresses in an instruction. Segment override prefix can only be used for data access. An override is made by appending the register plus a colon to the beginning of the memory reference of the instruction as in the following examples: MOV AX, ES: [0126] ; USE ES AS THE SEGMENT MOV AX, CS: [BX] ; USE CS AS THE SEGMENT MOV AX, DS: [BP+SI+3] ; USE SS AS THE SEGMENT Direct Addressing The direct addressing, directly specifies the address as one of the operand MOV AL,DS:[8088H] loads the al register with a copy of the byte at memory location 9

DS: 8088h.Effective address is calculated using segment offset scheme MOV DS: [1234H], DL Stores the value in the DL register to memory location DS: 1234H. Effective address is calculated using segment offset scheme. Register indirect Addressing The 80x86 CPUs let you access memory indirectly through a register using the register indirect addressing modes. Register indirect addressing allows data to be addressed at any memory location through an offset address held in any following register: BX,BP,SI,DI. MOV AL, [BX] MOV AL, [BP] MOV AL, [SI] MOV AL, [DI] The [BX], [SI] and [DI] modes use the data segment by default. The [BP] addressing mode uses the stack segment (SS) by default. Segment Overriding can also be done. Example MOV AL, CS:[BX] MOV AL, DS:[BP] MOV AL, SS:[SI] MOV AL, ES:[DI] Register Relative Addressing Mode: The indexed addressing modes use the following syntax: MOV AL, DISP[BX] MOV AL, DISP[BP] MOV AL, DISP[SI] MOV AL, DISP[DI] If BX contains 1000h, then the instruction MOV CL,20H[BX] will load CL from memory location DS:1020h. Likewise, if BP contains 2020h, MOV DH,1000H[BP] will load dh from location SS:3020.Segment overriding can also be done . MOV AL, SS:DISP[BX] MOV AL, ES:DISP[BP] MOV AL, CS:DISP[SI] MOV AL, SS:DISP[DI] Base Indexed Addressing These addressing modes form the offset by adding together a base register (BX or BP) and an index register (SI or DI). The possible combinations are MOV AL,[BX+SI] ;AL will be loaded with DS:[BX+SI] 10

MOV AL, [BX+DI];AL will be loaded with DS:[BX+DI] MOV AL, [BP+SI] ; AL will be loaded with SS:[BP+SI] MOV AL, [BP+DI]; AL will be loaded with SS:[BP+SI] Suppose that BX contains 1000h and SI contains 880h. Then the instruction MOV AL,[BX+SI] would load AL from location DS:1880h. Likewise, if BP contains 1598h and DI contains 1004, MOV AX,[BP+DI] will load the 16 bits in AX from locations SS:259C and

SS:259D.

The addressing modes that do not involve BP use the data segment by default. Those that have BP as an operand use the stack segment by default. Base Register plus index addressing mode These addressing modes are a slight modification of the base/indexed addressing modes with the addition of an eight bit or sixteen bit constant. The following are some examples of these addressing modes: MOV MOV AL, DISP[BP+SI] ;AL will be loaded from address SS:DISP+BP+SI AL, 10h[BX+SI] ;AL will be loaded from address DS:DISP+BX+SI

Way to remember addressing modes There are a total of 17 different legal combination possible the 8086: DISP, [BX], [BP], [SI], [DI], DISP[BX], DISP[BP], DISP[SI], DISP[DI], [BX][SI], [BX][DI], [BP][SI], [BP][DI], DISP[BX][SI], DISP [BX][DI], DISP[BP][SI], AND DISP[BP][DI]. Easier way to remember is DISP BP or BX SI or DI

We can choose either one column or two columns or three columns to land into the addressing modes and combination we desire

11

Task1: To search for a string in a memory location. 1. Load a txt file having a string virus anywhere in the file using N and L commands of debug. N Names a program or a file you intend to read or write onto disk. Format of command N path:name.ext 2. Load File into memory using L command. L Loads a File or Disk Sectors into memory. L 100 will load the file specified by N into CS:100. 3. Examine contents of CS:80 What is present there? 4. Search for string virus using S command. S Searches memory for a string. Format S start-addr L val data Format S start-addr end-addr data Task2: To use debug to create a very small machine language program that you save on disk as a file. 1. Store data id DS:0200 using E command. 2. Using A command write a small ALP that will transfer the contents of this memory location into AX,BX,CX,DX,SI,DI registers. 3. Use N command to create a file. (Note: The executable file that you will create will have .com extension) 4. Enter value 0 in BX and size of program in CX 5. Using W command now write executable program. Format W address 6. Exit Debug. 7. Open Debug with name of file.com and trace through the program and examine contents of registers. Task3: Move a string from location DS:500 to DS:600 Explore M command. Task4: What happens if you type H 14F 22? Task5: Fill a set of memory location with ASCII character H Explore F command. 12

Das könnte Ihnen auch gefallen