Debugging (masm, link, debug) simple assembly language programs under DOSBox-ic

Debugging (masm, link, debug) simple assembly language programs under DOSBox-ic

There was an assembly experiment some time ago, and I briefly learned the usage format and functions of the basic instructions of assembly language. Including the installation and use of DOSBox, several commonly used instructions, compiling a few simple assemblers, executing programs, viewing registers, and learning the design process of assembly language.

Preparation tools

Required tools-DOSBox0.74-win32, debug.exe, LINK.EXE, MASM.EXE.

Install DOSBox

  • There is nothing to say about this, just run the installation directly. Then click Close. Then run the program.

Create virtual C drive

1. debug.exe LINK.exe MASM.exeThe three XXXX into a disk folder (mine is on the d:/test), then enter the following command in the program

mount c d:/test 

2. Enter c:the virtual c drive. Then enter to dirconfirm whether the three programs are in it. The returned results are as follows.

3. Create a notepad file in the same directory of the three programs . The suffix is XXX.asm. (I created t2.asm) Ready to write the first assembly program.

Sample assembler

Question: Assuming (AX)=0025H, the logic is shifted to the left by 2 bits.

code segment
assume cs:code
s:mov ax,0025h
  mov cl,2
  shl ax,cl

  mov ax,4ch
  int 21h
code ends
end s 
  • Note mov ax,4ch int 21hthat the exit code of the program. Avoid entering an endless loop.

4. Then enter it in the dosbox masm t2.asm. Then press Enter several times. Finally, it will show whether there are errors. If it is corrected according to the wrong position, execute the command again. Until there are no errors. Go back to the directory and you can see the generated T2.OBJfile.

5. Then execute link t2.obj(not case sensitive) multiple times to enter, and finally generate a t2.exefile.

6. Execute the debug t2.execommand. Enter. Perform debugging. Enter ucommands (the role of each command is explained in detail at the end of the article). The machine code is disassembled and displayed. As shown below.

7. Execute the g 0007command. Execute the program to address 0007. (Why to 0007? Because when the assembly command is executed, the command from the previous address is executed to 0007. The code at address 0007 will not be executed.) The result is as shown in the figure below. AX=94This is the final result of the procedure.

There are many simple exercises below. The steps are the same. I won't introduce them one by one. Fold up the code later. If you are interested, you can try to practice.

Assembler exercise

  • Assuming (AX)=3333H, (BX)=1111H, find the difference between the two, and check the value of the flag register

Exercise one


  • Assuming (AX)=FFFFH, (BX)=0001H, find the sum of the two, and check the value of the flag register

Exercise two


  • Assuming (AX)=0035H, (CX)=000FH, find the two to perform the AND operation

Exercise three


  • Program to realize the exchange of the contents of the two storage units (assuming that the following data is stored in the known data segment: [0100]=12H, [0110]=34H)

Exercise four


The role of all commands

R command

R command function: watch and modify the value of the register.

Enter the following command at the prompt "-": R. DEBUG will display the current status of all registers and flags.

Then enter the command RCX. Enter 100 after the prompt ":". The function of this command is to set the value of register CX to 100 (Note: DEBUG uses hexadecimal, where 100 is equivalent to 256 in decimal.)

Finally, execute the R command to view the modified register value.

H command

H command function: Calculate the sum and difference of two hexadecimal numbers.

Enter the following command at the prompt " ": H 10 1. Watch the command execution result.

The first number of the running result is the calculated sum, and the latter number is the calculated difference. The calculation results are all expressed in hexadecimal format.

D command

D command function: display the contents of the memory area.

Execute commands R, D, D continuously under the prompt " ". Watch the command execution result.

As mentioned earlier, the function of the command R is to display the value of the current register. The function of command D is to display the contents of the memory area, the leftmost is the starting address of the memory, the middle displays the memory value in hexadecimal format, and the far right is the ASCII code. Each line can display up to 16 bytes of content.

Command D can take parameters or omit parameters. Set the value of DS when DEBUG is started as X. When the parameter is omitted, the display content of command D starts with X: 100, and 128 bytes of content are displayed each time. When the command D without parameters is executed in the future, DEBUG will continue to display according to the last position.

