实验4 8086标志寄存器及中断

1. 实验任务1

① add指令对标志寄存器中的零标志位ZF(Zero Flag)、进位标志位CF(Carry Flag)是否有影响?
② inc指令对标志寄存器中的零标志位ZF(Zero Flag)、进位标志位CF(Carry Flag)是否有影响?
进入Debug编辑调试

1、查看结果得出add对零标志位有影响,因为溢出一位后剩余位为0。对进位标志位也有影响。
2、inc对零标志位有影响,对进位标志位没有影响,所以inc进位不会被存下来

使用任意文本编辑器,录入8086汇编源码task1.asm。
task1.asm:

assume cs:code, ds:data
 
data segment
    x dw 1020h, 2240h, 9522h, 5060h, 3359h, 6652h, 2530h, 7031h
    y dw 3210h, 5510h, 6066h, 5121h, 8801h, 6210h, 7119h, 3912h
 data ends
 code segment 
 start:
     mov ax, data
     mov ds, ax
     mov si, offset x
     mov di, offset y
     call add128
 
     mov ah, 4ch
     int 21h
 
 add128:
     push ax
     push cx
     push si
     push di
 
     sub ax, ax
 
     mov cx, 8
s:  mov ax, [si]
     adc ax, [di]
     mov [si], ax
 
     inc si
     inc si
     inc di
     inc di
     loop s
 
     pop di
     pop si
     pop cx
     pop ax
     ret
 code ends
 end start

对程序进行汇编、链接,得到可执行程序task1.exe。在debug中调试程序,并回答问题。
① line31~line34的4条inc指令,能否替换成如下代码?你的结论的依据/理由是什么?

add si, 2
add di, 2

在本题的数据中,可以替换成add si,2和add di,2。因为add对零标志位、进位标志位均有影响,但inc对零标志位有影响,对进位标志位没有影响。但在本题中,si与di增加并没有产生进位,所以替换之后效果相同。

② 在debug中调试,观察数据段中做128位加之前,和,加之后,数据段的值的变化。

在加之前;

加之后:

替换为add之后的效果:

2. 实验任务2

此部分书写内容:

程序task2.asm源码

assume cs:code, ds:data
data segment
        str db 80 dup(?)
data ends

code segment
start:  
        mov ax, data
        mov ds, ax
        mov si, 0
s1:        
        mov ah, 1
        int 21h
        mov [si], al
        cmp al, '#'
        je next
        inc si
        jmp s1
next:
        mov ah, 2
        mov dl, 0ah
        int 21h
        
        mov cx, si
        mov si, 0
s2:     mov ah, 2
        mov dl, [si]
        int 21h
        inc si
        loop s2

        mov ah, 4ch
        int 21h
code ends
end start

运行结果:

运行程序,从键盘上输入一串字符,以#结束(比如,输入George Orwell, 1984#),观察结果。结合运
行结果,理解代码并回答问题:
① 汇编指令代码line11-18,实现的功能是?
② 汇编指令代码line20-22,实现的功能是?
③ 汇编指令代码line24-30,实现的功能是?

①实现的功能是从键盘输入字符后,将其存入偏移地址为s1的内存,然后将其与'#'比较,如果相等跳转到next段,不等的话si自增,输入下一个字符。
②line20-22实现的功能是输出换行符
③line24-30实现的功能是输出从键盘输入的字符。

3. 实验任务3

(1)task3.asm源码

assume cs:code, ds:data
data segment
   x dw 91,792,8536,65521,2021
   len equ $ - x
data ends
   
stack segment
  db 16 dup(0)
stack ends
code segment
start:
   mov ax,data
   mov ds,ax

   mov cx,len
   mov si,offset x
s:
   mov ax,ds:[si]   ;被除数放到ax中
   call printNumber
   call printSpace
   inc si
   inc si
   loop s

   mov ah,4ch
   int 21h
printNumber:
   mov bx,0AH  ;除数放到bx中
   mov dx,0
   mov di,0 ;计数
   push cx
s1:
   div bx     ;除以bx
   push dx   ;dx中的余数入栈
   inc di      ;计数加一
   mov dx,0
   cmp ax,0 ;比较商是否为0,为0时ZF=1
   je next     ;ZF=1时,跳转到next处
   jmp s1     ;ZF=0时,说明商不为0,继续s1的循环
next:
   mov ah,2
   mov cx,di  ;di是余数个数,放到cx中,作为循环次数
s2:
   pop dx  ;将栈中的余数放入dx
   or dl,30H  ;转换后输出
   int 21h
   loop s2

   pop cx
   ret
printSpace:
    mov dl,' '
    mov ah,2  ;输出空格
    int 21h
    ret
code ends
end start

运行测试截图:

4.实验任务4

task4.asm源码:

assume cs:code,ds:data
data segment
    str db "assembly language, it's not difficult but tedious"
    len equ $ - str
data ends

code segment
start:
    mov ax,data
    mov ds,ax
     
    mov cx,len   ;循环次数,即str长度
    mov si,offset str
s:
    call strupr
    inc si
    loop s
    
    mov ah,4ch
    int 21h
strupr:
    mov al,[si]
    cmp al,'a'
    jb  s1    ;小于则跳转
    cmp al,'z'
    ja s1    ;大于则跳转
    and byte ptr [si],11011111b
s1:
    ret
code ends
end start

调试截图:
调用前:

调用后:

5.实验任务5

task5.asm源码:

assume cs:code, ds:data

data segment
    str1 db "yes", '$'
    str2 db "no", '$'
data ends

code segment
start:
    mov ax, data
    mov ds, ax

    mov ah, 1
    int 21h

    mov ah, 2
    mov bh, 0
    mov dh, 24
    mov dl, 70
    int 10h

    cmp al, '7'
    je s1
    mov ah, 9
    mov dx, offset str2
    int 21h

    jmp over

s1: mov ah, 9
    mov dx, offset str1
    int 21h
over:  
    mov ah, 4ch
    int 21h
code ends
end start

程序运行测试截图:

程序的功能是:当输入字符为7时,输出yes,不为7则输出no。

6.实验任务6

调试结果:

通过此项实现任务,你对中断、软中断的理解:

CPU收到中断信息后,需要对中断信息进行处理,中断分为两种,硬中断(由硬件产生)和软中断(由软件产生),我们可以编写中断处理程序来对不同的中断信息进行处理,当CUP接收到中断信息后,可以根据中断向量表来找到相应的中断处理程序的入口地址,而处理完毕后,即可通过iret指令返回。

posted @ 2021-12-14 19:16  MainKeys  阅读(115)  评论(3)    收藏  举报