Experiment 3 transfer instruction jump principle and its simple application programming

empirical conclusion

1. Experimental task 1

Using any text editor, enter the 8086 assembler source code task1.asm.

assume cs:code, ds:data

data segment
    x db 1, 9, 3
    len1 equ $ - x

    y dw 1, 9, 3
    len2 equ $ - y
data ends

code segment
start:
    mov ax, data
    mov ds, ax

    mov si, offset x
    mov cx, len1
    mov ah, 2
 s1:mov dl, [si]
    or dl, 30h
    int 21h

    mov dl, ' '
    int 21h

    inc si
    loop s1

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

    mov si, offset y
    mov cx, len2/2
    mov ah, 2
 s2:mov dx, [si]
    or dl, 30h
    int 21h

    mov dl, ' '
    int 21h

    add si, 2
    loop s2

    mov ah, 4ch
    int 21h
code ends
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.

 

  The address at the s1 label is 000D, i.e. 13 in decimal; The address of the next instruction in loop is 001B, that is, decimal 27. Subtract it to get - 14. The loop instruction contains the transferred displacement in the corresponding machine code instead of the destination address. The jump displacement is the address difference from s1 to loop s1.

 

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

 

The jump displacement = 0029 h- 0039 h=41-57=-16. The loop instruction contains the transferred displacement in the corresponding machine code instead of the destination address. The jump displacement is the address difference from s2 to loop s2.

 

2. Experimental task 2

Program task2.asm source code

 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, it is analyzed theoretically that register (ax) = 21 register (bx) = 26 register (cx) = 076C before program execution and exit (line31)

 

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

(AX) = offset address of pop ax; (bx) = offset address of pop bx, (cx) = segment address

 

3. Experimental task 3

For 8086 CPU, the known logical segment is defined as follows:

 data segment

  x db 99, 72, 85, 63, 89, 97, 55

  len equ $- x

data ends

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.

Requirements: write the subroutine printNumber

Function: output a two digit number in decimal form

Entry parameter: register ax (data to be output -- > ax)

Outlet parameters: None

Write the subroutine printSpace

Function: print a space

Entry parameters: None

Outlet parameters: None

In the main code, the addressing mode and loop are comprehensively applied, and printNumber and printSpace are called to realize the subject requirements.

  Program source code task3.asm

 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 code segment
 7 start:
 8 mov ax,data
 9 mov ds,ax
10 mov si,0
11 mov cx,len
12 
13 
14  s:
15     call printNumber
16     call printSpace
17 
18     inc si
19     loop s
20 
21 mov ax,4c00h
22 int 21h
23 
24  printNumber:
25  mov ah,0
26  mov al,ds:[0+si]
27  mov bl,10                  ;Ten bits and ten bits of two digits are stored in two memory units respectively
28  div bl
29  mov ds:[len+si],al
30  mov ds:[len+1+si],ah
31 
32  mov ah,2                     ; Output ten digits
33  mov dl,ds:[len+si]
34  add dl,30H
35   int 21h
36 
37 mov ah,2                     ; Output bit number
38 mov dl,ds:[len+1+si]
39 add dl,30h
40 int 21h
41 
42 ret
43 
44 
45 printSpace:
46 mov ah,2                      ;  Output space
47 mov dl,20H
48 int 21h
49 
50 ret
51 
52 
53 code ends
54 end start

  Screenshot of running test:

 

 

4. Experimental task 4

For 8086 CPU, the known logical segment is defined as follows:

data segment

  str db 'try'

  len equ $ - str

data ends

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

requirement:

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

Program source code task4.asm

  1 assume cs:code
  2 data segment
  3     str db 'try',0                ;data Segment defines the string to be output to'0'As an end flag
  4     len equ $ - str    
  5 data ends
  6 
  7 stack segment
  8     db 128 dup(0)
  9 code segment
 10 start:            mov ax,data
 11                 mov ds,ax
 12                 mov ax,stack
 13                 mov ss,ax
 14                 mov sp,128
 15                 call init_data                ;Initialization data
 16                 call printStr                ;Display method
 17                 call init_data2
 18                 call printStr
 19                 mov ax,4c00h
 20                 int 21h
 21 
 22 init_data2:        mov ax,0B800h
 23                 mov es,ax
 24                 mov dh,24    ;Specify line number
 25                 mov dl,0        ;Specify column number
 26                 mov cl,00000100B;Specify color
 27                 mov si,0
 28                 mov di,0
 29                 ret
 30 
 31 init_data:        mov ax,0B800h
 32                 mov es,ax
 33                 mov dh,0    ;Specify line number
 34                 mov dl,0        ;Specify column number
 35                 mov cl,00000010B;Specify color
 36                 mov si,0
 37                 mov di,0
 38                 ret
 39 
 40 printStr:        ;call clear_screen            ;Clear screen
 41                 call getRow                    ;Gets the byte offset of the specified line number
 42                 call getCol                    ;Gets the specified column number
 43                 call show_String            ;Real display string method
 44                 ret
 45 
 46 clear_screen:    push cx
 47                 push dx
 48                 push es
 49                 push bx
 50                 mov cx,2000     ;A page has 2000 characters, 2 bytes each
 51                 mov dx,0700h    ;Use 0700 for double words on the screen h replace
 52                 mov bx,0
 53 clearScreen:    mov es:[bx],dx
 54                 add bx,2
 55                 loop clearScreen
 56                 
 57                 pop bx
 58                 pop es
 59                 pop dx
 60                 pop cx
 61                 ret
 62 
 63 show_String:    push cx     ;Save the registers to be used below
 64                 push ds
 65                 push es
 66                 push dx
 67                 push si
 68                 push di
 69                 
 70                 mov dh,cl        ;High memory color
 71                 mov cx,0
 72 showString:        mov cl,ds:[si]
 73                 jcxz showStringRet
 74                 mov dl,ds:[si]    ;Low memory character
 75                 mov es:[di],dx
 76                 add di,2
 77                 inc si
 78                 jmp showString
 79                 
 80 showStringRet:    pop di        ;Restore register
 81                 pop si
 82                 pop dx
 83                 pop es
 84                 pop ds
 85                 pop cx
 86                 ret                
 87 
 88 getRow:            mov al,dh
 89                 mov bl,160   ;One line of 80 characters, 160 bytes
 90                 mul bl
 91                 mov di,ax
 92                 ret
 93 
 94 getCol:            mov al,dl
 95                 mov bl,2
 96                 mul bl
 97                 add di,ax
 98                 ret
 99                 
