# Experiment 3 transfer instruction jump principle and its simple application programming

Source code:

```assume cs:code, ds:data
data segment
x db 1, 9, 3
len1 equ \$ - x ; symbolic constants , \$Refers to the offset address of the next data item. In this example, it is 3
y dw 1, 9, 3
len2 equ \$ - y ; symbolic constants , \$Refers to the offset address of the next data item. In this example, it is 9
data ends
code segment
start:
mov ax, data
mov ds, ax
mov si, offset x ; Take symbol x Corresponding offset address 0 -> si
mov cx, len1 ; From symbol x Number of consecutive byte data items at the beginning -> cx
mov ah, 2
s1:mov dl, [si]
or dl, 30h
int 21h
mov dl, ' '
int 21h ; Output space
inc si
loop s1
mov ah, 2
mov dl, 0ah
int 21h ; Line feed
mov si, offset y ; Take symbol y Corresponding offset address 3 -> si
mov cx, len2/2 ; From symbol y Number of consecutive word data items started -> cx
mov ah, 2
s2:mov dx, [si]
or dl, 30h
int 21h
mov dl, ' '
int 21h ; Output space
loop s2
mov ah, 4ch
int 21h
code ends
end start
```

Operation screenshot:

Question 1:

line27, when the assembly instruction loop s1 jumps, it jumps according to the displacement. Check the machine code through debug disassembly and analyze the jump displacement? (the displacement value is answered in decimal) from the perspective of the CPU, explain how to calculate the offset address of the instruction after the jump label s1.

A: the machine code of loop s1 is E2F2. The displacement of jump is - 14.

The offset address of the first byte after the loop instruction is: 001B, while the offset address at label s1 is: 000D, displacement = 13 (000D) - 27 (001B) = - 14 (the complement is expressed as F2). Therefore, the jump displacement is - 14.

Question 2:

line44. When the assembly instruction loop s2 jumps, it jumps according to the displacement. Check the machine code through debug disassembly and analyze the jump displacement? (the displacement value is answered in decimal) from the perspective of the CPU, explain how to calculate the offset address of the instruction after the jump label s2.

A: the machine code of loop s2 is E2F0. The displacement of jump is - 16.

The offset address of the first byte after the Loop instruction is 0039, while the offset address at label s2 is 0029, and the displacement = 41 (0029) - 57 (0039) = - 16 (the complement is expressed as F0). Therefore, the jump displacement is - 16.

Question 3:

Disassembly:

Source code:

```assume cs:code, ds:data

data segment
dw 200h, 0h, 230h, 0h
data ends

stack segment
db 16 dup(0)
stack ends

code segment
start:
mov ax, data
mov ds, ax

mov word ptr ds:[0], offset s1
mov word ptr ds:[2], offset s2
mov ds:[4], cs

mov ax, stack
mov ss, ax
mov sp, 16

call word ptr ds:[0]
s1: pop ax

call dword ptr ds:[2]
s2: pop bx
pop cx

mov ah, 4ch
int 21h
code ends
end start
```

Question 1:

According to the jump principle of call instruction, it is analyzed theoretically that before the program executes to exit (line31), register (ax) =? Register (bx) =? Register (cx) =?

Theoretical analysis:

Line13-line18: indicates that the offset address of s1, the offset address of s2 and cs, that is, the address of the current preceding segment, are put into the data segment, each accounting for 2 bytes, that is, 1 word.

Line20-lin22: indicates that the stack segment is set as the stack segment.

Line24: indicates that the current IP is put into the stack first, and the current IP is the offset address at s1, and then the intra segment transfer is performed. After execution, IP=ds:[0], that is, the offset address 0021 of s1, jump to the transferred IP, that is, execute s1, and get the elements in the current stack out of the stack, and the current stack is the offset address of s1, so ax=0021H.

Line27: since dword represents a double word, this instruction indicates that the current CS is stacked first, and then the current IP is stacked. The current CS and IP are CS (076C) and IP (0026) at s2, and then the inter segment transfer is performed. After execution, CS=ds:[4], that is, 076C, IP=ds:[2] , that is, the offset address 0026 at s2. After jumping to this position, execute s2 to get the elements in the current stack out of the stack, that is, bx=0026.

Line29: get the elements in the current stack out of the stack, that is, cx=076C.

Question 2:

Assemble and link the source program to get the executable program task2.exe. Use debug to observe and verify whether the debugging results are consistent with the theoretical analysis results.

The results are consistent with the theory.

Source code:

```assume cs:code, ds:data
data segment
x db 99, 72, 85, 63, 89, 97, 55
len equ \$- x
data ends
code segment
start:
mov ax,data
mov ds,ax
mov si,0
mov cx,7
s0:
call printNumber
call printSpace
loop s0
mov ax,4c00h
int 21h

printNumber:
mov al,[si]
mov ah,0
mov bl,10
div bl
mov dl,al
mov dh,ah
mov ah,2
or dl,30h
int 21h
mov dl,dh
or dl,30h
int 21h
inc si
ret

printSpace:
mov ah,2
mov dl,' '
int 21h
ret
code ends
end starts```

Source code:

```assume cs:code,ds:data
data segment
str db 'try'
len equ \$ - str
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0b800h
mov es,ax

mov bh,0
mov bl,2h
call printStr

mov bh,24
mov bl,4h
call printStr

mov ax,4c00h
int 21h

printStr:
mov si,0
mov ah,0
mov al,bh
mov dx,160
mul dx
mov di,ax
mov cx,3
s0:
mov al,[si]
mov es:[di],al
mov es:[di+1],bl
inc si
loop s0
ret

code ends
end starts```

Experimental results:

Source code:

```assume cs:code
data segment
stu_no db '201983290171'
len = \$ - stu_no
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0b800h
mov es,ax
call printBackground
mov bh,24
mov al,160
mul bh
mov bx,ax
call printChar
call printNum
call printChar

mov ax,4c00h
int 21h

printBackground:
mov si,1
mov bl,17h
mov cx,2000
s0:
mov es:[si],bl
loop s0
ret

printChar:
mov al,'-'
mov cx,34
s1:
mov es:[bx],al
loop s1
ret

printNum:
mov di,0
mov cx,12
s2:
mov al,ds:[di]
mov es:[bx],al
inc di
loop s2
ret

code ends
end start
```

Experimental results:

## Experimental summary

Through this experiment, I have a deeper understanding and understanding of the principle of transfer instruction in Chapter 9 and call and RET instructions in Chapter 10. In the experiment on Chapter 9, I have deepened my understanding and operation of operator offset, multiple execution modes of jmp instruction and multiple execution modes of loop instruction, and I fully recognize the content of Chapter 10 When I realized the combined use of call and RET instructions, I could make the program more understandable and convenient, and I also learned how to use mul instructions. By learning experiment 9 in books and programming according to materials, I also learned how to display data in various places on the screen and change the attributes of fonts.

Through this experiment, I also reviewed the relevant knowledge I had learned before, which really benefited me a lot.

Posted on Tue, 30 Nov 2021 15:04:38 -0500 by sigmon