1, Experimental purpose

1. Understand and master the jump principle of transfer instruction

2. Master the method of using call and ret instructions to realize subroutines, and understand and master the parameter transfer mode

3. Understand and master 80 × 25 color character mode display principle

4. Integrate addressing mode and assembly instructions to complete simple application programming

2, Experimental preparation

Review chapters 9-10 of the textbook:

Jump principle of transfer instruction

Usage of assembly instructions jmp, loop, jcxz, call, ret, retf

3, Experimental content

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

` `

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

From 001B to 000D, the displacement is - 14 (decimal)

F2 is the complement of - 14

The loop instruction is a circular instruction. All circular instructions are short transfers. The IP modification range is - 128 ~ 127 (8 bits). The shift of the transfer is included in the corresponding machine code, not the destination address. Then the shifted 8-bit displacement = the address at the label - the address of the first byte after the loop instruction, i.e. 001B-000D. The 8-bit displacement is calculated by the compiler during compilation.

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

From 0039 to 0029, the displacement is - 16 (decimal)

F0 is the complement of - 16

The machine code format of the loop instruction is E2 offset (represented by 8 bits and complement)

The loop instruction is a circular instruction. All circular instructions are short transfers. The IP modification range is - 128 ~ 127 (8 bits). The shift of the transfer is included in the corresponding machine code, not the destination address. Then the shifted 8-bit displacement = the address at the label - the address of the first byte after the loop instruction, i.e. 0039-0029. The 8-bit displacement is calculated by the compiler during compilation, and the offset is represented by complement.

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

Execute call word ptr ds:[0], ds:[0] is the address of s1: 0021, and press IP:0021 into the stack. After executing pop ax, take out 0021 and assign it to ax.

Execute call dword ptr ds:[2], ds:[2] is the address of label s2: 0026, press CS: 076C and IP: 0026 into the stack, execute pop bx, take out 0026 and assign it to bx, execute pop cx, and take out 076C and assign it to cx

Therefore, theoretically, ax=0021,bx=0026,cx=076C

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

See above

```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 cx,10
mov si,0

s:    mov ah,0
mov al,ds:[si]
mov bl,10
div bl
mov ds:[len+si],al            ;Store the two digits of the number separately
mov ds:[len+1+si],ah

mov ah,2　　　　　　　　　　　　　;int 21h Subfunction 2 of
mov dl,ds:[len+si]
add dl,30h           　　　　  ;Convert numbers from decimal to ASCII code
int 21h               　　　　 ;Print

mov ah,2
mov dl,ds:[len+1+si]
int 21h

mov ah,2
mov dl," "
int 21h

inc si
loop s

mov ah,4ch
int 21h
code ends
end start```

Test results:

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

Write subroutine printStr

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

Entry parameters

The address of the first character of the string -- > ds: Si (where, the segment address of the segment where the string is located -- > DS, and the 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

Subroutine not used:

```assume cs:code,ds:data
data segment
str db 'try'
len equ \$ - str
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0b800h
mov es,ax
mov cx,len
mov si,0
mov bx,0000h    ;first line

s1:        mov ah, 02h
mov al,ds:[si]
mov es:[bx],ax
inc si
loop s1

mov bx,0F00h    ;Last line
mov cx,len
mov si,0

s2:     mov al,ds:[si]
mov ah,04h
mov es:[bx],ax
inc si
loop s2

mov ax,4c00h
int 21h
code ends
end```

Using subroutines

```assume cs:code,ds:data
data segment
str db 'try'
len equ \$ - str
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0b800h
mov es,ax

mov si, offset PrintStr
mov bx,0000h    ;first line
mov ah, 02h
call si

mov si, offset PrintStr
mov bx,0F00h    ;Last line
mov ah,04h
call si

mov ax,4c00h
int 21h

PrintStr:
mov cx,len
mov si,0

s:    mov al,ds:[si]
mov es:[bx],ax
inc si
loop s
ret
code ends
end start```

For 8086 CPU, for 8086 CPU, the known logical segment is defined as follows: 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 student number and broken lines on both sides of the output window are displayed in white foreground.

Note *:

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.

```assume cs:code,ds:data
data segment
str db '201983290287'
len equ \$ - str;12 Characters
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0b800h
mov es,ax
mov cx,0F9Fh
mov bx,0

s1:    mov ah,17h;White characters on blue background
mov al,' ';Blue screen
mov es:[bx],ax
loop s1

mov bx,0F00h
mov cx,34;A line has 80 characters, accounting for 160 bytes-12/2=34，Achieve centering
s2:    mov al,'-'
mov es:[bx],ax
loop s2

mov cx,len
mov si,0
s3:    mov al,[si]
inc si
mov es:[bx],ax
loop s3

mov cx,34

s4:    mov al,'-'
mov es:[bx],ax
loop s4

mov ax,4c00h
int 21h
code ends
end```

5, Experimental summary

Using subroutines is more concise

Posted by nemesis1931 on Mon, 29 Nov 2021 06:52:01 -0800