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

实验任务一

  • 验证add指令对标志寄存器中的零标志位ZF(Zero Flag)、进位标志位CF(Carry Flag)的影响:
    image
    add指令对ZF和CF都产生了影响。ff+1以后变成了00,进位为1,所以零标志位ZF从NZ变成了ZR,进位标志位CF从NC变成了CY。
  • 验证inc指令对标志寄存器中的零标志位ZF(Zero Flag)、进位标志位CF(Carry Flag)的影响:
    image
    inc指令对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
  • 回答问题:
    ① line31~line34的4条inc指令,能否替换成如下代码?你的结论的依据/理由是什么?
    inc指令不会影响CF标志位, add指令会影响CF标志位。
    ② 在debug中调试,观察数据段中做128位加之前,和,加之后,数据段的值的变化。
    做128加之前:
    image
    做128加之后:
    image

实验任务二

  • 源代码:
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

image

  • 回答问题:
    ① 汇编指令代码line11-18,实现的功能是?
    读取用户输入的字符, 并将之保存在ds:[si]中.如果是#则跳转到next处执行,否则继续读入下一个字符。
    ② 汇编指令代码line20-22,实现的功能是?
    换行。
    ③ 汇编指令代码line24-30,实现的功能是?
    打印输出除了"#"以外用户输入的所有字符。

实验任务三

  • 源代码:
assume cs:code,ds:data
data segment
    x dw 91, 792, 8536, 65521, 2021
    len equ $ - x
data ends
stack segment
    db 64 dup(0)
    top equ $+1
stack ends
code segment
start:
    mov ax,data
    mov ds,ax
    mov ax,stack
    mov ss,ax
    mov sp,top

    mov cx,len/2
    mov bx,0
s1:  
    mov ax,ds:[bx]
    push bx
    push cx
    call printNumber
    call printSpace
    pop cx
    pop bx
    add bx,2
    loop s1

    mov ah,4ch
    int 21h

printNumber:
    ;ax dx
    ;ax 被除数,直到
    ;cx计有多少位,然后循环出栈输出
    mov bx,10
    mov cx,0
s2:
    mov dx,0
    div bx
    ;ax 商
    ;dx 余数
    push dx
    inc cx
    cmp ax,0
    jne s2

s3:
    mov ah,2
    pop dx
    or dl,30h
    int 21h
    loop s3
    ret

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

code ends
end start

image
实验任务四:

  • 源代码:
assume ds:data, cs:code
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 si, offset str

    mov cx, len

s1:
    call strupr
    inc si
    loop s1

    mov ah, 4ch
    int 21h

strupr:
    cmp byte ptr ds:[si], 96
    jna s2          ;如果当前的字符小于等于96,则直接输出
    sub byte ptr ds:[si], 32 ;否则转为大写字母
s2:
    mov dx, ds:[si]
    mov ah, 2
    int 21h
    ret

code ends
end start

image

实验任务五

  • 源代码:
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

image
image

  • 功能:
    接收用户输入的一个字符:
    如果输入的是7, 则在(第27行, 第70列)输出yes。
    如果输入的不是7, 则在(第27行, 第70列)输出no。

实验任务六

  • 源代码:
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
assume cs:code

code segment
start:
    int 42

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

image

  • 中断和软中断的区别:
    中断,其实就是中断源发出中断信号,CPU暂定当前的程序,转而去执行其他程序,即改变CS:IP
    中断源有很多,CPU内部的中断源发出中断信号,外部设备发出的中断信号。
    硬件中断指的就是外部设备发出的电信号中断信号,需要中断控制器,排队器等硬件电路实现。
    软中(内)断就是CPU内部产生的中断信号,比如除法溢出,或者程序调用中断例程,感觉和call类似。
posted @ 2021-12-13 21:31  onism127  阅读(216)  评论(2)    收藏  举报