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

实验任务1

对ZF和CF标志位均有影响

对ZF标志位有影响,但是对CF标志位没有影响

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

  

①不能替换为add si,2与add di,2 因为会影响到多个标志位,而inc是不会影响到的

②调试截图如下:

 

 

 

 

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

答:实现的功能是从键盘获取输入,每次输入一个字符,同时判断输入的字符是否为结束符号#,若是#则跳出循环,继续往下执行段next和段s2,否则si加1用于记录字符串长度,然后跳转到段s1开头。

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

答: 输出回车符。

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

答: 循环si次,将刚刚输入的字符串打印出来。

 

实验任务3

assume ds:data, cs:code, ss:stack

data segment
    x dw 91, 792, 8536, 65535, 2021
    len equ $ - x
data ends

stack segment
    dw 8 dup(0)
stack ends

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

    mov ax, stack
    mov ss, ax
    mov sp, 16

    mov cx, len/2 ; 
    print:
        mov ax, word ptr ds:[di]    ; 把数据放入al
        add di, 2                   ; di指针后移2字节

        push cx	;保存cx值

        call printNum    ; 打印数字
        call printSpace     ; 打印空格

        pop cx              
    loop print
    
    mov ah, 4ch
    int 21h

printNum:
    mov bx, 0       ; 获取位数
    All:
        mov bp, 10      ; 除10运算
        mov dx, 0            
        div bp          
        push dx         ; 将余数入栈
        inc bx          ; 位数+1
        mov cx, ax     ;将商赋值cx
        inc cx          

    loop All

    mov cx, bx          ; 将位数赋值,用于循环
    printAll:
        pop dx          ; 取出一位数
        add dl, 30h
        mov ah, 2     
        int 21h
    loop printAll 

    ret

printSpace:
    mov ah, 2
    mov dl, 20h
    int 21h
    ret

code ends
end start

  

 

 实验任务4

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
    mov si, 0
 
s:
    call strUpr
    inc si
    loop s
 
    mov cx, len
    mov si, 0
    mov ah, 2
 
printStr:
    mov dl, ds:[si]
    int 21h
    inc si
    loop printStr
 
    mov ah, 4ch
    int 21h
 
strUpr:
    mov al, byte ptr ds:[si]
    cmp al, 'a'
    jb back
    cmp al, 'z'
    ja back
    sub al, 20h
    mov ds:[si], al
     
back:
    ret
 
code ends
end start

  

 

 

实验任务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进行比较

如果相同

则输出数据段的str1标号处数据yes

如果不相同

则输出数据段的str2标号处数据no

 

 

实验任务6

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 ;将标志寄存器Flag的方向标志位DF清零。
    rep movsb ;把代码从ds:si复制到es:di中,cx中存放复制的代码长度
 
    ; 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 ;CPU回到执行中断处理程序前的执行点继续执行程序
int42_end:
   nop
code ends
end start

  

assume cs:code
 
code segment
start:
    int 42 ; 调用自己实现的42号软中断
     
    mov ah, 4ch
    int 21h
code ends
end start

  

 

 

运行程序过后,通过实现中断,在屏幕底部的中间成功显示绿色的“welcome to 2049!”。

在程序ex4_6_1.asm中,先将si赋值为标号int42的地址,将di赋值为200h,然后利用rep movsb指令,将int42到int42_end的指令复制到0:200h开始的连续内存空间,接着设置中断向量表,使得ex4_6_2.asm中通过int42可以跳转到中断处理程序。标号int42到int42_end的指令则实现了在屏幕第0页的最后一行中间打印绿色的“welcome to 2049!”。

posted @ 2021-12-12 10:47  居林超  阅读(46)  评论(0编辑  收藏  举报