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

``` 1 assume cs:code, ds:data
2
3 data segment
4     x db 1, 9, 3
5     len1 equ \$ - x
6
7     y dw 1, 9, 3
8     len2 equ \$ - y
9 data ends
10
11 code segment
12 start:
13     mov ax, data
14     mov ds, ax
15
16     mov si, offset x
17     mov cx, len1
18     mov ah, 2
19  s1:mov dl, [si]
20     or dl, 30h
21     int 21h
22
23     mov dl, ' '
24     int 21h
25
26     inc si
27     loop s1
28
29     mov ah, 2
30     mov dl, 0ah
31     int 21h
32
33     mov si, offset y
34     mov cx, len2/2
35     mov ah, 2
36  s2:mov dx, [si]
37     or dl, 30h
38     int 21h
39
40     mov dl, ' '
41     int 21h
42
44     loop s2
45
46     mov ah, 4ch
47     int 21h
48 code ends
49 end start```

## ① 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.

debug disassembly:

(1) The machine code of the loop instruction is E2F2, and the 8-bit shift of the jump = the address at the label - the address of the first byte after the loop instruction.

Here, the address at the label is s1, and the address of the first byte after the loop instruction is 076B:000D. The address of the next instruction after the loop instruction is 076B:001B.

Therefore, 8-bit displacement = 000D-001B=-0E (hexadecimal) = - 14 (decimal), and the hexadecimal complement is F2.

(2) The CPU calculates the offset address (IP) after the jump through the machine code of the loop instruction and the current IP.

Machine code E2F2, where E2 indicates that this command is a loop instruction, and F2 is the hexadecimal complement form of displacement.

When the CPU reads and executes E2F2, the current IP=001B (0019 + 0003), and after jump, IP = current IP + displacement = 001B-0E=000D.

## ② 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.

debug disassembly:

(1) The machine code of the loop instruction is E2F0, and the 8-bit shift of the jump = the address at the label - the address of the first byte after the loop instruction.

Here, the address at the label is s2, and the address of the first byte after the loop instruction is 076B:0029. The address of the next instruction after the loop instruction is 076B:0039.

Therefore, the 8-bit displacement = 0029-0039 = - 10 (hexadecimal) = - 16 (decimal), and the hexadecimal complement is F0.

(2) The CPU calculates the offset address (IP) after the jump through the machine code of the loop instruction and the current IP.

Machine code E2F0, where E2 indicates that the command is a loop instruction, and F0 is the hexadecimal complement of the displacement.

When the CPU reads and executes E2F0, the current IP=0039 (0037 + 0002), and after jump, IP = current IP + displacement = 0039-10 = 0029.

``` 1 assume cs:code, ds:data
2
3 data segment
4     dw 200h, 0h, 230h, 0h
5 data ends
6
7 stack segment
8     db 16 dup(0)
9 stack ends
10
11 code segment
12 start:
13     　　mov ax, data
14     　　mov ds, ax
15
16     　　mov word ptr ds:[0], offset s1
17     　　mov word ptr ds:[2], offset s2
18     　　mov ds:[4], cs
19
20     　　mov ax, stack
21     　　mov ss, ax
22    　　 mov sp, 16
23
24    　　 call word ptr ds:[0]
25 s1: 　　pop ax
27     　　call dword ptr ds:[2]
28 s2: 　　pop bx
29     　　pop cx
30
31     　　mov ah, 4ch
32     　　int 21h
33 code ends
34 end start```

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

(1) call word ptr memory unit is equivalent to

push IP

jmp word ptr memory unit address

(2) call dword ptr memory unit is equivalent to

push CS

push IP

jmp dword ptr memory unit address

(3) The IP of s1 is stored in ds:[0], and the IP of s2 is stored in ds:[2]. The first call instruction first pushes the IP of s1 into the stack, and then jumps to ds:[0], that is, s1 to execute pop ax, that is, s1's IP=0021 is out of the stack and stored in ax.

(4) The second call instruction first pushes the CS at s2 into the stack, then pushes the IP at s2 into the stack, and then jumps to ds:[2], that is, execute pop bx at s2, that is, get the IP:0026 of s2 out of the stack and store it in bx, and then execute pop cx, that is, get the CS:076C of s2 out of the stack and store it in cx.

To sum up, the program is executed before line31, ax=0021, bx=0026, cx=076C

## Use debug to debug, observe and verify the debugging results

Run to line 31 before -g 076C:0028

It can be seen that ax=0021, bx=0026, cx=076C, which is consistent with the theoretical value.

