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

1、实验任务1

源码:

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

 

回答问题:

1)line31~line34的4条inc指令,能否替换成如下代码?你的结论的依据/理由是什么?

不能替换,因为add会影响标志寄存器的值,从而使用adc时,结果可能会受影响

而inc不影响标志寄存器的值

 

2)在debug中调试,观察数据段中做128位加之前和加之后,数据段的值的变化。给出调试观察截图。

相加之前,数据段的值:

 

相加之后,数据段的值:

 

 

2、实验任务2

源码:

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,实现的功能是?

将输入的字符,从偏移地址0开始,存入到数据段中;当输入#是,通过je比较,跳转到next中

② 汇编指令代码line20-22,实现的功能是?

换行;并设置si为0

③ 汇编指令代码line24-30,实现的功能是?

在屏幕上输出刚刚输入的除#以外的字符串

 

3、实验任务3

 源码:

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,5
       mov si,offset x
s:
       mov ax,ds:[si]   
       call printNumber
       call printSpace
       ;字数据,inc si两次
      inc si
       inc si
      loop s

       mov ah,4ch
       int 21h

printNumber:
    ;每次除10,取余数,并压栈
       mov bx,10
       mov dx,0
       mov di,0 ;记录取余数次数
       push cx

s1:
      div bx     ;除以10
      push dx   ;dx中的余数入栈
      inc di      ;取余数的次数加一
       mov dx,0
    ;商为0,代表该字数据已逐位入栈,就跳转next
       cmp ax,0 
       je next    
      jmp s1     

next:
       mov ah,2
      mov cx,di  

s2:
    ;将栈中数据逐个弹出,并输出
       pop 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

 源码:

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

stack segment
        db 8 dup(?)
stack ends

code segment
start:
        mov ax, data
        mov ds, ax
        mov si, 0
        mov cx, len     
        call strupr     
   
        mov ax, 4c00h
        int 21h

strupr:
        mov al, ds:[si] ; 取出字符
          ;小写字母的ascii码在97~122
        cmp al, 97  
        jl next   
           cmp al, 122    
        jg next     
           and al, 0dfh    ; 小写转大写
           mov ds:[si], al 

next:
           inc si         ; 后移指向下一个字符
        loop strupr 
    ret  

code ends
end start

执行前,观察数据段中的字符:

 

 执行到程序退出前,并再次观察该数据段:

 

 数据段中字符全都转换成大写字母。

 

5、实验任务5

源码:

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;否则输出no

 

6、实验任务6

源码:

task6_1.asm

assume cs:code

code segment
start:
    ; 42 interrupt routine install code
    mov ax, cs
    mov ds, ax
    mov si, offset int42  ; set ds:si

    mov ax, 0
    mov es, ax
    mov di, 200h        ; set es:di

    mov cx, offset int42_end - offset int42
    cld
    rep movsb

    ; set IVT(Interrupt Vector Table)
    mov ax, 0
    mov es, ax
    mov word ptr es:[42*4], 200h
    mov word ptr es:[42*4+2], 0

    mov ah, 4ch
    int 21h

int42: 
    jmp short int42_start
    str db "welcome to 2049!"
    len equ $ - str

    ; display string "welcome to 2049!"
int42_start:
    mov ax, cs
    mov ds, ax
    mov si, 202h

    mov ax, 0b800h
    mov es, ax
    mov di, 24*160 + 32*2

    mov cx, len
s:  mov al, [si]
    mov es:[di], al
    mov byte ptr es:[di+1], 2
    inc si
    add di, 2
    loop s

    iret
int42_end:
   nop
code ends
end start

task6_2.asm

assume cs:code

code segment
start:
    int 42

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

 运行截图:

 

问题:

CPU收到中断信息后,通过我们编写的中断处理程序来处理中断信息;

如int 42,int是一种中断,而42是中断向量,代表着中断处理程序的程序入口;

通过调用int 42进入中断处理程序,通过屏幕输出的绿字“welcome to 2049”可以判断程序的执行成功。

 

posted @ 2021-12-11 16:29  厘厝  阅读(58)  评论(2)    收藏  举报