Experiment 3 transfer instruction jump principle and its simple application programming

Posted by sugarat on Sun, 28 Nov 2021 04:31:30 +0100

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

 

  According to the disassembly, you can see that the machine code of the loop instruction is "E2F2", and F2 is the complement form of the displacement, which is converted to decimal is - 14, that is, the jump displacement.

The offset address of the next instruction of the loop instruction is 001B, plus the jump displacement, that is, 001B-E=000D, which is just the offset address of the label S1 instruction.

 


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

 

 

  According to the disassembly, we can see that the loop machine code is "E2F0", and F0 is the complement form of the displacement, which is converted to decimal is - 16, that is, the jump displacement.

The offset address of the next instruction of the loop instruction is 0039, plus the jump displacement, that is, 0039-0010 = 0029, which is just the offset address of the label S2 instruction.


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

The first call instruction puts the offset of the instruction s1 after it on the stack, jumps to the code executing s1, takes the top byte of the stack out of the stack and assigns it to ax, which is the offset of the s1 tag address

The second call instruction successively puts the segment address cs of the current code segment and the offset address of the next instruction s2 on the stack, and then jumps to the code executing s2. First assign the offset address of s2 to bx, and then assign the segment address cs to cx. Therefore, bx is the offset address marked by s2, and cx is the segment address of cs


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

It can be seen that at the beginning, the offset address of mark s1 is 0021, the segment address of mark s2 is 076c, and the offset address is 0026

 

 

After execution, you can see ax=0021, bx=0026, cx=076c

 

Experiment 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 si, 0
    mov cx, len
    s:
    mov ah, 0
    mov al, [si]
    inc si
    call printNumber
    call printSpace
    loop s

    mov ah, 4ch
    int 21h

printNumber:
    mov bl, 10
    div bl
    mov bx, ax
    mov ah, 2
    add bl, 48
    mov dl, bl  ;
    int 21h

    add bh, 48
    mov dl, bh  ;
    int 21h
    ret

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

code ends
end start

The experimental results are as follows:

 

 

 

 

 

 

Experiment task 4:

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 si,offset str
    mov cx,len
    mov bl,2
    mov bh,0
    call printStr

    mov si,offset str
    mov cx,len
    mov bl,4
    mov bh,24
    call printStr

    mov ah, 4ch
    int 21h

printStr:
    mov al,bh
    push bx
    mov bl,160
    mul bl
    mov di,ax
    pop bx
  s:mov al,[si]
    mov es:[di],al
    mov es:[di+1],bl
    inc si
    add di,2
    loop s
    ret

code ends
end start

The experimental results are as follows:

 

 

Experiment task 5:

assume cs:code, ds:data

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

code segment
start:
    mov ax, data
    mov ds, ax
    mov ax, 0b800h
    mov es, ax
    mov si, 1
    mov cx, 7d0h    ;
    s:
    mov byte ptr es:[si],17h    ;
    add si, 2
    loop s
 
    mov si, 0f00h
    mov cx, 80
    s1:
    mov byte ptr es:[si], 2dh  ;
    add si, 2
    loop s1

    mov si, 0f44h   ;
    mov cx, 12
    mov bx, 0
    s2:
    mov al, [bx]
    mov es:[si], al
    add si, 2
    inc bx
    loop s2

    mov ax, 4c00h
    int 21h 

code ends
end start

The experimental results are as follows: