Experiment 3 transfer instruction jump principle and its simple application programming

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

    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

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. question answering
    ① 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.
    ② 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.
    ③ Attach the disassembly screenshot of debugging observation in debug during the above analysis

experimental result

The jump displacement is F2, which is converted to decimal, i.e. 242 bytes.
When the program runs to loop s1, the instruction register IP=001B points to the next instruction, while 001B+00F2=010D, the high bit is discarded, that is, IP points to 000D.

The jump displacement is F0 and converted to decimal, i.e. 240 bytes.
When the program runs to loop s2, the instruction register IP=0039 points to the next instruction, while 0037+00F0=0129, the high bit is discarded, that is, IP points to 0029.

The screenshots of disassembly and program operation are shown above.

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) =?
② 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.

experimental result

ax = s1
bx = s2
cx = cs
The instruction call word ptr ds:[0] performs a short transfer and only records the IP value of the next instruction, that is, s1.
Instruction call dword ptr ds:[2] execute long transfer, jump out of the memory space of the program, and record the CS value and IP value of an instruction, namely CS and s2.

By compiling and linking the source program, it can be verified that ax, BX and CX comply with the above analysis.

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.

experimental result

The code is as follows:
task3.asm

assume cs:code, ds:data

data segment
        x db 99, 72, 85, 63, 89, 97, 55
    len equ $- x
data ends

stack segment
        db 16 dup(0)
stack ends

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

    mov cx, len
    mov si, offset x
s:    mov ah, 0
    mov al, ds:[si]
    call printNumber
    call printSpace
    inc si
loop s

    mov ah, 4ch
    int 21h

printNumber:
    mov bl, 10
    div bl
    mov dl, al

    mov dh, ah

    or dl, 30H    ;Numeric to character
    mov ah, 2
    int 21h        ;Output high

    mov dl, dh
    or dl, 30H    ;Numeric to character
    int 21h        ;Output low
ret

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

code ends
end start

experimental result

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
Inlet 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, the offset of the starting address of the string)
Move address - > 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 black at the bottom of the screen
The string is displayed in red at the bottom

experimental result

The code is as follows:

assume cs:code, ds:data

data segment
        str db 'try'
    len equ $ - str
data ends

stack segment
        db 16 dup(0)
stack ends

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

    mov cx, len
    mov si, 0
    mov bl, 2    ;green
    mov bh, 0    ;first line
    mov di, bh    ;Line beginning address
    call printStr

    mov bh, 24    ;Last line
    ;The space occupied by each row is 00 A0H
    mov bl, bh
    mov bh, 0
    mov cx, bx
    mov ax, 0
findLastRow:
    add ax, 00A0H
loop findLastRow
    mov di, ax

    mov cx, len
    mov si, 0
    mov bl, 4    ;red
    call printStr

    mov ah, 4ch
    int 21h

printStr:
s:
    mov al, ds:[si]
    mov ah, bl
    mov es:[di], ax
    inc si
    inc di
    inc di
loop s
ret

code ends
end start

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.

experimental result

The code is as follows:

assume cs:code, ds:data

data segment
    stu_no db '201983290035'
    len = $ - stu_no
data ends

stack segment
        db 16 dup(0)
stack ends

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

    mov bl, 17H    ;White characters on blue background
    mov si, 0
    call setBgColor

    mov bh, 24    ;Last line
    ;The space occupied by each row is 00 A0H
    mov bl, bh
    mov bh, 0
    mov cx, bx
    mov ax, 0
findLastRow:
    add ax, 00A0H
loop findLastRow
    mov di, ax

    ;Front white thread length 34
    mov al, '-'
    mov cx, 34
    mov bl, 17H    ;White characters on blue background
    call printLine

    mov cx, len
    mov si, 0
    mov bl, 17H    ;White characters on blue background
    call printStr

    ;Rear white thread length 34
    mov al, '-'
    mov cx, 34
    mov bl, 17H    ;White characters on blue background
    call printLine

    mov ah, 4ch
    int 21h

printStr:
s:
    mov al, ds:[si]
    mov ah, bl
    mov es:[di], ax
    inc si
    inc di
    inc di
loop s
ret

printLine:
s1:
    mov al, '-'
    mov ah, bl
    mov es:[di], ax
    inc di
    inc di
loop s1
ret

setBgColor:
    mov cx, 2000    ;80*25
s2:
    mov al, ' '
    mov ah, bl
    mov es:[si], ax
    inc si
    inc si
loop s2
ret

code ends
end start

Keywords: Assembly Language debug asm

Added by GrexP on Fri, 03 Dec 2021 05:51:21 +0200