Loading

实验3 转移指令跳转原理及其简单应用编程

实验任务 1

  • 给出程序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

回答问题①

① line27, 汇编指令 loop s1 跳转时,是根据位移量跳转的。通过debug反汇编,查看其机
器码,分析其跳转的位移量是多少?(位移量数值以十进制数值回答)从CPU的角度,说明
是如何计算得到跳转后标号s1其后指令的偏移地址的。

位移量为:0x001B-0x000D=-14

IP=(IP)-14=0x001B-14

回答问题②

② line44,汇编指令 loop s2 跳转时,是根据位移量跳转的。通过debug反汇编,查看其机
器码,分析其跳转的位移量是多少?(位移量数值以十进制数值回答)从CPU的角度,说明
是如何计算得到跳转后标号s2其后指令的偏移地址的。

位移量为:0x0039-0x0029=-16

IP=(IP)-16=0x0039-16

③ 附上上述分析时,在debug中进行调试观察的反汇编截图

shown above

实验任务 2

给出程序 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

① 根据call指令的跳转原理,先从理论上分析,程序执行到退出(line31)之前,寄存器(ax) = s1的偏移地址,寄存器(bx) = s2 的偏移地址,寄存器(cx) = cs

② 对源程序进行汇编、链接,得到可执行程序task2.exe。使用debug调试,观察、验证调试结果与理论分析结果是否一致。

给出分析、调试、验证后,寄存器(ax) = 0021 (bx) = 0026 (cx) = 076C。与分析一致。

实验任务 3

给出程序源码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, stack
    mov ss, ax
    mov sp, 16
    mov ax, 78
    mov cx, len
    mov ax, data
    mov ds, ax
    mov bx, 0
s1:
    mov ah, 0
    mov al, [bx]
    call printNumber
    call printSpace
    inc bx
    loop s1
    mov ah, 4ch
    int 21h
printNumber PROC NEAR
    push bx
    mov bh, 0ah
    div bh
    mov bx, ax
    mov ah, 2
    mov dl, bl
    or dl, 30h
    int 21h
    mov ah, 2
    mov dl, bh
    or dl, 30h
    int 21h
    pop bx
    ret
printNumber ENDP
printSpace PROC NEAR
    mov ah, 2
    mov dl, ' '
    int 21h
    ret
printSpace ENDP
code ends
end start

运行测试截图

实验任务 4

给出程序源码task4.asm

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, stack
    mov ss, ax
    mov sp, 16
    mov ax, 78
    mov ax, data
    mov ds, ax
    mov si, 0
    mov cx, len
    mov bl, 2
    mov bh, 0
    call printStr
    mov si, 0
    mov cx, len
    mov bl, 4
    mov bh, 24
    call printStr
    mov ah, 4ch
    int 21h
printStr PROC NEAR
    mov ax, 0b800H
    mov es, ax
    mov di, 0
    push cx
    mov ch, 0
    mov cl, bh
ps1:
    add di, 160
    loop ps1
    pop cx
ps2:
    mov al, [si]
    mov es:[di], al
    mov es:[di+1], bl
    inc si
    add di, 2
    loop ps2
    ret
printStr ENDP
code ends
end start

运行测试截图

实验任务 5

给出程序源码task5.asm

assume cs:code, ds:data
data segment
    stu_no db '201983290307'
    len = $ - stu_no
    line db 80 dup(' ')
    llen = $ - line
    lineend = $
data ends

stack segment
    db 16 dup(0)
stack ends

code segment
start:
    mov ax, stack
    mov ss, ax
    mov sp, 16
    mov ax, 78
    mov ax, data
    mov ds, ax
    mov cx, 24
    mov bh, 0
s1:
    push cx
    mov si, len
    mov cx, llen
    mov bl, 00010111b
    call printStr
    inc bh
    pop cx
    loop s1

    mov ax, 80
    sub ax, len
    mov bh, 2
    div bh
    mov cl, al
    mov ch, 0
    
    mov si, len
s2:
    mov byte ptr [si], '-'
    inc si
    loop s2
    
    mov bx, 0
    mov cx, len
s3:
    mov al, [bx]
    mov [si], al
    inc bx
    inc si
    loop s3

s4:
    mov byte ptr [si], '-'
    inc si
    cmp si, lineend
    jnz s4

    mov si, len
    mov cx, llen
    mov bl, 00010111b
    mov bh, 24
    call printStr


    mov ah, 4ch
    int 21h
printStr PROC NEAR
    mov ax, 0b800H
    mov es, ax
    mov di, 0
    push cx
    mov ch, 0
    mov cl, bh
ps1:
    add di, 160
    loop ps1
    pop cx
ps2:
    mov al, [si]
    mov es:[di], al
    mov es:[di+1], bl
    inc si
    add di, 2
    loop ps2
    ret
printStr ENDP
code ends
end start

运行测试截图

posted @ 2021-11-30 15:24  zskr  阅读(10)  评论(4编辑  收藏  举报