# The third experiment of assembly language

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
44     loop s2
45
46     mov ah, 4ch
47     int 21h
48 code ends
49 end start
• Running screenshot

• Question 1

The jump displacement is - 14. The compiler calculates the jump 8-bit displacement through the address at the label - the address of the first byte after the loop instruction. In this example, the offset address of s1 is 000D, and the offset address of the first byte after the loop instruction is 001B. The subtraction result is - 14.

• Question 2

The jump displacement is - 10, the offset address of label s2 is 0029, and the offset address of the first byte after the loop instruction is 0039. The subtraction result is - 10. All loop instructions, including loop instructions, are short transfers, and the transfer displacement is included in the corresponding machine code instead of the address.

•   Disassembly screenshot

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
• Analysis results

The source program line24 stacks the offset address (IP) of the next line instruction in memory, and modifies the content of the IP to the content of the word at ds:[0] in memory. Previously, the offset address (offset s1) of label s1 has been saved to ds:[0], so after the call instruction is executed, it jumps to label s1 for execution. The instruction at s1 takes the stack top data, that is, the offset address (IP) of the line25 pop instruction in memory, out of the stack and stores it in the register ax.

The line27 call instruction successively stacks the segment address (CS) and offset address (IP) of the next line instruction in memory, and modifies the contents of CS and IP to the contents of double words at memory ds:[2]. Previously, the offset address (offset s2) of label s2 was stored at ds:[2], and the segment address (CS) was stored at ds:[4]. Therefore, after the call instruction is executed, it jumps to label s2 for execution. The instruction after label s1 takes the stack top data (IP) out of the stack and stores it in register bx, and takes the stack top data (CS) out of the stack and stores it in register cx.

Conclusion: ax stores the offset address of the instruction at label s1 in memory, bx stores the offset address of the instruction at label s2 in memory, and cx stores the segment address of the instruction at label s2 in memory.

• Debugging verification

After reverse compilation, observe the address of the instruction at labels s1 and s2

The debugging results are the same as the analysis.

1 assume cs:code, ds:data
2 data segment
3     x db 99, 72, 85, 63, 89, 97, 55
4     len equ \$- x
5 data ends
6
7 code segment
8 start:
9     mov ax, data
10     mov ds, ax
11     mov si, offset x
12     mov cx, len
13   s:mov ah, 0
14     mov al, [si]
15     mov dl, 10
16     div dl
17     mov bx, ax
18     mov ah, 2
19     call printNumber
20     call printSpace
21     inc si
22     loop s
23     mov ah, 4ch
24     int 21h
25
26 printNumber:
27     mov dl, bl
28     or dl, 30h
29     int 21h
30     mov dl, bh
31     or dl, 30h
32     int 21h
33     ret
34 printSpace:
35     mov dl, ' '
36     int 21h
37     ret
38 code ends
39 end start
• Run test

1 assume cs:code, ds:data
2 data segment
3     str db 'try'
4     len equ \$ - str
5 data ends
6
7 code segment
8 start:
9     mov ax, data
10     mov ds, ax
11
12     mov si, offset str
13     mov bh, 0
14     mov bl, 2
15     call printStr
16
17     mov si, offset str
18     mov bh, 24
19     mov bl, 4
20     call printStr
21
22     mov ah, 4ch
23     int 21h
24
25 printStr:mov cx, len
26   s:mov ax, 0b800h
27     mov dx, cx
28     mov ch, 0
29     mov cl, bh
31     loop t
32     mov cx, dx
33     mov es, ax
34     mov di, si
36     mov al, [si]
37     mov es:[di], al
38     mov es:[di].1, bl
39     inc si
40     loop s
41     ret
42 code ends
43 end start
• Run test

1 assume cs:code, ds:data
2 data segment
3     stu_no db '201983300512'
4     len = \$ - stu_no
5 data ends
6
7 code segment
8 start:
9     mov ax, data
10     mov ds, ax
11     call blueCurtain
12     call printStu_no
13
14     mov ah, 4ch
15     int 21h
16 printStu_no:mov ax, 0b800h
17     mov cx, 24
19     loop s
20     mov es, ax
21     mov di, 0
22     mov cx, 34
23   l:mov byte ptr es:[di], '-'
25     loop l
26     mov cx, len
27     mov si, offset stu_no
28   t:mov dl, [si]
29     mov es:[di], dl
30     inc si
32     loop t
33     mov cx, 34
34   r:mov byte ptr es:[di], '-'
36     loop r
37     ret
38
39 blueCurtain:mov ax, 0b800h
40     mov es, ax
41     mov cx, 2000
42     mov si, 0
43   b:mov byte ptr es:[si].1, 23