实验3 转移指令跳转原理及其简单应用编程
四、实验结论
1. 实验任务1
(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
- 运行截图:
(2)回答问题①
① line27, 汇编指令 loop s1 跳转时,是根据位移量跳转的。通过debug反汇编,查看其机
器码,分析其跳转的位移量是多少?(位移量数值以十进制数值回答)从CPU的角度,说明
是如何计算得到跳转后标号s1其后指令的偏移地址的。
- 由上图可知,执行完
loop s1
指令后,IP为001B
跳转到000D
,所以跳转的位移量=000D-001B=-14(十进制)。 - 从CPU的角度,反汇编后查看机器码 :
loop 000D
的机器码为E2F2,F2为-14的补码。
回答问题②
② line44,汇编指令 loop s2 跳转时,是根据位移量跳转的。通过debug反汇编,查看其机
器码,分析其跳转的位移量是多少?(位移量数值以十进制数值回答)从CPU的角度,说明
是如何计算得到跳转后标号s2其后指令的偏移地址的。
- 同①,执行完
loop s2
,IP为0039
跳转到0029
,所以其跳转的位移量=0029-0039=-16(十进制)。 - 从CPU的角度,反汇编后查看机器码:
loop 0029
的机器码为E2F0
,F0为-16的补码。
问题③
③ 附上上述分析时,在debug中进行调试观察的反汇编截图。
见①回答中的反汇编截图。
2. 实验任务2
(1)给出程序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
(2)给出分析、调试、验证后,寄存器(ax) = ?(bx) = ? (cx) = ? 附上调试结果界面截图。
① 根据call指令的跳转原理,先从理论上分析,程序执行到退出(line31)之前,寄存器(ax) =
0021h ,寄存器(bx) = 0026h ,寄存器(cx) = 076ch
==>因为call word ptr ds:[0]
是短转移,此时将当前ip即s1对应的ip压栈;
call dword ptr ds:[2]
是长转移,此时将cs:ip即cs:s2压栈。
② 对源程序进行汇编、链接,得到可执行程序task2.exe。使用debug调试,观察、验证调试
结果与理论分析结果是否一致。
由上图可知,与理论分析结果一致。
3. 实验任务3
(1)给出程序源码task3.asm
assume cs:code, ds:data
data segment
x db 99,72,85,63,89,97,55
len equ $- x ;$指下一个数据项的偏移地址=7
data ends
code segment
start:
mov ax,data
mov ds,ax
mov cx,len
mov si,offset x
s:
mov ah,0
mov al,ds:[si]
call printNumber
call printSpace
inc si
loop s
mov ah,4ch
int 21h
printNumber:
mov bl,10
div bl ;分离十位数和个位数
mov bh,ah ;余数
mov dl,al ;al中为商
or dl,30h ;ascii数字变字符
mov ah,2
int 21h ;输出十位数
mov dl,bh
or dl,30h
int 21h ;输出个位数
ret
printSpace:
mov ah,2
mov dl,' '
int 21h
ret
code ends
end start
(2)运行测试截图:
4. 实验任务4
(1)给出程序源码task4.asm
assume cs:code, ds:data
data segment
str db 'try', 0
len equ $ - str
data ends
code segment
start:
mov ax, data
mov ds, ax
mov ax,0b800h ;显示区缓存地址开始位置
mov es,ax
mov si,offset printStr
mov ah, 2 ;绿
mov bx,0 ;在第一行打印
call si ; 第一次调用
mov si,offset printStr
mov ah,4 ;红
mov bx,3840 ;一行80个字符160字节,最后为24行,24*160=3840
call si
mov ah, 4ch
int 21h
printStr:
mov cx,len
mov si,0
s:
mov al,[si]
mov es:[bx+si],ax
inc si
inc bx
loop s
ret
code ends
end start
(2)运行测试截图:
5. 实验任务5
(1)给出程序源码task5.asm
assume cs:code,ds:data
data segment
stu_no db '201983440043'
len = $ - stu_no
data ends
code segment
start:
mov ax,data
mov ds,ax
mov ax,0b800h
mov es,ax
mov si,offset stu_no
mov di,0
mov al,17h ;蓝底白字:0 001 0 111为17h
call printBgcolor
call printHyphen
call printStuno
call printHyphen
mov ah,4ch
int 21h
;打印背景颜色为蓝色
printBgcolor:
mov cx,1920 ;背景颜色占24行,一行80个字符:24*80
s:
inc di
mov es:[di],al;
inc di
loop s
ret
;打印学号前后的34个'-'
printHyphen:
mov cx,34
s1:
mov word ptr es:[di],'-'
inc di
mov es:[di],al
inc di
loop s1
ret
;打印学号
printStuno:
mov cx,len ; stu_no长度
s2:
mov bl,ds:[si]
mov es:[di],bl
inc di
mov es:[di],al
inc si
inc di
loop s2
ret
code ends
end start
(2)运行测试截图: