Experiment 3 transfer instruction jump principle and its simple application programming

Experiment 3 transfer instruction jump principle and its simple application programming

Experiment 1

Using any text editor, enter the 8086 assembler source code task1.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 ;$Is the offset address of the next data, Equivalent to the current offset value of the segment currently being assembled.
 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

 

Assemble and link the source program to get the executable program task1.exe. After running, combined with the running results, comments and necessary debug
Commissioning:
1. Understand the flexible use of operator offset, pseudo instruction equ and predefined symbol $.
Continuous data items can be easily calculated through line5, line8 and data attributes (bytes, words, doublewords, etc.) of data items
Without manual counting.
Note *: the symbolic constants len1 and len2 do not occupy the memory space of the data segment
2. Answer questions
① 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.
 
8-bit shift = address at label - the address of the first byte after the loop instruction
    =001B-000D=27-13=14

 

  ② 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=0039h-0029h=10h=16

 

 

Experiment 2

 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 before the program executes to exit (line31), register (ax) =? Register (bx) =? Register (cx) =?

ax=0021 bx=0026 cx=076c

Ax refers to the IP when executing s1:pop ax instruction, bx refers to the IP when executing s2:pop bx instruction, and cx refers to the CS when executing s2:pop bx instruction.

  call word ptr ds:[0] pushes the IP of the s1:pop ax instruction onto the stack. Then call dword ptr ds:[2]   Instruction will   S2: the CS and IP of pop bx are successively pushed into the stack, so cx is the bottom of the stack, and bx and ax are at the top of the stack at the end

 

 

correct

Experiment 3

For 8086 CPU, the known logical segment is defined as follows:
1 data segment
2     x db 99, 72, 85, 63, 89, 97, 55
3     len equ $- x
4 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.

 

Grid spacing.
requirement:
        Write 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.
The description of sub function 2 in attachment *: int 21h is as follows:
1 ; Function: output single character
2 
3 mov ah,2
4 mov dl,  xx        ;xx Is the character to be output, or its ASCII Code value
5 int 21h

  Experiment code:

 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  si, 0
13                 mov  bl, 10 ;16
14                 mov  cx, len
15 
16     s:          mov  al, ds:[si]
17                 mov  ah, 0
18                 inc  si
19                 call printNumber
20                 call printSpace
21                 loop s
22  
23                 mov  ah, 4ch
24                 int  21h
25  
26     printNumber:
27                 div  bl
28                 mov  dx,ax
29  
30                 or   dl, 30h
31                 or   dh, 30h
32  
33                 mov  ah, 2;The remaining bits are high
34                 ;dl
35                 int  21h
36  
37                 mov  dl, dh
38                 int  21h
39  
40     printSpace: 
41                 mov  ah, 2
42                 mov  dl, 32
43                 int  21h
44                 ret
45  
46 code ends
47 end start

Experiment 4

For 8086 CPU, the known logical segment is defined as follows:
1 data segment
2      str db 'try'
3      len equ $ - str
4 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

 

 

 1 assume cs:code, ds:data
 2 data segment
 3     str  db  'try'
 4     len  equ $ - str
 5 data ends
 6 
 7 code segment
 8     start:   
 9              mov  ax, data
10              mov  ds, ax
11              mov  ax,0b800h
12              mov  es,ax
13 
14              mov  si,offset str
15              mov  cx,len
16              mov  bh,    0         ;Action line 1
17              mov  bl,    2        ;The string color is green on a black background
18              call printStr
19 
20              mov  si,offset str
21              mov  cx,len
22              mov  bl,  4          ; Specifies that the string color is red on a black background
23              mov  bh, 24           ;Specify the last line of the behavior
24              call printStr
25 
26              mov  ah, 4ch
27              int  21h
28 
29     printStr:
30              mov  al, 160
31              mul  bh
32              mov  di, ax
33              
34     s:       mov  ah, ds:[si];Take character
35              mov  es:[di], ah;character es High 8 bits
36              inc  di
37              mov  es:[di], bl;colour
38              inc  di
39              inc  si
40              loop s
41              ret
42 
43 code ends
44 end start
 

Experiment 5

For 8086 CPU, 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.
 1 assume ds:data, cs:code
 2 
 3 data segment
 4     stu db '201983310042'
 5     len    =  $ - stu
 6 data ends
 7 
 8 code segment
 9     start:
10           mov  ax,0b800h
11           mov  es,ax
12           mov  bp,1
13           mov  cx,8000h
14           mov  ax,data
15           mov  ds,ax
16 
17     s:
18           mov  byte ptr es:[bp],00011111B
19           add  bp,2
20           loop s
21 
22           mov  ax,0050h
23           sub  ax,len;Lose length
24           mov  bh,02h
25           div  bh
26           mov  bl,al;merchant
27           mov  bh,0
28 
29           mov  ax,0b800h
30           mov  es,ax
31           mov  bp,0F00h
32           mov  cx,bx
33 
34     p1:    
35           mov  byte ptr es:[bp],'-'
36           inc  bp
37           inc  bp
38           loop p1
39 
40           mov  cx,len;Student number in the middle
41           mov  di,0
42 
43     s1:   
44           mov  al,ds:[di]
45           mov  es:[bp],al
46           inc  bp
47           inc  bp
48           inc  di
49           loop s1
50 
51           mov  cx,bx
52 
53     p2:   
54           mov  byte ptr es:[bp],'-'
55           inc  bp
56           inc  bp
57           loop p2
58 
59           mov  ax,4c00h
60           int  21h
61 
62 code ends
63 end start

 

 

Posted by ADLE on Thu, 02 Dec 2021 13:39:54 -0800