1. 程式人生 > 其它 >實驗 4 8086標誌暫存器及中斷

實驗 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) 是否有影響?

    • inc 指令對標誌暫存器中的零標誌位 ZF(Zero Flag)、進位標誌位 CF(Carry Flag) 是否有影響?

    結論: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 位加之前和加之後,資料段的值的變化。給出除錯觀察截圖。

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#),觀察結果。結合執行結果,理解程式碼並回答問題:

    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
  • 執行測試截圖

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 呼叫之前,資料段的值,以及,呼叫之後,資料段的值)

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,以及輸入其他字元,執行結果截圖)
  • 程式的功能是?
    輸入一個字元並判斷輸入的字元是否為 7,若是則呼叫 21h 中斷的第 9 號程式,輸出 yes 否則輸出 no;

6. 實驗任務 6

  • 通過此項實現任務,你對中斷、軟中斷實現機制的理解
    • task6-1.asm 將中斷處理程式寫入記憶體,起始地址 0:200
    • 將中斷處理程式起始地址寫入中斷向量表
    • 當程式產生 42(2ah) 中斷時訪問中斷向量表中儲存的地址,呼叫中斷處理程式
    • iret 指令將 IP CS PSW 依次彈出,恢復程式的執行
  • 自己選一個未被使用的中斷碼,實現一箇中斷例程,並呼叫測試。給出原始碼和執行測試截圖。(選做*)
    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)
    • 功能:將日期列印至控制檯