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

4, Experimental conclusion

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

Operation results:

Question 1: line27,   Assembly instruction   loop s1   When jumping, 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 result:

The offset of label s1 is 000DH. If the offset of the currently executing instruction plus the space occupied by this instruction is 001BH, the displacement is 001BH - 000DH = 000EH, that is, decimal number 14.

The CPU obtains the displacement by calculating the next instruction address - s1 label address of the LOOP instruction.

Question 2: line44, assembly instructions   loop s2   When jumping, 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.

debug disassembly result:

The offset of label s2 is 0029H. If the offset of the currently executing instruction plus the space occupied by this instruction is 0039H, the displacement is 0039H - 0029H = 0010H, which is the decimal number 16.

The CPU obtains the displacement by calculating the next instruction address - s2 label address of the LOOP instruction.

Question 3: attach the disassembly screenshot of debugging observation in debug during the above analysis.

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

According to the jump principle of call instruction, theoretically, the register ax = 0021h, bx before program execution and exit (Line31)=   0026h，cx =   076ch.

In debug, use the u command to disassemble to get the exit instruction offset address, and use the g command to debug to the exit instruction execution. It is observed that ax = 0021h, bx = 0026h, cx = 076ch.

```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 ah, 0
mov al, ds:[si]
call printNumber
call printSpace
inc si
loop s

mov ah, 4ch
int 21h

printNumber:
mov bl, 10
div bl
mov dl, al

mov dh, ah

or dl, 30H     ;Numeric to character
mov ah, 2
int 21h        ;Output high

mov dl, dh
or dl, 30H     ;Numeric to character
int 21h        ;Output low
ret

printSpace:
mov ah, 2
mov dl, ' '
int 21h        ;Output space
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 cx, len
mov si, 0
mov bl, 2     ;green
mov bh, 0     ;first line
mov di, bh    ;Line beginning address
call printStr

mov bh, 24    ;Last line
;The space occupied by each row is 00 A0H
mov bl, bh
mov bh, 0
mov cx, bx
mov ax, 0
findLastRow:
loop findLastRow
mov di, ax

mov cx, len
mov si, 0
mov bl, 4     ;red
call printStr

mov ah, 4ch
int 21h

printStr:
s:  mov al, ds:[si]
mov ah, bl
mov es:[di], ax
inc si
inc di
inc di
loop s
ret

code ends
end start```

Screenshot of running test:

```assume cs:code, ds:data

data segment
stu_no db '201983290068'
len = \$ - stu_no
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, 17H    ;White characters on blue background
mov si, 0
call setBgColor

mov bh, 24     ;Last line
;The space occupied by each row is 00 A0H
mov bl, bh
mov bh, 0
mov cx, bx
mov ax, 0
findLastRow:
loop findLastRow
mov di, ax

mov al, '-'
mov cx, 34
mov bl, 17H    ;White characters on blue background
call printLine

mov cx, len
mov si, 0
mov bl, 17H    ;White characters on blue background
call printStr

mov al, '-'
mov cx, 34
mov bl, 17H    ;White characters on blue background
call printLine

mov ah, 4ch
int 21h

printStr:
s:  mov al, ds:[si]
mov ah, bl
mov es:[di], ax
inc si
inc di
inc di
loop s
ret

printLine:
s1: mov al, '-'
mov ah, bl
mov es:[di], ax
inc di
inc di
loop s1
ret

setBgColor:
mov cx, 2000    ;80*25
s2: mov al, ' '
mov ah, bl
mov es:[si], ax
inc si
inc si
loop s2
ret

code ends
end start```

Screenshot of running test:

5, Experimental summary

Through this experiment, I am familiar with 80 × 25 color character mode display principle. Each line has 160 bytes and 25 lines per page. If you want to output at the specified position, you need to calculate accordingly.

Numbers cannot be displayed directly, so they need to be converted into characters first. or 30h can be used for fast conversion.

Posted on Fri, 03 Dec 2021 00:19:41 -0500 by Mr Tech