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

```assume cs:code, ds:data

data segment
x db 1, 9, 3
len1 equ \$ - x

y dw 1, 9, 3
len2 equ \$ - y
data ends

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

mov si, offset x
mov cx, len1
mov ah, 2
s1:mov dl, [si]
or dl, 30h
int 21h

mov dl, ' '
int 21h

inc si
loop s1

mov ah, 2
mov dl, 0ah
int 21h

mov si, offset y
mov cx, len2/2
mov ah, 2
s2:mov dx, [si]
or dl, 30h
int 21h

mov dl, ' '
int 21h

loop s2

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

Running screenshot ① 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: disassemble and check the machine code. The machine code corresponding to loop s1 is E2F2, E2 represents loop, F2 is the displacement, which is converted into binary, i.e. 1111 0010. This is the complement. The original code is 1000 1110, i.e. decimal number - 14, so the displacement is - 14001B, i.e. 27-14 = 13, i.e. 000D. It is confirmed that the displacement is - 14.

From the perspective of cpu, ip starts at 0019, i.e. 25. After executing the loop instruction, + 2, i.e. 001B, i.e. 27, shifts - 14, i.e. 27 + (- 14) = 13, i.e. 001D, and ip is 001D, i.e. at s1 label.

② line44, when the assembly instruction loop s2 jumps, it jumps according to the displacement. Through debug disassembly, check its machine code and analyze the jump displacement? (the displacement value is answered in decimal) from the perspective of CPU, explain how to calculate the offset address of the instruction after the jump label s2. A: disassemble and check the machine code. The machine code corresponding to loop s2 is E2F0, E2 represents loop, F0 is the displacement, which is converted into binary, i.e. 1111 0000. This is the complement. The original code is 1001 0000, i.e. decimal number - 16, so the displacement is - 160039, i.e. 57-16 = 41, i.e. 0029. It is confirmed that the displacement is - 16.

From the perspective of cpu, ip starts at 0037, i.e. 55. After executing the loop instruction, + 2, i.e. 0039, i.e. 57, displaces - 16, i.e. 57 + (- 16) = 41, i.e. 0029, and ip is 0029, i.e. at the s2 label.

③ Attach the disassembly screenshot of debugging observation in debug during the above analysis  ```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:, offset s1
mov word ptr ds:, offset s2
mov ds:, cs

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

call word ptr ds:
s1: pop ax

call dword ptr ds:
s2: pop bx
pop cx

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

After analysis, debugging and verification, register (ax) = (bx) = (cx) =? Attach the screenshot of debugging result interface.

① 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) =?

ax is the offset address 0021 of s1, bx is the offset address 0026 of s2, and cx is cs, 076C.

Analysis: when call word ptr ds: is executed, ip is the starting address of the next instruction, that is, ip is the starting address of s1 code segment. At this time, first press cs into the stack, and then press ip=0021 into the stack. Then the instruction jumps to ds: The stored address, that is, the s1 label, performs the stack out operation and assigns 0021 in the stack to ax. Therefore, the value of ax is the offset address of s1. At this time, there is a current cs value left in the stack.

When call word ptr ds: is executed, ip is also the starting address of the next instruction, that is, ip is the starting address of s2 code segment. At this time, first press cs into the stack, and then press ip=0026 into the stack. Then the instruction jumps to ds: The stored address, that is, at the s2 label, performs the stack out operation and assigns 0026 in the stack to bx. Therefore, the value of bx is the offset address of s2. At this time, there are two current cs values left in the stack.

At this time, if you perform the stack out operation again, the value of the current cs will be assigned to cx, so cx stores the current cs.

② 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. Answer: the debugging results are consistent with the theoretical analysis results.

assume cs:code, ds:data
data segment
x db 99, 72, 85, 63, 89, 97, 55
len equ \$- x
data ends
stack segment
db 16 dup(0)
stack ends
code segment
start:
mov ax,data
mov ds,ax
mov cx,len
mov si,offset x
s:
mov al,[si]
mov ah,0
call printNumber
call printSpace
inc si
loop s
mov ah,4ch
int 21h
printNumber:
mov dl,10
div dl
mov bx,ax
mov dl,bl
or dl,30h
mov ah,2
int 21h
mov ah,2
mov dl,bh
or dl,30h
int 21h
ret
printSpace:
mov dl,' '
mov ah,2
int 21h
ret
code ends
end start

Screenshot of running test ```assume cs:code,ds:data
data segment
str db 'try'
len equ \$ - str
data ends
stack segment
db 16 dup(0)
stack ends
code segment
start:
mov ax,data
mov ds,ax
mov ax,0b800h
mov es,ax

mov si,offset str
mov cx,len
mov bl,2
mov bh,0
call printStr

mov si,offset str
mov cx,len
mov bl,4
mov bh,24
call printStr

mov ah,4ch
int 21h

printStr:
push bx
mov al,bh
mov bl,160
mul bl
mov di,ax
pop bx
s:
mov al,[si]
mov es:[di],al
mov es:[di+1],bl
inc si
loop s
ret

code ends
end start```

Screenshot of running test ```assume cs:code,ds:data
data segment
stu_no db '201983290262'
len = \$ - stu_no
len1 equ 40-len/2;
data ends
stack segment
db 16 dup(0)
stack ends
code segment
start:
mov ax,data
mov ds,ax
mov ax,0b800h
mov es,ax
mov bl,17
call setBackground
mov bl,23
call printstuno
mov ah,4ch
int 21h

setBackground:
mov di,0
mov ax,0
mov al,25
mov dx,0
mov dl,80
mul dl
mov cx,ax
mov al,20h
s:
mov es:[di],al
inc di
mov es:[di],bl
inc di
loop s
ret

printstuno:
mov ax,0b800h
mov es,ax
mov ax,0
mov al,24
mov dx,0
mov dl,160
mul dl
mov di,ax
mov cx,len1
s1:
mov byte ptr es:[di],'-'
mov es:[di+1],bl
loop s1
mov si,0
mov cx,len
s2:
mov al,ds:[si]
mov es:[di],al
mov es:[di+1],bl
inc si
loop s2
mov cx,len1
s3:
mov byte ptr es:[di],'-'
mov es:[di+1],bl
loop s3
code ends
end start```

Screenshot of running test Experimental summary (optional)

1. When there is a problem operating must have size during assembly, the content of the corresponding error reporting statement shall be clearly transmitted as word or byte, that is, the size can be specified through assembly.
2.or register, 30h, means to convert the contents of this register into the corresponding ascall code, because when function 2 calls dl content to display characters, it must be ascaII code.

Posted by ereptur on Sun, 28 Nov 2021 07:31:14 -0800