100 code ends
101 end start

Screenshot of running test

 

 

5. Experimental task 5

For 8086CPU, for 8086CPU, the known logical segment is defined as follows:

data segment

  stu_no db '20498329042'

  len = $ - stu_no

data ends

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.

Program source code task5.asm

  1 assume ds:data, cs:code
  2 data segment
  3        stu_no db '201983290195'
  4        ;db 2H,24H,71H          ;String attribute value
  5        len = $ - stu_no
  6 data ends
  7 
  8 code segment
  9 start:  mov ax,data
 10         mov ds,ax            ;Enter the segment address of the segment where the string and attribute value are located ds
 11 
 12         mov ax,0b800H
 13         mov es,ax            ;80×25 Color character mode display buffer section address input es
 14 
 15         ; Add code to send each character and its attributes into the corresponding line of the corresponding display buffer one by one through a loop
 16         ; I.e. will data The characters and attributes of segments are passed one by one through a loop mov To display buffer(es)In the corresponding unit of the segment
 17 
 18         ; You can try to write three pieces of code, one line at a time
 19         ; After the program runs, try to modify and simplify the three sections of code through flexible addressing mode and loop
 20         ; After learning the subroutine in Chapter 10, you can further improve the optimization, design the subroutine, and set the row number, column number and color attributes as the entry parameters
 21          mov bx,0
 22          mov si,0
 23          mov cx,80
 24 
 25 s1:     ; mov ax,[bx]
 26         ; mov es:[bx+720h][si],ax
 27          mov al,23               ;Store color attribute value blue
 28          mov es:[bx+1h][si],al
 29          inc bx
 30          inc si
 31          loop s1
 32 
 33 
 34          mov bx,0
 35          mov cx,80
 36          mov si,160
 37 
 38 s2:mov ax,[bx]
 39mov es:[bx+720h][si],ax
 40          mov al,23               ;Store color attribute value blue
 41          mov es:[bx+1h][si],al
 42          inc bx
 43          inc si
 44          loop s2
 45 
 46 
 47          mov bx,0
 48          mov cx,80
 49          mov si,320
 50 
 51 s3:mov ax,[bx]
 52mov es:[bx+720h][si],ax
 53          mov al,23               ;Store color attribute value blue
 54          mov es:[bx+1h][si],al
 55          inc bx
 56          inc si
 57          loop s3
 58 
 59          mov bx,0
 60          mov cx,1760
 61          mov si,480
 62 
 63 s4:mov ax,[bx]
 64mov es:[bx+720h][si],ax
 65          mov al,23               ;Store color attribute value blue
 66          mov es:[bx+1h][si],al
 67          inc bx
 68          inc si
 69          loop s4
 70 
 71 
 72          mov bx,0
 73          mov cx,32
 74          mov si,3840
 75 
 76 s251:      mov ax,'-'
 77          mov es:[bx+0h][si],ax
 78          mov al,23               ;Store color attribute value blue
 79          mov es:[bx+1h][si],al
 80          inc bx
 81          inc si
 82          loop s251
 83 
 84 
 85          mov bx,0
 86          mov cx,80
 87          mov si,3872
 88 
 89 s252:      mov ax,'-'
 90          mov es:[bx+0h][si],ax
 91          mov al,23               ;Store color attribute value blue
 92          mov es:[bx+1h][si],al
 93          inc bx
 94          inc si
 95          loop s252
 96 
 97 
 98 
 99          mov bx,0
100          mov cx,len
101          mov si,2080
102 
103 s25:      mov ax,[bx]
104          mov es:[bx+720h][si],ax
105          mov al,23            ;White background blue
106          mov es:[bx+721h][si],al
107          inc bx
108          inc si
109          loop s25
110 
111          mov ax,4c00h
112          int 21h
113 code ends
114 end start

Screenshot of running test

 

Posted on Sun, 28 Nov 2021 01:55:01 -0500 by Boudga