DEBUGGING A PROGRAM

DEBUGGING A PROGRAM

b-zilog-z80-cpu

Debugging a program is similar to troubleshooting hardware, but it is much more difficult and cumbersome. When a program fails to work, very few clues alert you to what exactly went wrong. Therefore, it is essential to search carefully for the errors in the program logic, machine code, and execution.

The debugging procedure can be divided into two parts: static debugging and dynamic debugging. Static debugging is similar to visual inspection of a circuit board; it is the paper-and-pencil check of a flowchart and machine code. Dynamic debugging involves observing out puts; register contents, and flags following the execution of either instruction (the single-step technique) or a group of instruc­tions (the breakpoint technique).

Static Debugging of Machine Code

Translating the assembly language into the machine code is similar to building a circuit from a schematic in that the machine code will have errors just as would the circuit board. If an assembler is used to translate the code, most of the errors involved in hand assembly can be eliminated. The following errors are common in manual assembly:

I. Selecting a wrong code.

1. Forgetting the second byte or third byte of an instruction.

2. Specifying the wrong jump location.

3. Not reversing the order of high and low bytes in a Jump instruction.

4. S. Writing memory addresses in decimal, thus specifying wrong jump locations.

The debugging problems given in the Assignments section at the end of the chapter will illustrate some of these errors.

Dynamic Debugging

Dynamic debugging is concerned with observations of data after executing an instruction or a set of instructions. These observations may include verifying out­put displays, checking flags, examining register contents, and tracing execution flow. The process is similar to that of the signal-injection technique in trouble­shooting analog circuits, which involves injecting a signal into a hardware system (such as an amplifier) and checking signals at various points against the expected outputs. Similarly, in debugging programs, we execute a few instructions and check register contents or outputs against the expected results. The commonly used techniques and tools are (1) Single Step, (2) Register Examine, and (3) Breakpoint.

SINGLE STEP

The single step technique allows us to execute one instruction at a time and to observe the results following each instruction. As we advance through each in-striation, we will be able to observe memory addresses and codes as they are executed. With the single step technique, we can spot

· Incorrect addresses.

· Incorrect jump locations for loops.

· Incorrect data or missing codes.

This technique is generally used in conjunction with the Register Examine facility (described below), and it is very useful for short programs (50-100 machine codes). For larger programs, the technique is cumbersome and time consuming.

REGISTER EXAMINE

The Register Examine facility allows us to examine the contents of the micropro­cessor registers and the flags. We can examine registers after the execution of each instruction or after the execution of a group of instructions and compare the contents with the expected outcomes.

BREAKPOINT

The breakpoint technique allows us to check the program in segments. We can set a breakpoint at the end of a program segment or multiple breakpoints at vari­ous memory locations. When the microprocessor is asked to execute the program, it executes the codes until it comes across the first breakpoint, where it returns the control to the breakpoint subroutine in the system. At this point, we can ex­amine the registers· for expected results. If the segment of the program is found satisfactory, the program can be executed up to the next breakpoint. With the breakpoint technique, we can isolate the segments of the programs with errors and debug those segments with the single step technique. The breakpoint tech­nique is generally used to check out timing loops, I/O sections, and interrupts.

COMMON SOURCES OF ERRORS

In addition to the errors mentioned in Section other common errors in the types of programs discussed in this chapter are:

· Failure to clear the accumulator when it is used to add data.

· Failure to clear registers when they are used to store partial results or carries.

· Failure to update an index or a counter.

· ·Failure to set a flag before using a conditional Jump instruction or use of an inappropriate flag.

· Inadvertently changing a flag before using a Jump instruction.

Leave a comment

Your email address will not be published. Required fields are marked *