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

1. 实验任务1

源码

assume cs:code, ds:data
data segment
x db 1, 9, 3
len1 equ $ - x ; 符号常量, $指下一个数据项的偏移地址,这个示例中,是3
y dw 1, 9, 3
len2 equ $ - y ; 符号常量, $指下一个数据项的偏移地址,这个示例中,是9
data ends
code segment
start:
mov ax, data
mov ds, ax
mov si, offset x ; 取符号x对应的偏移地址0 -> si
mov cx, len1 ; 从符号x开始的连续字节数据项个数 -> cx
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 ; 取符号y对应的偏移地址3 -> si
mov cx, len2/2 ; 从符号y开始的连续字数据项个数 -> cx
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其后指令的偏移地址的。

 

 

机器码为E2F2,F2为-14的补码,偏移量为14。

从CPU角度,IP地址根据当前指令长度更新为(IP)+指令长度,因此当前IP地址为001B,跳转到000D,他们之间相减即为偏移量,它们之间的偏移量为14。

 

回答问题②

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

 

 

 

 

机器码为E2F0,F2为-16的补码,偏移量为16。

从CPU角度,IP地址根据当前指令长度更新为(IP)+指令长度,因此当前IP地址为0039,跳转到0029,他们之间相减即为偏移量,它们之间的偏移量为16。

 

2. 实验任务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

寄存器(ax) = 0021 寄存器 (bx) = 0026 寄存器(cx) = 076C

调试截图

 

 

 

 

 offset s1,offset s2获得标号为s1,s2的偏移地址,存入内存单元。执行第一个call指令时,首先将下一个指令的偏移地址0021存入栈中,然后跳到ds:[0]所指向的偏移地址,即标号s1的偏移地址0021,然后将栈中数据0021存入ax;执行第二个call指令时,首先将下一个指令的段地址076E和偏移地址0026存入栈中,然后跳到ds:[2]所指向的偏移地址,即标号s2的段地址076E和偏移地址0026,然后将栈中段地址076E存入cx,偏移地址0026存入bx。

3. 实验任务3

 源码

assume ds:data, cs:code
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
    mov byte ptr ds:[10], 10

s1:    mov ah, 0
    mov al, ds:[si]

    div byte ptr ds:[10]
    call printNumber
    call printSpace
    inc si
    loop s1

    mov ax, 4c00h
    int 21h

printNumber:mov bx, ax
    or bl, 30h
    or bh, 30h
    mov ah, 2
    mov dl, bl
    int 21h
    mov dl, bh
    int 21h
    ret

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

code ends
end start

截图

 

 

 

 

4. 实验任务4

源码

assume ds:data, cs:code
data segment
    str db 'try'
    len equ $ - str
data ends

code segment
start:
    mov ax, data
    mov ds, ax
    mov cx, len
    mov si, offset str
    mov ax, 0b800h
    mov es, ax
    mov di, 0
    mov ah, 2

s:    call printStr
    inc si
    add di, 2
    loop s

    mov di, 3840
    mov si, offset str
    mov cx, len
    mov ah, 4

s1:    call printStr
    inc si
    add di, 2
    loop s1

    mov ax, 4c00h
    int 21h

printStr:mov al, [si]
    mov es:[di], al
    mov es:[di+1], ah
    ret

code ends
end start

截图

 

 

 

 

 

5. 实验任务5

 源码

assume ds:data, cs:code
data segment
    stu_no db '201983290455'
    len = $ - stu_no
data ends

code segment
start:
    mov ax, data
    mov ds, ax
    mov cx, 4000
    mov si, offset stu_no
    mov ax, 0b800h
    mov es, ax
    mov di, 0
    mov ah,17h

s:    mov al, 0
    mov es:[di], al
    mov es:[di+1], ah
    inc si
    add di, 2
    loop s

    mov di, 3840
    mov si, offset stu_no
    mov cx, 74
    mov ah, 17h
s1:    call printgang
    add di, 2
    loop s1

    mov di, 3908
    mov si, offset stu_no
    mov cx, len
    mov ah, 17h
s2:    call printStu_no
    inc si
    add di, 2
    loop s2

    mov di, 3932
    mov si, offset stu_no
    mov cx, 74
    mov ah, 17h
s3:    call printgang
    add di, 2
    loop s3

    mov ax, 4c00h
    int 21h

printStu_no:mov al, [si]
    mov es:[di], al
    mov es:[di+1], ah
    ret

printgang:mov al, 45
    mov es:[di], al
    mov es:[di + 1], ah
    ret

code ends
end start

截图

 

posted @ 2021-11-26 08:56  风雨2  阅读(19)  评论(2编辑  收藏  举报