Experiment 3 transfer instruction jump principle and its simple application programming

Experimental task 1

<task3_1.asm>

 1 assume cs:code, ds:data
 2 
 3 data segment
 4     x db 1, 9, 3
 5     len1 equ $ - x
 6 
 7     y dw 1, 9, 3
 8     len2 equ $ - y
 9 data ends
10 
11 code segment
12 start:
13     mov ax, data
14     mov ds, ax
15 
16     mov si, offset x
17     mov cx, len1
18     mov ah, 2
19  s1:mov dl, [si]
20     or dl, 30h
21     int 21h
22 
23     mov dl, ' '
24     int 21h
25 
26     inc si
27     loop s1
28 
29     mov ah, 2
30     mov dl, 0ah
31     int 21h
32 
33     mov si, offset y
34     mov cx, len2/2
35     mov ah, 2
36  s2:mov dx, [si]
37     or dl, 30h
38     int 21h
39 
40     mov dl, ' '
41     int 21h
42 
43     add si, 2
44     loop s2
45 
46     mov ah, 4ch
47     int 21h
48 code ends
49 end start

Operation results:

Operator offset: returns the offset address of the data label, calculated in bytes

Pseudo instruction equ: alias strings, registers, instructions, etc

Predefined symbol $: the offset address of the current instruction

  Disassembly result:

According to the machine code obtained by disassembly, the displacement is F2 (complement), that is - 14 (decimal original code).

From the perspective of CPU,

(1) First read E2 F2 and enter the instruction buffer

(2) The IP points to the offset address of the next instruction, i.e. 001B

(3) Execute the instruction E2, F2, IP(001B) in the instruction buffer plus the displacement F2, 27-14 = 13000DH

(4) After execution (IP)=000DH

Disassembly result:

  The method is exactly the same as that in question 1. The displacement is F0 (complement), that is - 16 (decimal original code)

From the perspective of CPU,

(1) First read E2 F0 and enter the instruction buffer

(2) IP refers to the offset address of the next instruction, i.e. 0039H

(3) Execute the instruction E2 F0, IP(0039H) in the instruction buffer plus the displacement f00029H

(4) After execution (IP)=0029H

Experimental task 2

<task3_2.asm>

 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 process:

The offset address of s1 is sent to ds:[0], and the offset address of s2 is sent to ds:[2];

When the call word ptr instruction is executed, first put the offset address of the next instruction on the stack, which is equivalent to push IP

call dword ptr, the segment address and offset address of the next instruction must be put on the stack, which is equivalent to push CS and push IP

When line 24 executes, the IP points to s1, puts the offset address 0021 of s1 on the stack, and then jmp to ds:[0] executes pop ax. At this time, ax=0021;

When line 27 is executed, the IP points to s2, successively puts the segment address 076C and offset address 0026 of s2 into the stack, and then jmp to ds:[2] executes pop bx. At this time, bx=0026; Then execute pop cx, where cx=076C;

To sum up, ax=0021, bx=0026, cx=076C

Commissioning process:

  Step by step, ds:[0] stores 0021, and ax after pop ax is 0021

  Execute to mov ah,4c, and the contents of each register are as shown in the figure, which meets the expectation:

  Experimental task 3

Write the 8086 assembly source program task3.asm to output this group of continuous data in the data section in decimal form on the screen, and the data is separated by spaces.

<task3_3.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     mov bx,0
13     mov cx,len
14 s:  call printNumber
15     call printSpace
16     add bx,1
17     loop s
18     mov ax,4c00h
19     int 21h
20 printNumber:
21     push ax
22     push bx
23     push cx
24     push dx
25 
26     mov si,0 ;Record digit
27     ;After debugging, it is found that if you directly mov ax,ds:[bx],ax The high-order storage represents the hexadecimal number H,There will be problems with the final division
28     mov ah,0 
29     mov al,ds:[bx] ;Put the real divisor low
30     ;mov ax,ds:[bx]
31 convert:
32     mov dx,0 ;Divisor high position 0 to prevent interference
33     mov cx,10 ;Divisor
34     div cx
35 
36     add dx,30h ;Remainder to character
37     push dx ;The separated characters are temporarily saved to the stack;Stack operations are in words
38 
39     inc si
40     mov cx,ax ;End with quotient 0
41     jcxz back
42     jmp convert
43 back:
44     mov cx,si
45 s1:
46     pop bx
47     mov ah,2
48     mov dl,bl
49     int 21h
50     loop s1
51 
52     pop dx
53     pop cx
54     pop bx
55     pop ax  
56     ret
57 printSpace:
58     mov ah,2
59     mov dl,32
60     int 21h
61     ret
62 code ends
63 end start

Values are displayed in hexadecimal in debug (actually stored in binary)

When putting the data into the divisor ax register, because it is byte data, if mov ax,ds:[bx], it will also assign H representing hexadecimal to AH, resulting in errors in subsequent division results. As shown in the figure below, the value 99 (hexadecimal 63H) is stored as 4863:

  Modification method: let ah be 0, and al store the divisor. Operation result:

Because stack is used to store data temporarily, 16 bit divisor is used. If stack is not used, 8-bit register can also be used as divisor.

Experimental task 4

<task3_4.asm>

 1 assume cs:code,ds:data
 2 data segment
 3     str db 'try'
 4     len equ $ - str
 5 data ends
 6 code segment
 7 start:
 8     mov ax,data
 9     mov ds,ax
10 
11     mov bh,0 ;Specify row
12     mov bl,2 ;String color
13     mov cx,len ;String length
14     mov si,0
15     call printStr
16 
17     mov bh,24 ;Specify row
18     mov bl,4 ;String color
19     mov cx,len ;String length
20     mov si,0
21     call printStr
22     mov ax,4c00h
23     int 21h
24 printStr:
25     push bx
26     push cx
27     push si
28 
29     mov ax,0b800h
30     mov es,ax
31     mov ax,160
32     mul bh     ;ax=The offset address of the display area
33 
34     mov di,ax
35     mov cx,len
36 s0:
37     mov ax,ds:[si]
38     mov es:[di],ax
39     mov es:[di+1],bl
40     inc si
41     add di,2
42     loop s0
43 
44     pop si
45     pop cx
46     pop bx
47     ret
48 code ends
49 end start

Operation results:

  Experimental task 5

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 blue background, student number and broken lines on both sides of the output window be displayed in white foreground color.

 <task3_5.asm>

 1 assume cs:code,ds:data
 2 data segment
 3     stu_no db '201983300444'
 4     len = $ - stu_no
 5 data ends
 6 code segment
 7 start:
 8     mov ax,data
 9     mov ds,ax
10     mov ax,0b800h
11     mov es,ax
12 
13     mov si,0 ;Even address storage character
14     mov bx,1 ;Odd address storage color
15     mov cx,2000
16 s:
17     mov al,17h
18     mov es:[bx],al
19     add bx,2
20     loop s
21     mov bx,0f00h
22     call printLine
23     call printNum
24     mov bx,0f5ch
25     call printLine
26     mov ax,4c00h
27     int 21h
28 
29 printLine:
30     mov cx,34
31 s1:
32     mov byte ptr es:[bx],'-'
33     add bx,2
34     loop s1
35     ret
36 
37 printNum:
38     mov cx,len
39 s2:    
40     mov al,[si]
41     mov es:[bx],al
42     add bx,2
43     inc si
44     loop s2
45     ret
46 code ends
47 end start

Set the background color and foreground text color: you can set the properties directly, regardless of the characters. Note that the attribute takes only one byte

Test results:

 

  Summary and analysis:

1. Note whether you are manipulating bytes or words

2. Pay attention to the jump out timing, jump out conditions and jump out subsequent operations of the cycle

3. Flexible use of stack

Posted on Sun, 28 Nov 2021 04:21:33 -0500 by CarbonCopy