实验3 转移指令跳转原理及其简单应用编程
-------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------
1. 实验任务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
1.2 实验结果截图
1.3 回答问题
回答问题:
① line27, 汇编指令 loop s1 跳转时,是根据位移量跳转的。通过debug反汇编,查看其机器码, 分析其跳转的位移量是多少?(位移量数值以十进制数值回答)从CPU的角度,说明是如何计算得 到跳转后标号s1其后指令的偏移地址的。
(1)位移量:14;
(2)分析过程:
-
当CS:IP指向0771:0019的时候 ,下一条loop指令机器码E2 F2;
-
此后,E2 F2进入指令缓冲器;
-
此时CX不为零,所以执行该指令
- $IP = IP + 所读取到的指令的长度$ ==$IP + 2 = 001B$ ,CS:IP指向mov ah,2;
- CPU执行缓冲器中指令;执行后IP = 000D,CS:IP指向mov dl,[si];
② line44,汇编指令 loop s2 跳转时,是根据位移量跳转的。通过debug反汇编,查看其机器码, 分析其跳转的位移量是多少?(位移量数值以十进制数值回答)从CPU的角度,说明是如何计算得 到跳转后标号s2其后指令的偏移地址的。
(1) 位移量:16;
(2) 分析:,loop s2
的机器码为E2F0。F2的八位二进制形式为11110000,补码为10010000,为-16。即位移量为16;
-------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------
2.实验任务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.2 实验结果截图
2.2.1 结果过程原理分析:
- call指令的跳转原理
- ! 调用子过程跳转,将当前(CS+IP)或者iP压入栈中。
- !! 跳转
2.2.2 结果推理过程:
- 据此,ax的值应由该指令推得,即该指令压入栈中的IP:
call word ptr ds:[0]
故,应为0021,即21;
- 同理,bx的值则由如下指令得到,应等于压入栈中的IP
call dword ptr ds:[2]
- cx的值为076C
-------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------
3 实验任务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 byte ptr bl,10 mov si,offset x mov cx,len s: mov al,ds:[si] mov ah,0 div bl call printNumber call printSpace inc si loop s mov ah,4ch int 21h printNumber: mov dx,ax mov ah,2 or dl,30h int 21h mov dl,dh or dl,30h int 21h ret printSpace: mov ah,2 mov dl,' ' int 21h ret code ends end start
3.1 实验结果截图
-------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------
4.实验4
4.1 实验源码
assume cs:code, ds:data data segment str db 'try' len = $-str data ends stack segment db 16 dup(0) stack ends code segment start: mov ax,data mov ds,ax mov ax,stack mov ss,ax mov sp,16 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 dx,0b800h mov es,dx mov ah,0 mov al,bh mov di,160 mul di mov di,ax s: mov al,ds:[si] mov es:[di],al inc di mov es:[di],bl inc di inc si loop s ret code ends end start
4.2 实验结果截图