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

四、实验结论

1. 实验任务 1

  • 验证性实验:有些汇编指令会影响到标志寄存器中的一个或多个状态标志位。
    在 debug 环境中,分别实践、观察:

    溢出 OV (overflow,OF = 1)
    无溢出 NV (no overflow,OF = 0)

    减量 DN (direction down,DF = 1)
    增量 UP (direction up,DF = 0)

    允许中断 EI (enable interrupt,IF = 1)
    进制中断 DI (disable interrupt,IF = 0)

    负 NG (negative,SF = 1)
    正 PL (plus,SF = 0)

    零 ZR (zero,ZF = 1)
    非零 NZ (no zero,ZF = 0)

    辅助进位 AC (auxiliary carry,AF = 1)
    无辅助进位 NA (no auxiliary carry,AF = 0)

    偶校验 PE (even parity,PF = 1)
    奇校验 PO (odd parity,PF = 0)

    进位 CY (carry,CF = 1)
    无进位 NC (no carry,CF = 0

    • add 指令对标志寄存器中的零标志位 ZF(Zero Flag)、进位标志位 CF(Carry Flag) 是否有影响?
      image

    • inc 指令对标志寄存器中的零标志位 ZF(Zero Flag)、进位标志位 CF(Carry Flag) 是否有影响?
      image

    结论:add 和 inc 指令均对 ZF 位有影响,但 inc 不改变进位标志位 CF,而 add 改变进位标志位 CF,此外两指令对 8bit 加法运算第三位向第四位做加法时产生半进位标志 AC 均有影响。

  • 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
    
  • 回答问题

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

      add si, 2
      add di, 2
      

      不能,此处为 128bit 加法,inc 不改变进位标志位 CF,而 add 改变进位标志位 CF,若使用 add 则可能丢失进位信息。

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

image

image

image

image

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
    
  • 运行测试截图
    image

  • 回答问题
    运行程序,从键盘上输入一串字符,以#结束(比如,输入 George Orwell, 1984#),观察结果。结合运行结果,理解代码并回答问题:

    1. 汇编指令代码 line11-18,实现的功能是?
      判断输入字符是否等于 #,若不等于存入 data 段, 使用 si 计数,并继续读入;若等于则跳转至 next。
    2. 汇编指令代码 line20-22,实现的功能是?
      ASCII 码中 0ah 为换行符,打印换行符使得输出字符串在下一行开始显示。
    3. 汇编指令代码 line24-30,实现的功能是?
      将读入字符串长度写入 cx 使用 loop 循环打印字符串。

3. 实验任务 3

编写8086汇编源程序task3.asm,在屏幕上以十进制形式输出data段中这一组连续的数据,数据和数据之间以空格间隔。
要求:

  • 编写子程序printNumber
    功能:以十进制形式输出一个任意位数的整数(整数范围0 ~ 65535)
    入口参数:寄存器ax(待输出的数据 --> ax)
    出口参数:无
  • 编写子程序printSpace
    功能:打印一个空格
    入口参数:无
    出口参数:无

在主体代码中,综合应用寻址方式和循环,调用printNumber和printSpace, 实现题目要求。

  • task3.asm源码
assume cs:code, ds:data

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

code segment

; print number in ax (0 <= [ax] <= 65535) to console
; param     :  ax
; return    : none
printNumber:
    push bx
    push cx
    push dx

    mov cx, 0
s1: 
    mov dx, 0
    mov bx, 10
    div bx
    push dx
    inc cx
    cmp ax, 0
    jne s1

s2:
    pop dx
    add dx, 30h
    mov ah, 2
    int 21h
    loop s2

    pop dx
    pop cx
    pop bx
    ret

; print a space to console
; param     : none
; return    : none
printSpace:
    push ax
    push dx

    mov dl, 20h
    mov ah, 02h
    int 21h
    
    pop dx
    pop ax
    ret

main: 
    mov ax, data
    mov ds, ax

    mov cx, len
    mov di, offset x
s:  mov ax, [di]
    call printNumber
    call printSpace
    add di, 2
    sub cx, 1
    loop s

    mov ax, 4c00h
    int 21h

code ends

end main
  • 运行测试截图

image

4. 实验任务 4

针对8086CPU,已知逻辑段定义如下:

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

编写8086汇编源程序task4.asm,将data段中字符串里的小写字符转换成大写。
要求:

  • 编写子程序strupr
    功能:将包含任意字符的字符串中的小写字母变成大写
    入口参数:
    • (ds:si ) 字符串首地址的段地址和偏移地址分别送至ds和si
    • (cx) 字符串的长度
      出口参数:无

在主体代码中,设置入口参数,调用strupr, 实现题目要求。

  • task4.asm源码
    assume cs:code, ds:data
    
    data segment
        string db "assembly language, it's not difficult but tedious"
        len equ $ - string
    data ends
    
    code segment
    
    ; change lowercase letter to uppercase letter in string
    ; param: 
    ;   (ds:[si]): string ptr 
    ;   cx: string length
    ; ret: none
    strupr:
    s1:
        and byte ptr ds:[si], 11011111b 
        inc si
        loop s1
        ret
    
    
    main: 
        mov ax, data
        mov ds, ax
    
        mov si, offset string
        mov cx, len
        call strupr
    
        mov ax, 4c00h
        int 21h
    
    code ends
    
    end main
    
  • 在debug中调试截图( call strupr 调用之前,数据段的值,以及,调用之后,数据段的值)

image

image

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,以及输入其他字符,运行结果截图)

