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

The program task1.asm source code is given

```assume cs:code, ds:data
data segment
x db 1, 9, 3
len1 equ \$ - x ; symbolic constants , \$Refers to the offset address of the next data item. In this example, it is 3
y dw 1, 9, 3
len2 equ \$ - y ; symbolic constants , \$Refers to the offset address of the next data item. In this example, it is 9
data ends
code segment
start:
mov ax, data
mov ds, ax
mov si, offset x ; Take symbol x Corresponding offset address 0 -> si
mov cx, len1 ; From symbol x Number of consecutive byte data items at the beginning -> cx
mov ah, 2
s1:mov dl, [si]
or dl, 30h
int 21h
mov dl, ' '
int 21h ; Output space
inc si
loop s1
mov ah, 2
mov dl, 0ah
int 21h ; Line feed
mov si, offset y ; Take symbol y Corresponding offset address 3 -> si
mov cx, len2/2 ; From symbol y Number of consecutive word data items started -> cx
mov ah, 2
s2:mov dx, [si]
or dl, 30h
int 21h
mov dl, ' '
int 21h ; Output space
loop s2
mov ah, 4ch
int 21h
code ends
end start```

Operation screenshot:

① 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 displacement is: 14 (the end address of Loop instruction is 001B, the start address of s1 instruction is 000D, 001B-000D=14)

Analysis: the or dl, 30h command occupies three bytes, the inc instruction occupies one byte, and other instructions occupy two bytes respectively. The total is 14 bytes.

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

Displacement: 16 (Loop instruction end address: 0039, s1 instruction start address: 0029001B-000D=16)

Analysis: the or DL and 30h commands occupy three bytes, and other instructions occupy two bytes respectively. The total is 16 bytes.

```assume cs:code, ds:data
data segment
dw 200h, 0h, 230h, 0h
data ends
stack segment
db 16 dup(0)
stack ends
code segment
start:
mov ax, data
mov ds, ax
mov word ptr ds:[0], offset s1
mov word ptr ds:[2], offset s2
mov ds:[4], cs
mov ax, stack
mov ss, ax
mov sp, 16
call word ptr ds:[0]
s1: pop ax
call dword ptr ds:[2]
s2: pop bx
pop cx
mov ah, 4ch
int 21h
code ends
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) =?

A: theoretically, the data in ax should be the IP address of the instruction s1:pop ax. Because   call word ptr ds:[0] pushes the IP of the s1:pop ax instruction onto the stack.

bx should be the IP of s2:pop bx, and cx should be the CS of s2:pop bx.

Because   call dword ptr ds:[2]   Instruction will   S2: CS and IP of pop BX are successively pushed into the stack.

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

It is consistent with the conjecture.

The program source code task3.asm is given

```assume cs:code, ds:data
data segment
x db 99, 72, 85, 63, 89, 97, 55
len equ \$- x
data ends

code segment
start:
mov ax, data
mov ds, ax
mov si, 0
mov bl, 10
mov cx, len

s:  mov ah, 0
mov al, [si]
call printNumber
call printSpace
inc si
loop s

mov ah, 4ch
int 21h

printNumber:
div bl
mov dl, al     ;al For business, existence dl in
mov bh, ah   ;ah Is remainder, exists bh in

or dl,30H  ;Output ten bits dl
mov ah,2
int 21h

mov dl, bh  ;Output bit bh
or dl,30H
mov ah,2
int 21h

ret

printSpace:
mov ah, 2
mov dl, ' '
int 21h
ret

code ends
end start```

Screenshot of running test:

code:

```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,stack
mov ss,ax
mov sp,16
mov ax, data
mov ds, ax
mov ax, 0b800h      ;The memory address of the video memory is b8000h~bFFFFh
mov es, ax
mov di, 0
mov cx, len
mov si, offset str
s1:
mov bl, 2      ;Set the color to green
mov bh, 0     ;Set row
call printStr
loop s1

mov cx, len
mov si, offset str
mov bh, 24     ;Set row
mov al, 0a0h       ;The number of bytes per line is 160(8 0 columns per row)
mov ah, 0
mul bh
mov di, ax       ;Set position
s2:
mov bl, 4      ;Set the color to red
call printStr
loop s2

mov ah, 4ch
int 21h

printStr:
mov ah, bl        ;ah Save color
mov al, [si]        ;al Save displayed content
mov es:[di], ax  ;es:[di]Yes, specify the output location
inc si
ret

code ends
end start```

Operation screenshot:

code:

```assume cs:code, ds:data

data segment
stu_no db '201983290435'
len = \$ - stu_no
data ends

stack segment
dw 0,0,0,0,0,0,0,0,0      ;Define a segment to be used as a stack segment with a capacity of 16 bytes
stack ends

code segment
start:
mov ax,stack
mov ss,ax
mov sp,16

mov ax, data
mov ds, ax
mov ax, 0b800h
mov es, ax

;First layer circulation
mov di, 0
mov cx, 24
s1:
push cx   ;Circulating the outer layer cx Stack pressing
mov cx, 80  ;take cx Set to the number of inner loops
s2:
mov ax, 1720h
mov es:[di], ax
loop s2
mov di, 0
pop cx ;From the top of the stack cx Restore the value of
mov ax, es
mov es, ax
loop s1

;Second layer circulation
mov di, 0
mov cx, 30
s3:
mov ax, 172dh
mov es:[di], ax
loop s3

;Third layer circulation
mov di, 003ch       ;Set the starting position of student number
mov si, offset stu_no
mov cx, len
s4:
mov ah, 017h        ;ah Save color
mov al, [si]        ;al Save displayed content
mov es:[di], ax  ;es:[di]Yes, specify the output location
inc si
loop s4

;Fourth layer circulation
mov cx, 38
s5:
mov ax, 172dh
mov es:[di], ax
loop s5

mov ah, 4ch
int 21h

code ends
end start```

Screenshot:

Experiment summary:

```or dl,30H
mov ah,2
int 21h```

The function of is to convert the number in the dl register into the corresponding ASCL code

Posted on Sun, 28 Nov 2021 20:45:10 -0500 by cybaf