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

``` 1 assume cs:code, ds:data
2 data segment
3 x db 1, 9, 3
4 len1 equ \$ - x ; symbolic constants , \$Refers to the offset address of the next data item. In this example, it is 3
5 y dw 1, 9, 3
6 len2 equ \$ - y ; symbolic constants , \$Refers to the offset address of the next data item. In this example, it is 9
7 data ends
8 code segment
9 start:
10 mov ax, data
11 mov ds, ax
12 mov si, offset x ; Take symbol x Corresponding offset address 0 -> si
13 mov cx, len1 ; From symbol x Number of consecutive byte data items at the beginning -> cx
14 mov ah, 2
15 s1:mov dl, [si]
16 or dl, 30h
17 int 21h
18 mov dl, ' '
19 int 21h ; Output space
20 inc si
21 loop s1
22 mov ah, 2
23 mov dl, 0ah
24 int 21h ; Line feed
25 mov si, offset y ; Take symbol y Corresponding offset address 3 -> si
26 mov cx, len2/2 ; From symbol y Number of consecutive word data items started -> cx
27 mov ah, 2
28 s2:mov dx, [si]
29 or dl, 30h
30 int 21h
31 mov dl, ' '
32 int 21h ; Output space
34 loop s2
35 mov ah, 4ch
36 int 21h
37 code ends
38 end start```

tip: understand the flexible use of operator offset, pseudo instruction equ and predefined symbol \$. Through line5, line8 and the data attributes of data items (bytes, words, doublewords, etc.), the number of continuous data items can be calculated easily without manual counting.

Note *: the symbolic constants len1 and len2 do not occupy the memory space of the data segment

Question ①

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

The jump displacement is 14. The CPU obtains the offset address by subtracting the address of s1 from the address of the next instruction of the loop instruction.

Question ②

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

The displacement is 16, and the CPU obtains the offset address by subtracting the address of s1 from the address of the next instruction of the loop instruction.

Question ③

③ Attach the disassembly screenshot of debugging observation in debug during the above analysis

See screenshot above.

``` 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
26
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```

Question ①

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

call will stack the current ip or cs and ip before jump.

Therefore, ax is the address of pop ax, bx is the address of px bx, and cx is the value of cs.

Question ②

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

For 8086 CPU, the known logical segment is defined as follows:

```1 data segment
2 x db 99, 72, 85, 63, 89, 97, 55
3 len equ \$- x
4 data ends```

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.

requirement:

Write subroutine printNumber

Function: output a two digit number in decimal form

Entry parameter: register ax (data to be output -- > ax)

Outlet parameters: None

Write the subroutine printSpace

Function: print a space

Entry parameters: None

Outlet parameters: None

In the main code, the addressing mode and loop are comprehensively applied, and printNumber and printSpace are called to realize the subject requirements.

The description of sub function 2 in tip: int 21h is as follows

```; Function: output single character
mov ah, 2
mov dl, ×× ; ××Is the character to be output, or its ASCⅡCode value
int 21h```

The code is as follows:

``` 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 cx,len
13     mov bx,0
14     mov byte ptr ds:[len],10
15
16 s:    mov ax,0
17     mov al,ds:[bx]
18     call printNumber
19     call printSpace
20     inc bx
21     loop s
22
23     mov ah,4ch
24     int 21h
25
26 printNumber:
27     div byte ptr ds:[len]
28     mov dx,ax
29     or dl,30h
30     mov ah,2
31     int 21h
32
33     mov dl,dh
34     or dl,30h
35     mov ah,2
36     int 21h
37
38     ret
39
40 printSpace:
41     mov dl,' '
42     mov ah,2
43     int 21h
44     ret
45
46 code ends
47 end start```

For 8086 CPU, the known logical segment is defined as follows:

``` 1 data segment
2 str db 'try'
3 len equ \$ - str
4 data ends```

Write 8086 assembly source program task4.asm, specify the color and line on the screen, and output the string on the screen.

requirement:

Write subroutine printStr

Function: display the string on the screen in the specified line and in the specified color

Entry parameter string first character address -- > ds: Si (where, segment address of the segment where the string is located -- > DS, offset address of the starting address of the string -- > SI) string length -- > CX string color -- > BL specified line -- > BH (value: 0 ~ 24)

Outlet parameters: None

In the main code, printStr is called twice to display the string in green on a black background at the top of the screen and in red on a black background at the bottom of the screen

The code is as follows:

``` 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
13     mov si,0
14     mov cx,len
15     mov bl,00000010b
16     mov bh,0
17     call printStr
18
19     mov si,0
20     mov cx,len
21     mov bl,00000100b
22     mov bh,24
23     call printStr
24
25     mov ah,4ch
26     int 21h
27
28 printStr:
29     mov ax,0b800h
30     mov es,ax
31     mov ax,0
32     mov al,bh
33     mov dx,160
34     mul dx
35     mov di,ax
36     s:    mov al,ds:[si]
37         mov es:[di],al
38         inc si
39         inc di
40         mov es:[di],bl
41         inc di
42         loop s
43     ret
44 code ends
45 end start```

For 8086CPU, for 8086CPU, the known logical segment is defined as follows:

```1 data segment
2 stu_no db '20498329042'
3 len = \$ - stu_no
4 data ends```

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.

tips:

1. 80 × 25 color character mode display buffer structure, see the description in the textbook "experiment 9 programming according to materials".

2. When writing the program, replace the student number of the data segment with your own student number.

The code is as follows:

``` 1 assume cs:code, ds:data
2
3 data segment
4     stu_no db '201983290075'
5     len = \$ - stu_no
6 data ends
7
8 code segment
9 start:
10     mov ax,data
11     mov ds,ax
12
13     mov ax, 0b800h
14     mov es, ax
15     mov di, 0
16     mov bl, 17h
17     mov cx, 80 * 25
18
19     s1:
20     mov al, ' '
21     mov es:[di], al
22     inc di
23     mov es:[di], bl
24     inc di
25     loop s1
26
27     mov di, 160 * 24
28     mov cx, 34
29 s2:
30     mov al, '-'
31     mov es:[di], al
32     inc di
33     mov es:[di], bl
34     inc di
35     loop s2
36
37     mov di, 160 * 24 + 34 * 2
38     mov cx, 12
39     mov si, 0
40 s3:
41     mov al, [si]
42     mov es:[di], al
43     inc di
44     mov es:[di], bl
45     inc di
46     inc si
47     loop s3
48
49     mov di, 160 * 24 + 46 * 2
50     mov cx, 34
51 s4:
52     mov al, '-'
53     mov es:[di], al
54     inc di
55     mov es:[di], bl
56     inc di
57     loop s4
58
59     mov ax, 4c00h
60     int 21h
61 code ends
62 end start```

Posted on Fri, 03 Dec 2021 04:26:33 -0500 by Ziq