Experiment 3 transfer instruction jump principle and its simple application programming

Posted by banks1850 on Sun, 28 Nov 2021 12:46:09 +0100

1. Experimental task 1

The program task1.asm source code and running screenshot are given

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.

-14

Cpu executes E2F2, which is the complement of decimal-14, that is, ip moves 14 bytes forward to 076B:000D.

②       line44. When the assembly instruction 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.

-16

Cpu executes e2f0, which is the complement of decimal-16, that is, ip moves 16 bytes forward to 076B:000D.

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

2. Experimental task 2

The program task2.asm source code is given

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

      Register (ax) = 0021H register (bx) =0026H   Register (cx) =076CH


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

call word ptr ds:[0] assign ip stack to AX, so it is 0021H in AX

call dword ptr ds:[2] put cs and IP on the stack, and then assign the value in cs to CX and IP to bx. Therefore, 0026 in bx and 076C in Cx

 

3. Experimental task 3

The program source code task3.asm is given

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, offset x
    mov cx, len

 s1: mov ah,0
    mov al,[si]
    push ax
    call printNumber
    call printSpace
    pop ax
    inc si
    loop s1 
    
    mov ah, 4ch
    int 21h
printNumber:
    mov bl,10
    div bl
    mov bx,ax
    mov ah,2

    or bl, 30h
    mov dl,bl
    int 21h

    or bh, 30h
    mov dl,bh
    int 21h
    ret

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



    
code ends
end start

 

Screenshot of running test

The experiment requires that the output is two digits, that is, after being divided by 10, the remainder and quotient represent one digit and ten digits respectively. Although the above code can meet the experimental requirements, it is not applicable if it is three digits or more. In this case, it is necessary to increase the cycle, press the remainder of each division into the stack, and then output one by one.

 

4. Experimental task 4

The program source code task4.asm is given

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,2     ;attribute
    mov bh,0     ;Line number
    call printStr;Output above green

    mov si,offset str
    mov bl,4
    mov bh,24
    call printStr;Output below red
    
    mov ah, 4ch
    int 21h

printStr:  
    mov al, bh      
    mov dl, 0A0h    
    mul dl      ;Calculate offset address    
    mov di, ax     
    mov cx,len
   s:
   
     mov al,[si]
     mov ah,bl
     
     mov es:[di],ax
     inc si
     add di,2
   loop s
   ret

code ends
end start

 

Screenshot of running test

5. Experimental task 5

The program source code task5.asm is given

assume cs:code, ds:data

data segment

stu_no db '201983290532' 
len = $ - stu_no

data ends

code segment
start:

    mov ax, data
    mov ds, ax
    mov ax,0B800H
    mov es,ax

    mov cx,0780H
    mov ah,00010000b
    mov al,' '
    mov bx,0
back:  mov es:[bx],ax;Output blue background
    add bx,2
    loop back
    
    mov cx,80
    mov ah,00010111b
    mov al,'-'
vet: mov es:[bx],ax;Output all of the last row'-'
    add bx,2
    loop vet

    mov cx,len
    mov bx,0F44H
    mov si,0
letter: mov al,[si];Output student number
    mov es:[bx],ax
    inc si
    add bx,2
    loop letter
    mov ah, 4ch
    int 21h

code ends
end start

Screenshot of running test

When outputting the student number, the offset address is 0f44h calculated by yourself. If it is more complete, the offset address should be obtained by multiplying the line number of 25-1 = 24 by 160 characters and adding [(80-12) / 2] 16.