Write the 8086 assembly source program task3.asm to output this group of continuous data in the data section in decimal form on the screen, and the data is separated by spaces.

``` 1 assume cs:code, ds:data
2
3 data segment
4         x db 99, 72, 85, 63, 89, 97, 55
5         len equ \$ - x
6 data ends
7
8 code segment
9 start:
10         mov ax, data
11         mov ds, ax
12         mov si, offset x
13         mov cx, len
14         mov byte ptr ds:[8],10 ;Use a memory unit to store divisor 10. Be careful not to use the used memory space
15
16 s:      mov ah, 0
17         mov al, [si]
18         div byte ptr ds:[8] ;Divisor x The remainder of division 10 and the existence of commercial insurance ax in
19         call printNumber
20         call printSpace
21
22         inc si
23         loop s
24
25         mov ax, 4c00h
26         int 21h
27
28 printNumber:
29         mov bx, ax ;Used here bx Temporary storage, because the output characters need to be used ax
30         or bh, 30h ;Converted to digital ASCII Code value
31         or bl,30h
32         mov ah,2 ;Ready to output characters
33         mov dl,bl ;First exporter
34         int 21h;
35         mov dl,bh ;Re output remainder
36         int 21h;
37         ret
38
39 printSpace:
40         mov ah, 2
41         mov dl, ' '
42         int 21h
43         ret
44
45 code ends
46 end start```

## 2) Screenshot of running test

Output a string on the screen with a specified color and a specified line.

``` 1 assume cs:code, ds:data
2
3 data segment
4         str db 'try'
5         len equ \$ - str
6 data ends
7
8 code segment
9 start:
10         mov ax, data
11         mov ds, ax
12         mov ax, 0b800h ;Show beginning of buffer
13         mov es, ax
14
15         mov si, offset str
16         mov cx, len
17         mov bl, 2h ;The color is green
18         mov bh, 0 ;The number of rows is row 0
19         call printStr
20
21         mov si, offset str
22         mov cx, len
23         mov bl, 4h ;The color is red
24         mov bh, 24 ;The number of lines is line 24
25         call printStr
26
27         mov ax, 4c00h
28         int 21h
29
30 printStr:
31         mov al, 160 ;Space occupied by each line of characters: 80×2=160Bytes
32         mul bh
33         mov di, ax ;ax For the first bh Offset of row
34 s:      mov ah, ds:[si]
35         mov es:[di], ah ;character
36         mov es:[di+1], bl ;colour
37
39         inc si
40         loop s
41         ret
42
43 code ends
44 end start```

## 2) Screenshot of running test

At 80 × In 25 color character mode, the student number is displayed in the middle of the last line of the screen. It is required that the blue background, student number and broken lines on both sides of the output window be displayed in white foreground color.

White characters on blue background: 0001,0111 = 17h

``` 1 assume ds:data, cs:code
2
3 data segment
4     stu_no db '201983290278'
5     len = \$ - stu_no
6 data ends
7
8 code segment
9 start:
10         mov ax, data
11         mov ds, ax
12         mov ax, 0b800h
13         mov es, ax
14         mov cx, 4000 ;The number of bytes occupied by the content of each screen in the display buffer:80×25×2 = 4000Bytes
15         mov di, 0
16         mov ah,17h ;White characters on blue background
17
18 s1:    mov al, 0
19         mov es:[di], al
20         mov es:[di+1], ah
22         loop s1
23
24         ;At the beginning of the last line-
25         mov di, 3840 ;Start on line 24
26         mov cx, 34 ;=(160-12*2)/2/2
27         mov ah, 17h
28 s2:    call printSign
30         loop s2
31
32         ;Print student number
33         mov di, 3908 ;3840+(160-12*2)/2=3840+68
34         mov si, offset stu_no
35         mov cx, len
36         mov ah, 17h
37 s3:    call printStu_no
38         inc si
40         loop s3
41
42         ;At the end of the last line-
43         mov di, 3932 ;=3840-68
44         mov cx, 34
45         mov ah, 17h
46 s4:    call printSign
48         loop s4
49
50         mov ax, 4c00h
51         int 21h
52
53 printStu_no:
54         mov al, [si]
55         mov es:[di], al
56         mov es:[di+1], ah
57         ret
58
59 printSign:
60         mov al, 2Dh ;-
61         mov es:[di], al
62         mov es:[di + 1], ah
63         ret
64
65 code ends
66 end start```

## 2) Screenshot of running test

Posted on Fri, 26 Nov 2021 03:25:32 -0500 by astribuncio