Experiment 3 transfer instruction jump principle and its simple application programming

4, Experimental conclusion

1. Experimental task 1

  • Program task1.asm source code

 

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

 

Operation screenshot:

 

  • Answer question ①

When reading the assembly instruction loop s1, the IP points to 001B, and after executing the instruction, the IP points to 000D. The jump displacement is - 14. F2 in the machine code E2F2 corresponding to the instruction is the hexadecimal complement of jump displacement - 14. The CPU adds the jump displacement to the IP value to obtain the IP of the next instruction to be executed, that is, 000D.

  • Answer question ②

When reading the assembly instruction loop s2, the IP points to 0039, and after executing the instruction, the IP points to 0029. The jump displacement is - 16. F0 in the machine code E2F0 corresponding to the instruction is the hexadecimal complement of jump displacement - 16. The CPU adds the jump displacement to the IP value to obtain the IP of the next instruction to be executed, i.e. 0029.

 

  • Question ③

 

2. Experimental task 2

  • Program task2.asm source code

 

 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

 

  • ① When the CPU executes the call instruction, it will stack the current IP (or CS and IP). Therefore, the IP corresponding to the line25 (pop ax) instruction should be stored in ax, the IP corresponding to the line28 (pop bx) instruction should be stored in bx, and the CS corresponding to the line28 instruction should be stored in cx.

As can be seen from the figure below, register (ax)=0021, (bx)=0026, (cx)=076C

 

② Screenshot of debugging result interface:

  The debugging results are consistent with the theoretical analysis results.

 

3. Experimental task 3

  • Program source code task3.asm

 

 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 
13     mov cx, len    ;From symbol x Number of consecutive byte data items at the beginning
14     mov si, 0
15 s:    
16     mov ah, 0
17     mov al, ds:[si]    ;A two digit number occupies one byte
18     call printNumber
19     call printSpace
20     inc si
21     loop s
22 
23     mov ah, 4ch
24     int 21h
25 
26 ;Outputs a two digit number in decimal form
27 printNumber:
28     mov bl, 10
29     div bl
30     mov dl, al    ;Quotient: ten digits
31     mov dh, ah    ;Remainder: single digit
32     or dl, 30h    ;Convert numbers to corresponding characters
33     or dh, 30h
34 
35     mov ah, 2
36     int 21h
37     mov dl, dh
38     int 21h
39     ret
40 
41 ;Print a space
42 printSpace:
43     mov ah, 2
44     mov dl, ' '
45     int 21h
46     ret
47 
48 code ends
49 end start

 

  • Screenshot of running test

 

4. Experimental task 4

As can be seen from the figure below,

Green words on black background: 0000 0010, converted to hexadecimal to 2H

Red letter on black background: 0000 0100, converted to hexadecimal 4H

 

  • Program source code task4.asm

 

 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
13     mov es, ax
14 
15     mov si, offset str
16     mov bl,    2h    ;String color
17     mov bh,    0    ;Specify row
18     call printStr
19 
20     mov si, offset str
21     mov bl, 4h
22     mov bh, 24
23     call printStr
24 
25     mov ah, 4ch
26     int 21h
27 
28 ;Displays the string on the screen in the specified line and in the specified color
29 printStr:
30 ;Calculate the first offset address corresponding to the line number
31     mov al, bh
32     mov dl, 00A0h
33     mul dl
34     
35     mov di, ax
36     mov cx, len    ;From symbol str Number of consecutive byte data items at the beginning
37 s:
38     mov al, ds:[si]
39     mov es:[di], al
40     inc di
41     mov es:[di], bl
42     inc si
43     inc di
44     loop s
45     ret
46 
47 code ends
48 end start

 

  • Screenshot of running test

 

5. Experimental task 5

White words on blue background: 0001 0111, converted to hexadecimal 17H

Column 80, '-' 34 student number 12 '-' 34

  • Program source code task5.asm

 

 1 assume cs:code, ds:data
 2 
 3 data segment
 4     stu_no db '201983290259'
 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 
15     mov si, offset stu_no
16     mov di, 0
17     mov bl,    17h    ;White characters on blue background
18     call printColor
19     call printLine
20     call printNumber
21     call printLine
22 
23     mov ah, 4ch
24     int 21h
25 ;Print background color
26 printColor:
27     mov cx, 1920    ;First 24 lines 24*80
28 s0:
29     inc di
30     mov es:[di],bl
31     inc di
32     loop s0
33     ret
34 
35 ;Print student number
36 printNumber:
37     mov cx, len    ;From symbol stu_no Number of consecutive byte data items at the beginning
38 s1:
39     mov al, ds:[si]
40     mov es:[di], al
41     inc di
42     mov es:[di], bl
43     inc si
44     inc di
45     loop s1
46     ret
47 
48 ;Print 34'-'
49 printLine:
50     mov cx, 34
51 s2:
52     mov word ptr es:[di], '-'
53     inc di
54     mov es:[di], bl
55     inc di
56     loop s2
57     ret
58 
59 code ends
60 end start

 

  • Screenshot of running test

 

5, Experimental summary

The use of the operator offset is the offset label, which is used to take the offset address of the label during assembly.

The pseudo instruction equ is used for equivalent assignment, and its format is < symbol name > equ < expression >.

Predefined symbol $, equivalent to the current offset value of the segment currently being assembled.

 

Posted by sd9sd on Sun, 28 Nov 2021 05:04:15 -0800