Using the Microsoft Visual Studio 2005 Debugger |
Updated 10/11/2006
This tutorial shows how to begin using the Microsoft Visual Studio .NET Debugger to debug 32-bit assembly language programs running in Protected mode. This debugger lets you do the following:
This will be a hands-on tutorial, so you can learn the most if you follow each step. Open the Visual Studio Project fileIn this step-by-step tutorial, you will open the sample project and insert an example program named AddSub2.asm.
.data val1 DWORD 10000h val2 DWORD 40000h val3 DWORD 20000h finalVal DWORD ? .code main PROC mov eax,val1 ; start with 10000h add eax,val2 ; add 40000h sub eax,val3 ; subtract 20000h mov finalVal,eax ; store the result (30000h) call DumpRegs ; display the registers exit main ENDP END main Build the project by selecting Build Solution from the Build menu. Start the DebuggerPress the F10 key to start the debugger. Your environment should appear as follows, except that your window configuration may be different:
Don't worry if some of the windows shown here do not appear on your screen. You can open and close them at will. First however, let's take a tour: Source WindowThe Source window displays the program's source file. Note that the first MOV statement has a yellow arrow next to it. The arrow always indicates the next statement that's about to execute.
Watch WindowSelect Windows from the Debug menu, and select Watch 1. A Watch window is like an electronic spreadsheet that displays the names and values of selected variables. As you step through a program, you can see variables in this window change value. Currently the window is empty, but you can drag any program variable into the window with your mouse. Click on the tab at the bottom of the screen labeled Watch 1 to bring it to the front. Drag the val1, val2, and finalVal variables into the Watch window and note their current values. The values are currently displayed in decimal, so select hexadecimal format by right-clicking on the watch window and selecting Hexadecimal Display from the popup menu. Following is a sample: Memory WindowSelect Windows from the Debug menu, and select Memory. The Memory window displays a raw dump of memory in either hexadecimal or decimal. It is particularly useful when working with array variables. Since we don't have any arrays in the program, let's display the value of val3. Next to the Address label, type: &val3 The & before the variable name means to interpret the variable name as an address. In assembly language, all labels are implied addresses. Variable names are case-sensitive in the debugger. The Memory window displays a series of individual memory bytes, beginning at the address of val3. Right-click on the window, and select 4-byte Integer. Along the left side of the window is shown the address of the first value in each line: In our sample, val3 is located at address 00405008, although your addresses might be different. You can also type a hexadecmal constant into the Address field. Use the C-language format, where the digits are preceded by "0x". For example: 0x0040400C. (The Memory window resets its address to a default value when you restart the debugger.) Register WindowSelect Windows from the Debug menu, and select Register. The Register window displays the contents of the CPU registers. The flag values do not show by default, but you can add them in by right-clicking and selecting Flags:
Disassembly WindowSelect Windows from the Debug menu, and select Disassembly. The Disassembly window displays a raw listing of the program's code. In some programs, it will show code generated by macros or by high-level MASM directives (such as .IF, in Chapter 6). Right-click the disassembly window and note the various display possibilities (your list may vary slightly): Here is a sample, in which Show Source Code is disabled and Show Symbol Names is enabled: --- AddSub2.asm ---------------------------------------------------------------- The offset of each variable apears next to its name. The exit statement in the source code listing has been translated into a call to the MS-Windows ExitProcess function. This is explained more fully at the bottom of page 75 in the book. Step Over (F10)Let's step through the program using the F10 function key. As you press F10, watch the yellow arrow in the Source window move from statement to statement. Notice the following as you step through the program:
As soon as you step past the call DumpRegs statement, click on the black MS-DOS icon on the Taskbar at the bottom of the screen. You should see the program's output, which is a register display. When you reach the exit statement and press F10, the debugger halts. Step Into (F11)Another way to step through a program is to use the Trace (F11) command. It steps down into procedure calls. In contrast, the F10 key just executes procedure calls without tracing into the procedure code. Stopping and RestartingIt's easy to either stop or restart your program inside the debugger while you're in the process of stepping through a program:
TipsBe sure to stop the debugger before trying to modify and re-assemble your program's source code. Otherwise, the linker will refuse to assemble your EXE file, indicating that it's currently in use:
The End
|