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

 

四、实验结论

1.实验任务1

(1)程序源码

 1 assume cs:code, ds:data
 2 
 3 data segment
 4     x db 1, 9, 3
 5     len1 equ $ - x
 6     
 7     y dw 1, 9, 3
 8     len2 equ $ - y 
 9 data ends
10 
11 code segment
12 start:
13     mov ax, data
14     mov ds, ax
15     
16     mov si, offset x
17     mov cx, len1
18     mov ah, 2
19 s1:    mov dl, [si]
20     or dl, 30h
21     int 21h
22     
23     mov dl, ' '
24     int 21h
25     
26     inc si
27     loop s1
28     
29     mov ah, 2
30     mov dl, 0ah
31     int 21h 
32     
33     mov si, offset y 
34     mov cx, len2/2
35     mov ah, 2
36 s2:    mov dx, [si]
37     or dl, 30h
38     inu39 
40     mov dl, ' '
41     int 21h
42 
43     add si, 2
44     loop s2
45     
46     mov ah, 4ch
47     int 21h
48 code ends
49 end start

程序运行截图:

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

 跳转位移是(000D-001B)的补码为F2,位移量为-14

跳转后CS、IP值修改为s1处指令的段地址和偏移地址,计算当前指令的长度,IP值加上该长度即得到下条指令的位置。

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

跳转位移是(0029-0039)的补码用十六进制表示为F0,位移量为-16

 

2.实验任务2

(1)程序源码

 1 assume cs:code, ds:data
 2 
 3 data segment
 4     dw 200h, 0h, 230h, 0h
 5 data ends
 6 
 7 stack segment
 8     db 16 dup(0)
 9 stack ends
10 
11 code segment
12 start:  
13     mov ax, data
14     mov ds, ax
15 
16     mov word ptr ds:[0], offset s1
17     mov word ptr ds:[2], offset s2
18     mov ds:[4], cs
19 
20     mov ax, stack
21     mov ss, ax
22     mov sp, 16
23 
24     call word ptr ds:[0]  
25 s1: pop ax
26 
27     call dword ptr ds:[2]
28 s2: pop bx
29     pop cx
30 
31     mov ah, 4ch
32     int 21h
33 code ends
34 end start

(2)给出分析、调试、验证后,寄存器(ax) = ? (bx) = ? (cx) = ? 附上调试结果界面截图。

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

 call word ptr ds:[0]执行后,将下条指令的IP地址即s1的偏移地址压入栈中,然后跳转到ds:[0]处(即s1)执行指令,从栈中弹出的值赋给ax。故ax=s1的偏移地址。

    call dword ptr ds:[2]执行后,将当前的CS、IP值(s2的指令地址)依次压入栈中,然后跳转到ds:[2]处(即s2)执行指令,栈顶弹出IP赋给bx,CS赋给cx。故bx=s2的偏移地址,cx=s2的段地址。

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

 结果与分析一致。

 

3.实验任务3

 (1)源码

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

    mov ax,4c00h
    int 21h

printNumber: 
    mov bl,10
    div bl ; al:十位 ah:个位
    mov bx,ax  
    or bl,30h  ;转换为ASCII值
    mov ah,2
    mov dl,bl
    int 21h 
          
    or bh,30h
    mov dl,bh
    int 21h
    ret
    
printSpace:
    mov dl,' '
    int 21h
    ret

code ends
end start

运行结果:

 

4.实验任务4

(1)源程序

 1 assume cs:code,ds:data,ss:stack
 2 data segment
 3     str db 'try'
 4     len equ $ - str
 5 data ends
 6 
 7 stack segment
 8     db 16 dup(0)
 9 stack ends
10 
11 code segment
12 start:
13     mov ax,data
14     mov ds,ax
15 
16     mov ax,stack
17     mov ss,ax
18     mov sp,16
19     
20     mov ax,0b800h
21     mov es,ax  ;显存地址空间
22 
23     mov si,offset str
24     mov cx,len
25     mov bl,2h    ;黑底绿字
26     mov bh,0    
27     call printStr
28     
29     mov si,offset str
30     mov cx,len
31     mov bl,4h  ;黑底红字
32     mov bh,24
33     call printStr
34     mov ah,4ch 
35     int 21h
36 
37 printStr: push bx 
38     mov al,0a0h ;一行占160个字节
39     mul bh   ;160*bh->ax
40     mov bx,ax ;把偏移地址存在bx里
41     pop ax 
42     mov ah,al;高位存放属性信息
43 
44 s:  mov al,[si] ;低位存放字符 
45     mov es:[bx+si],ax
46     inc bx
47     inc si
48     loop s
49     ret
50 
51 code ends
52 end start

(2)运行结果

 

5.实验任务5

(1)源程序

 1 assume ds:data,cs:code
 2 data segment
 3     stu_no db '201983290284'
 4     len = $ - stu_no
 5 data ends
 6 
 7 code segment
 8 start:    mov ax,data
 9         mov ds,ax
10 
11         mov ax,0b800h
12         mov es,ax ;显存地址空间
13 
14         mov si,1
15         mov cx,7D0h ;25行的字符数
16 s:        mov byte ptr es:[si],17h
17         add si,2
18         loop s
19 
20         mov bx,0f00h
21         call prints
22         mov si,offset stu_no
23         mov cx,len
24 s2:        call printnum
25         loop s2
26         call prints
27         mov ah,4ch
28         int 21h
29 
30 prints:    mov cx,34
31 s3:        mov byte ptr es:[bx],'-'
32         add bx,2
33         loop s3
34         ret 
35 
36 printnum:mov al,[si]
37         or al,30h    ;转换成字符
38         mov byte ptr es:[bx],al
39         inc si
40         add bx,2
41         ret
42 code ends
43 end start

(1)运行结果

 

posted @ 2021-11-30 09:41  虾仁不眨眼~  阅读(29)  评论(3)    收藏  举报