image

image

  • 程序的功能是?
    输入一个字符并判断输入的字符是否为 7,若是则调用 21h 中断的第 9 号程序,输出 yes 否则输出 no;

6. 实验任务 6

  • 通过此项实现任务,你对中断、软中断实现机制的理解
    • task6-1.asm 将中断处理程序写入内存,起始地址 0:200
    • 将中断处理程序起始地址写入中断向量表
    • 当程序产生 42(2ah) 中断时访问中断向量表中存储的地址,调用中断处理程序
    • iret 指令将 IP CS PSW 依次弹出,恢复程序的执行

image

  • 自己选一个未被使用的中断码,实现一个中断例程,并调用测试。给出源码和运行测试截图。(选做*)
    assume cs:code
    
    code segment
    
    ; print date to console
    int43:
    
        jmp short int43_start
    
    ; print number in ax (0 <= [ax] <= 65535) to console
    ; param     :  ax
    ; return    : none
    printNumber:
        push bx
        push cx
        push dx
    
        mov cx, 0
    s1: 
        mov dx, 0
        mov bx, 10
        div bx
        push dx
        inc cx
        cmp ax, 0
        jne s1
    
    s2:
        pop dx
        add dx, 30h
        mov ah, 2
        int 21h
        loop s2
    
        pop dx
        pop cx
        pop bx
        ret
    
    ; print a space to console
    ; param     : none
    ; return    : none
    printSpace:
        push ax
        push dx
    
        mov dl, 20h
        mov ah, 02h
        int 21h
    
        pop dx
        pop ax
        ret
    
    int43_start:
        push ax
    
        mov ah, 2ah
        int 21h
        mov ax, cx
        call printNumber
        call printSpace
        mov al, dh
        mov ah, 0
        call printNumber
        call printSpace
        mov al, dl
        mov ah, 0
        call printNumber
        call printSpace
    
        pop ax
        iret
    
    int43_end:
        nop
    
    main:
        ; 43 interrupt routine install code
        mov ax, cs
        mov ds, ax
        mov si, offset int43  ; set ds:si
    
        mov ax, 0
        mov es, ax
        mov di, 200h        ; set es:di
    
        ; set IVT(Interrupt Vector Table)
        mov ax, 0
        mov es, ax
        mov word ptr es:[43*4], 200h
        mov word ptr es:[43*4+2], 0
    
        mov cx, offset int43_end - offset int43
        cld
        rep movsb
    
        mov ax, 4c00h
        int 21h
    
    code ends
    
    end main
    
  • 如选做,请说明你使用的中断码,并描述你实现的这个中断例程的功能。
    • 中断码:43(2bh)
    • 功能:将日期打印至控制台

image

posted @ 2021-12-13 20:59  ノノカ  阅读(99)  评论(3编辑  收藏  举报