With parameters, DEBUG can display the contents of the specified address range. There are three ways with parameters:

  • Way one: d [starting position]. DEBUG displays 128 bytes of content from the start position. Execute the command D 1AF5:100 at the prompt "-". Watch the command execution result.
  • Method two: d [start position] [end position]. DEBUG is displayed from the start position to the end position. Execute the command D DS:100
    1FF at the prompt "-" . Watch the command execution result.
  • Method 3: d [starting position] [L length], the length is identified by the L parameter. DEBUG displays the content of the specified length from the start position. Execute the command D
    DS:100 L10 at the prompt "-" . Watch the command execution result.

E command

E command function: change the content of the memory unit.

The use of E command is: E [start position].

Enter the following command at the prompt "-": E 1AF5:100.

DEBUG first displays the content of [1AF5:0000] 00. At this time, the value of this byte can be modified. If you want to modify the subsequent content, you can press the space bar to continue. When you want to skip a byte, you can press two consecutive spaces to jump to the next byte

F command

F command function: use the specified value to fill the address in the specified memory area.

The use of the F command is: F [range] [fill list].

Enter the following command at the prompt "-": F 1AF5:100 L20 1 2 3 4 5. Execute command D 1AF5:100 to watch the command execution result.

Explanation: This command uses byte sequence 01, 02, 03, 04, 05 to fill the memory area with a length of 20H starting from 1AF5:100 in turn.

Enter the following command at the prompt "-": F 1AF5:100 13F 41 42 43 44.

Explanation: This command uses byte sequence 41, 42, 43, 44 to fill the memory area from 1AF5:100 to 1AF5:13F in turn.

M command

M command function: copy the data in the specified memory area to the specified address.

The use of M command is: M [range] [specified address].

Enter the following command at the prompt "-": M 1AF5:100 13F 1AF5:140. Execute command D 1AF5:100 to watch the command execution result.

C command

C command function: compare the contents of two memory blocks.

The use of C command is: C [range] [specified address], which means to compare the memory area of the specified range with the memory area of the same length starting from the specified address byte by byte, and list different contents.

Enter the following command at the prompt "-": C 1AF5:100 13F 1AF5:140. Since the contents of the two blocks are exactly the same, there is no display after the command is executed.

Enter the following command at the prompt "-": C 1AF5:100 107 1AF5:180, the length of the compared area is 8 bytes. After the command is executed, the bytes with different comparison results are listed.

S command

S command function: search for the specified string in the specified memory area.

The use of S command is: S [range] [specified string].

Enter the following command at the prompt "-": D 1AF5:100 11F. Display the memory value of this area.

Enter the following command at the prompt "-": S 1AF5:100 11F 41 42 43 44. Search for the byte string 41 42 43 44 in this area, and list the search results one by one.

As can be seen from the execution results, a total of eight locations were searched.

A command

A command function: input assembly instructions.

G command

G command function: execute assembly instructions.

The usage of G command is: G [=start address] [breakpoint address], which means to execute from the start address to the breakpoint address. If you don't set a breakpoint, the program will run until the abort instruction is stopped.

U command

U command function: disassemble and display the machine code.

The usage method of U command is: U [range]. If only the start address is entered in the range parameter, only the 20H byte machine code will be disassembled. Execute command U100 and watch the disassembly result.

Execute command U100 10B and watch the disassembly result. The function of this command is to disassemble the machine code from 100 to 10B.

N command

N command function: set the file name to prepare for saving the assembler just written.

W command

W command function: write a file or a specific sector to the disk.

In the example "Using the N command", we have experimented how to use the W command to save a file.

  • Before you have a good grasp of assembly language and disk file system, do not use the W command to write disk sectors for the time being, otherwise it is easy to damage the disk file or even the entire disk file system.

L command

L command function: read the file or sector content from the disk into the memory.

To load a file into the memory, you must first use the DEBUG N command to set the file name.

T command

T command function: execute assembly program, single-step tracking.

The use of T command is T [=address] [number of instructions]. If the "address" is omitted, the T command starts to run from CS:IP. The "number of instructions" is the number of instructions to be executed in a single step.

P command

P command function: execute assembly program, single-step tracking. The difference with the T command is that the P command will not track into subroutines or soft interrupts.

The use of the P command is exactly the same as the use of the T command.

I command

I command function: read data from the computer input port and display it.

The usage of the I command is I [port address]. For example, the command to read data from port 3F8 and display it is: I 3F8.

O command

O command function: send data to the computer output port.

The usage of the O command is O [port address] [byte value]. For example, the command to send data 20H to port 278 is: I 278 20.

Q command

The function of the Q command is to exit DEBUG and return to the DOS state.

  • The effect of the above command is taken from: jack_