Experiment 3 transfer instruction jump principle and its simple application programming

Posted by dannylewin on Tue, 30 Nov 2021 08:14:58 +0100

Experimental task 1

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 displacement is: 14 (the end address of Loop instruction is 001B, the start address of s1 instruction is 000D, 001B-000D=14)

Analysis: the or dl, 30h command occupies three bytes, the inc instruction occupies one byte, and other instructions occupy two bytes respectively. The total is 14 bytes.

 

  ② 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 (Loop instruction end address: 0039, s1 instruction start address: 0029001B-000D=16)

Analysis: the or DL and 30h commands occupy three bytes, and other instructions occupy two bytes respectively. The total is 16 bytes.

 

Experimental task 2

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

A: theoretically, the data in ax should be the IP address of the instruction s1:pop ax. Because   call word ptr ds:[0] pushes the IP of the s1:pop ax instruction onto the stack.

bx should be the IP of s2:pop bx, and cx should be the CS of s2:pop bx.

Because   call dword ptr ds:[2]   Instruction will   S2: CS and IP of pop BX are successively pushed into the stack.

 

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

 

  Experimental task 3

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 ax, 0b800h
    mov es, ax
    mov bx,0
    mov si,0
    mov cx,len

    s:mov al, [si]
    mov ah, 0
    call printNumber
    add bx, 2
    call printSpace
    inc bx
    inc si
    loop s
    mov ax, 4c00h
    int 21h

    printNumber:
    mov dl, 10
    div dl
    or al, 30h ;merchant
    or ah, 30h   ;remainder
    mov dh, ah
    mov ah, 2
    mov dl, al
    int 21h
    mov dl, dh
    int 21h
    ret

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

code ends
end start

 

  Experimental task 4

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 dh, 0 
    mov bl, 2
    mov si, 0
    mov ax, 0b800h
    mov es, ax
    call printStr

    mov dh, 24
    mov bl, 4
    mov si, 0
    call printStr
    mov ax,4c00h
    int 21h

printStr:
    mov al, 160
    mul dh

    mov dx, ax
    mov al, bl
    mov bx, dx

    mov cx, len
s:  mov dl, [si]
    mov es:[bx], dl
    inc bx
    mov es:[bx], al
    inc bx
    inc si 
    loop s
    ret

code ends
end start

 

 

Experimental task 5

assume cs:code, ds:data

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

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

     mov si, 1
     mov dl, 17h
     mov cx, 2000
  bc:mov es:[si], dl
     add si, 2
    loop bc

     mov dh, 24
     mov al, 160
     mul dh
     mov bx, ax
     call minus

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

     call minus
     mov ax, 4c00h
     int 21h

 minus:
     mov dl, '-'
     mov cx, 34
   s:mov es:[bx], dl
     add bx, 2
     loop s
     ret
code ends
end start