Analysis of Experiment 8 in Assembly Language

“Assembly Language”, 3rd Edition by Wang Shuang

Chapter 9: Principles of Transfer Instructions (Page 187)

Experiment 8 Analysis of a Strange Program

Analyze the program below and think before running: Can this program return correctly?

After running, think: Why is the result like this?

This program deepens the understanding of related content.

;"Assembly Language", 3rd Edition by Wang Shuang; Chapter 9: Principles of Transfer Instructions (Page 187); Experiment 8: Analysis of a Strange Programassume cs:codesgcodesg segment         mov ax, 4c00H         int 21H  start: mov ax, 0      s: nop         nop
         mov di, offset s         mov si, offset s2         mov ax, cs:[si]         mov cs:[di], ax
     s0: jmp short s
     s1: mov ax, 0         int  21H         mov ax, 0
     s2: jmp short s1         nopcodesg endsend start        

=============

Reference Answer

It can return correctly.

After compiling and debugging the code, it indeed returns normally.

Reason analysis is as follows:

1. The program starts executing from the start label. The four mov instructions after the two nops actually copy the machine code corresponding to the jmp short s1 at s2 into the two nop bytes at label s (as can be seen from the screenshot below, the offset address of the first byte after the jmp instruction at s2 is 22H, and the offset address at s1 is 18H, so the displacement from s2 to s1 is: address of label s1 – address of the first byte after the jmp instruction = 18H – 22H = -0AH = -10, and its two’s complement is F6).

Analysis of Experiment 8 in Assembly Language

2. From the diagram below, after executing the four mov instructions, the machine code at offset address 076C:0008, which is the machine code at label s, becomes EBF6. The next jmp instruction jumps to label s to execute. The code at s is EBF6, which means jump forward 10 bytes, that is, the offset address of the instruction following s (0AH) minus the target offset address, which is exactly 10 (that is, the target offset address minus the address of the first byte after the jmp instruction at label s should equal -10), and 0A – 0 = 10, thus the program jumps to 0000H to execute, where the code at 0000H is exactly the correct program return code, so the program can return normally.

Analysis of Experiment 8 in Assembly Language

Summary:

This experiment examines the knowledge point of the jmp instruction based on displacement.

The code given in the question, at first glance, seems that the correct return code given at the start label will not be executed, and the int 21H after the start label seems also cannot return correctly because the previous line is mov ax, 0 instead of mov ax, 4c00H. However, because the jump from s2 to s1 and the jump from s to mov ax, 4c00H have the same displacement of -10, placing the code from s2 at s allows the program to return correctly.

Finally, attached is the process tracked using the t command, as shown in the figure below. The program indeed returns normally.

Analysis of Experiment 8 in Assembly Language

Leave a Comment