Experiment 3 transfer instruction jump principle and its simple application programming

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. Experimental task 1
Using any text editor, enter the 8086 assembler source code task1.asm.

task1.asm

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
  add si, 2
  loop s2
  mov ah, 4ch
  int 21h
code ends
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,

Analyze the displacement of its jump? (the displacement value is answered in decimal) from the perspective of CPU, explain how it is calculated
To the offset address of the instruction after the jump label s1.

 

When reading the assembly instruction loop s1, the IP points to 001B, and after executing the instruction, the IP points to 000D. The jump displacement is - 14.

F2 in the machine code E2F2 corresponding to the instruction is the hexadecimal complement of jump displacement - 14. The CPU adds the jump displacement to the IP value to obtain the IP of the next instruction to be executed, that is, 000D.


② line44. When the assembly instruction loop s2 jumps, it jumps according to the displacement. Check the machine code through debug disassembly,
Analyze the displacement of its jump? (the displacement value is answered in decimal) from the perspective of CPU, explain how it is calculated
To the offset address of the instruction after the jump label s2.

 

When reading the assembly instruction loop s2, the IP points to 0039, and after executing the instruction, the IP points to 0029. The jump displacement is - 16.

F0 in the machine code E2F0 corresponding to the instruction is the hexadecimal complement of jump displacement - 16. The CPU adds the jump displacement to the IP value to obtain the IP of the next instruction to be executed, i.e. 0029.

 

③ Attach the disassembly screenshot of debugging observation in debug during the above analysis

 

 

2. Experimental task 2
Using any text editor, enter the 8086 assembler source code task2.asm.
task2.asm

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 register (ax) =? Register before program execution and exit (line31)
(bx) =? Register (cx) =?

 

ax=0021;bx=0026;cx=076C

When the call instruction is executed, the cpu pushes the next instruction (ip or cs) onto the stack, and then pops   Ax will stack the content to ax and pop   bx will stack the ip to bx and pop   cx will stack cs to cx.


② Assemble and link the source program to get the executable program task2.exe. Use debug to observe and verify the debugging results and theory
Whether the analysis results are consistent.

  Observation shows that the analysis is consistent.

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 8086 assembly source program task3.asm to output this group of continuous data, data and data in the data section in decimal form on the screen
Space between.
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.
When correctly written, the expected test results are as follows:

The description of sub function 2 in attachment *: int 21h is as follows:  

; Function: output single character
mov  ah, 2
mov  dl, ××   ; ××Is the character to be output, or its ASCⅡCode value
int 21h

 

Code after writing:

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 cx, len ;Data is byte,len Is the data length
  s:
   mov ah, 0 ;The number has only one byte, so ah=0
   mov al, [si]
   mov bx, offset printNumber
   call bx
   mov bx, offset printSpace
   call bx
   inc si
   loop s
  mov ah, 4ch
  int 21h

  printNumber:
    mov bl, 10
    div bl;Separate ten bits and one bit
    mov bx, ax

    mov dl, bl ;Quotient
    add dl, 30h ;convert to ascii
    mov ah, 2 ;call int 21h Subroutine 2 of
    int 21h;

    mov dl, bh ;Remainder
    add dl, 30h ;convert to ascii
    mov ah, 2 ;call int 21h Subroutine 2 of
    int 21h;
  ret

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

code ends
end start

result:

 

 

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
When correctly written, the expected test results are as follows:

 

Code after writing:

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 str
    mov bl,    2h    ;String color
    mov bh,    0    ;Specify row
    call printStr

    mov si, offset str
    mov bl, 4h
    mov bh, 24
    call printStr

    mov ah, 4ch
    int 21h

;Displays the string on the screen in the specified line and in the specified color
printStr:
;Calculate the first offset address corresponding to the line number
    mov al, bh
    mov dl, 00A0h
    mul dl

    mov di, ax
    mov cx, len    ;From symbol str Number of consecutive byte data items at the beginning
s:
    mov al, ds:[si]
    mov es:[di], al
    inc di
    mov es:[di], bl
    inc si
    inc di
    loop s
    ret

code ends
end start

 

result:

 

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 to output the blue background of the window, student number and broken lines on both sides to
White foreground color display.
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.
After the program is written correctly, the expected output effect is as follows:

Code after writing:

assume cs:code, ds:data
data segment
  stu_no db '201983290051'
  len = $ - stu_no
data ends

code segment
start:
  mov ax, data
  mov ds, ax
  mov ax, 0b800h
  mov es, ax
  ;Tone background color
  mov si, 1
  mov dl, 17h ; 0 001 0 111
  mov cx, 2000
  blue:
    mov es:[si], dl
    add si, 2
    loop blue
  ;Print horizontal lines
  mov dh, 24
  mov al ,160
  mul dh ;Calculation start position
  mov bx, ax
  call line

  mov cx, len
  mov si, 0
  sn:
    mov dl, ds:[si]
    mov es:[bx], dl
    inc si
    add bx, 2
    loop sn

  call line
  mov ax, 4c00h
  int 21h

  line:
    mov dl, '-'
    mov cx, 34 ;( 80- 12 ) / 2
    s:
      mov es:[bx], dl
      add bx, 2
      loop s
    ret
code ends
end start

result:

 

 

 

 

 

 

assume cs:code, ds:data
data segment    str db 'try'    len equ $ - strdata ends
code segmentstart:    mov ax, data    mov ds, ax    mov ax, 0b800h    mov es, ax
    mov si, offset str     mov bl,     2h     ; String color     mov bh,     0     ; Specify row     call printStr
    mov si, offset str    mov bl, 4h    mov bh, 24    call printStr
    mov ah, 4ch    int 21h
; In the specified line and in the specified color, the string printStr: is displayed on the screen; Calculate the first offset address corresponding to the line number     mov al, bh     mov dl, 00A0h     mul dl
    mov di, ax     mov cx, len     ; Number of consecutive byte data items starting from the symbol str s:     mov al, ds:[si]     mov es:[di], al     inc di     mov es:[di], bl     inc si     inc di     loop s     ret
code endsend start

Added by nay4 on Mon, 29 Nov 2021 13:59:17 +0200