1. 程式人生 > >彙編程式設計:七段電子錶顯示

彙編程式設計:七段電子錶顯示

程式說明:
這是一個網上電子錶
流程圖:

在這裡插入圖片描述
程式原始碼

data segment
;*****定義please等提示資訊*****
  str db 20
         db 50h,7,0,0
           db 6ch,7,0,1
           db 65h,7,0,1
           db 61h,7,0,1
           db 73h,7,0,1
           db 65h, 7,0,1
           db 70h,7,1,-10
           db 72h,7,0,1
           db 65h,7,0,1  
           db 73h,7,0,1 
           db 73h,7,0,1
           db 53h,7,0,2 
          db 6bh,7,0,2
          db 65h,7,0,1
          db 79h,7,0,1
          db 65h,7,0,2
          db 78h,7,0,1
          db 69h,7,0,1 
         db 74h,7,0,1 
         db 21h,7,0,1
;*****定義0-9十個圖形數字*****
s0 db 6
     db 5fh,7,0,0
     db 7ch,7,1,-1
     db 7ch,7,1,0
     db 5fh,7,0,1
     db 7ch,7,0,1
     db 7ch,7,-1,0
  sy db 2
     db 7ch,7,1,0
     db 7ch,7,1,0
  s2 db 5
     db 5fh,7,0,0
     db 7ch,7,1,1
     db 5fh,7,0,-1
     db 7ch,7,1,-1
     db 5fh,7,0,1
  s3 db 5
     db 5fh,7,0,0
     db 7ch,7,1,1
     db 5fh,7,0,-1
     db 7ch,7,1,1
     db 5fh,7,0,-1
  s4 db 4
     db 7ch,7,1,0
     db 5fh,7,0,1
     db 7ch,7,1,1
     db 7ch,7,-1,0
  s5 db 5
     db 5fh,7,0,0
     db 7ch,7,1,-1
     db 5fh,7,0,1
     db 7ch,7,1,1
     db 5fh,7,0,-1
  s6 db 6
     db 5fh,7,0,0
     db 7ch,7,1,-1
     db 5fh,7,0,1
     db 7ch,7,1,1
     db 5fh,7,0,-1
     db 7ch,7,0,-1
  s7 db 3
     db 5fh,7,0,0
     db 7ch,7,1,1
     db 7ch,7,1,0
  s8 db 7
     db 7ch,7,1,0
     db 5fh,7,-1,1
     db 7ch,7,1,1
     db 5fh,7,0,-1
     db 7ch,7,1,1
     db 5fh,7,0,-1
     db 7ch,7,0,-1
  s9 db 6
     db 7ch,7,1,0
     db 5fh,7,-1,1
     db 7ch,7,1,1
     db 5fh,7,0,-1
     db 7ch,7,1,1
     db 5fh,7,0,-1
 s11 db 2
     db 03h,7,1,0
     db 03h,7,1,0

data ends
stack segment 
         db 200 dup (0)
stack ends
code segment
     assume cs:code,ss:stack,ds:data

start:
        mov ax,data
        mov ds,ax
        mov ah,0         ;設定顯示方式
        mov al,4
        int 10h
        mov ah,0bh        ;置彩色調板
        mov bh,0
        mov bl,1
        int 10h
        mov ah,0bh        ;置彩色調板
        mov bh,1
        mov bl,4
        int 10h

        mov ah,2               ;取系統時間
        int 1ah
        mov ax,0
        mov bx,0
        mov ah,ch            ;取系統時間
        mov al,cl                ;儲存分鐘
        mov bh,dh          ;儲存秒
        mov bl,dl           ;儲存百分之一秒
       
  tt:  
push ax
       push bx
       mov ah,0
       mov al,4
       int 10h
mov ah,0bh
        mov bh,0
        mov bl,1
        int 10h
        mov ah,0bh
        mov bh,1
        mov bl,4
        int 10h
 
       MOV       AH,1              ;讀鍵盤緩衝區字元
       INT       16h
       JZ        ww
       MOV       AH,8                ;從鍵盤輸入字元
       INT       21h
       CMP       AL,'s'             ;輸入s,結束程式
       JE       tu
  ww:  pop bx
       pop ax

  call xian                       ;呼叫顯示子程式
       push ax
       
       
  qu:  mov ah,2                   ;取系統時間
       int 1ah
       cmp bh,dh
       je qu
       pop ax
       inc bh                       ;對時間進行加1
       cmp bh,100
       jl tt
       mov  bh,0
       inc al
       call beep
            cmp al,100
       
            jl tt
       mov al,0
       inc ah
       cmp ah,24
       jl tt
      mov ah,0
    
      jmp tt
   tu: mov ah,4ch
      int 21h
      ret
 xian proc                          ;對時間值進行處理並顯示的子程式
      push si
      push di
      push dx
      push cx
      push bx
      push ax
      pop ax
      push ax
      mov bx,ax
      mov ax,0
      mov al,bh
    
      mov cl,4
      shr al,cl
      mov bl,al     
      mov dh,10
      mov dl,2
      call ff
      pop ax
      push ax
      mov bx,ax
      mov ax,0
      mov al, bh
      and al,0fh
      mov bl,al
      mov dh,10 
      mov dl,6
      call ff
      mov dh,10
      mov dl,10
      mov bl,10 
      call ff
      pop ax
      push ax
      mov cl,4
      shr al,cl
      mov bl,al
      mov dh,10 
      mov dl,14
      call ff
      pop ax
      push ax
      and al ,0fh
    
    
      mov bl,al
      mov dh,10 
      mov dl,18
      call ff
      mov dh,10
      mov dl,22
      mov bl,10
      call ff
      pop ax
      pop bx
      push bx
      push ax

      mov ax,0
      mov al,bh
      mov cl,4
      shr al,cl
     
      mov bl,al
      mov dh,10
      mov dl,26
      call ff
      pop ax
      pop bx
      push bx
      push ax
      mov al,bh
      and al,0fh
  
      mov bl,al
      mov dh,10 
      mov dl,30
      call ff
      mov dh,0
      mov dl,15
      mov bl,22
      call ff
      pop ax
      pop bx
      pop cx
      pop dx
      pop di
      pop si
      ret
xian endp
ff proc ;顯示具體數值
       push ax
       push bx
       push cx
       push dx
  
      cmp bl,0
       je l0
      cmp bl,1
       je l1
      cmp bl,2
       je l2
      cmp bl,3
      je l3
      cmp bl,4
      je l4
      cmp bl,5
      je l5
      cmp bl,6
       je l6
      cmp bl,7
      je l7
      cmp bl,8
      je l8
      cmp bl,9
      je l9
      cmp bl,10
      je l10
      cmp bl,22
      je ld
l0:   lea di,s0
      jmp ty
l1 :  lea di,sy
      jmp ty
l2:   lea di,s2
      jmp ty
l3:   lea di,s3
      jmp ty
l4:   lea di,s4
      jmp ty
l5:   lea di,s5
      jmp ty
l6:   lea di,s6
      jmp ty
l7:   lea di,s7
      jmp ty
l8:   lea di,s8
      jmp ty
l9:   lea di,s9
      jmp ty
l10:  lea di,s11
      jmp ty
ld :  lea di,str
ty:   mov ah,0fh  ;示方式
      int 10h
      sub cx,cx
      mov cl,[di]
      inc di
plot_next:   add dh,[di+2]
             add dl,[di+3]
             mov ah,2
             int 10h
             mov al,[di]
             mov bl,[di+1]
             push cx
             mov cx,1
             mov ah,09
             int 10h
             pop cx
             add di,4
        loop plot_next

    pop dx
    pop cx
    pop bx
    pop ax
    mov cx,100
gg: dec cx
loop gg

ret
ff endp
clear  proc 
       push ax
       push bx
       push cx
       push dx
       mov ah,6
       mov al,0
       mov bh,1
       int 10h
       pop dx
       pop cx
       pop bx
       pop ax
ret
clear endp
beep proc
 push ax
 push bx
 push cx
 push dx
  mov al ,10110110b
  out 43h,al
  mov ax,533h
  out 42h,al
  mov al,ah
  out 42h,al
  in al,61h
  mov ah,al
  or al,03
  out 61h,al
  sub cx,cx
  mov bl,28h
g7: loop g7
    dec bl
    jnz g7
    mov al,ah
    out 61h,al
   pop dx
   pop cx
   pop bx
   pop ax


    ret
beep endp

code ends
     end start

拆分之後的程式碼:
beep.asm

public beep
code segment
     assume cs:code
beep proc far
 push ax
 push bx
 push cx
 push dx
  mov al ,10110110b
  out 43h,al
  mov ax,533h
  out 42h,al
  mov al,ah
  out 42h,al
  in al,61h
  mov ah,al
  or al,03
  out 61h,al
  sub cx,cx
  mov bl,28h
g7: loop g7
    dec bl
    jnz g7
    mov al,ah
    out 61h,al
   pop dx
   pop cx
   pop bx
   pop ax
    ret
beep endp
code ends
     end

clear.asm

public clear
code segment
     assume cs:code
clear  proc far
       push ax
       push bx
       push cx
       push dx
       mov ah,6
       mov al,0
       mov bh,1
       int 10h
       pop dx
       pop cx
       pop bx
       pop ax
ret
clear endp
code ends
     end

ff.asm

data segment
;*****定義please等提示資訊*****
  str db 20
         db 50h,7,0,0
           db 6ch,7,0,1
           db 65h,7,0,1
           db 61h,7,0,1
           db 73h,7,0,1
           db 65h, 7,0,1
           db 70h,7,1,-10
           db 72h,7,0,1
           db 65h,7,0,1  
           db 73h,7,0,1 
           db 73h,7,0,1
           db 53h,7,0,2 
          db 6bh,7,0,2
          db 65h,7,0,1
          db 79h,7,0,1
          db 65h,7,0,2
          db 78h,7,0,1
          db 69h,7,0,1 
         db 74h,7,0,1 
         db 21h,7,0,1
;*****定義0-9十個圖形數字*****
s0 db 6
     db 5fh,7,0,0
     db 7ch,7,1,-1
     db 7ch,7,1,0
     db 5fh,7,0,1
     db 7ch,7,0,1
     db 7ch,7,-1,0
  sy db 2
     db 7ch,7,1,0
     db 7ch,7,1,0
  s2 db 5
     db 5fh,7,0,0
     db 7ch,7,1,1
     db 5fh,7,0,-1
     db 7ch,7,1,-1
     db 5fh,7,0,1
  s3 db 5
     db 5fh,7,0,0
     db 7ch,7,1,1
     db 5fh,7,0,-1
     db 7ch,7,1,1
     db 5fh,7,0,-1
  s4 db 4
     db 7ch,7,1,0
     db 5fh,7,0,1
     db 7ch,7,1,1
     db 7ch,7,-1,0
  s5 db 5
     db 5fh,7,0,0
     db 7ch,7,1,-1
     db 5fh,7,0,1
     db 7ch,7,1,1
     db 5fh,7,0,-1
  s6 db 6
     db 5fh,7,0,0
     db 7ch,7,1,-1
     db 5fh,7,0,1
     db 7ch,7,1,1
     db 5fh,7,0,-1
     db 7ch,7,0,-1
  s7 db 3
     db 5fh,7,0,0
     db 7ch,7,1,1
     db 7ch,7,1,0
  s8 db 7
     db 7ch,7,1,0
     db 5fh,7,-1,1
     db 7ch,7,1,1
     db 5fh,7,0,-1
     db 7ch,7,1,1
     db 5fh,7,0,-1
     db 7ch,7,0,-1
  s9 db 6
     db 7ch,7,1,0
     db 5fh,7,-1,1
     db 7ch,7,1,1
     db 5fh,7,0,-1
     db 7ch,7,1,1
     db 5fh,7,0,-1
 s11 db 2
     db 03h,7,1,0
     db 03h,7,1,0

data ends
stack segment 
         db 200 dup (0)
stack ends
public ff
code segment
     assume cs:code
ff proc far;顯示具體數值
       push ax
       push bx
       push cx
       push dx
  
      cmp bl,0
       je l0
      cmp bl,1
       je l1
      cmp bl,2
       je l2
      cmp bl,3
      je l3
      cmp bl,4
      je l4
      cmp bl,5
      je l5
      cmp bl,6
       je l6
      cmp bl,7
      je l7
      cmp bl,8
      je l8
      cmp bl,9
      je l9
      cmp bl,10
      je l10
      cmp bl,22
      je ld
l0:   lea di,s0
      jmp ty
l1 :  lea di,sy
      jmp ty
l2:   lea di,s2
      jmp ty
l3:   lea di,s3
      jmp ty
l4:   lea di,s4
      jmp ty
l5:   lea di,s5
      jmp ty
l6:   lea di,s6
      jmp ty
l7:   lea di,s7
      jmp ty
l8:   lea di,s8
      jmp ty
l9:   lea di,s9
      jmp ty
l10:  lea di,s11
      jmp ty
ld :  lea di,str
ty:   mov ah,0fh  ;示方式
      int 10h
      sub cx,cx
      mov cl,[di]
      inc di
plot_next:   add dh,[di+2]
             add dl,[di+3]
             mov ah,2
             int 10h
             mov al,[di]
             mov bl,[di+1]
             push cx
             mov cx,1
             mov ah,09
             int 10h
             pop cx
             add di,4
        loop plot_next

    pop dx
    pop cx
    pop bx
    pop ax
    mov cx,100
gg: dec cx
loop gg

ret
ff endp
code ends
     end

main.asm

extrn clear:far,ff:far,beep:far,xian:far
data segment
;*****定義please等提示資訊*****
  str db 20
         db 50h,7,0,0
           db 6ch,7,0,1
           db 65h,7,0,1
           db 61h,7,0,1
           db 73h,7,0,1
           db 65h, 7,0,1
           db 70h,7,1,-10
           db 72h,7,0,1
           db 65h,7,0,1  
           db 73h,7,0,1 
           db 73h,7,0,1
           db 53h,7,0,2 
          db 6bh,7,0,2
          db 65h,7,0,1
          db 79h,7,0,1
          db 65h,7,0,2
          db 78h,7,0,1
          db 69h,7,0,1 
         db 74h,7,0,1 
         db 21h,7,0,1
;*****定義0-9十個圖形數字*****
s0 db 6
     db 5fh,7,0,0
     db 7ch,7,1,-1
     db 7ch,7,1,0
     db 5fh,7,0,1
     db 7ch,7,0,1
     db 7ch,7,-1,0
  sy db 2
     db 7ch,7,1,0
     db 7ch,7,1,0
  s2 db 5
     db 5fh,7,0,0
     db 7ch,7,1,1
     db 5fh,7,0,-1
     db 7ch,7,1,-1
     db 5fh,7,0,1
  s3 db 5
     db 5fh,7,0,0
     db 7ch,7,1,1
     db 5fh,7,0,-1
     db 7ch,7,1,1
     db 5fh,7,0,-1
  s4 db 4
     db 7ch,7,1,0
     db 5fh,7,0,1
     db 7ch,7,1,1
     db 7ch,7,-1,0
  s5 db 5
     db 5fh,7,0,0
     db 7ch,7,1,-1
     db 5fh,7,0,1
     db 7ch,7,1,1
     db 5fh,7,0,-1
  s6 db 6
     db 5fh,7,0,0
     db 7ch,7,1,-1
     db 5fh,7,0,1
     db 7ch,7,1,1
     db 5fh,7,0,-1
     db 7ch,7,0,-1
  s7 db 3
     db 5fh,7,0,0
     db 7ch,7,1,1
     db 7ch,7,1,0
  s8 db 7
     db 7ch,7,1,0
     db 5fh,7,-1,1
     db 7ch,7,1,1
     db 5fh,7,0,-1
     db 7ch,7,1,1
     db 5fh,7,0,-1
     db 7ch,7,0,-1
  s9 db 6
     db 7ch,7,1,0
     db 5fh,7,-1,1
     db 7ch,7,1,1
     db 5fh,7,0,-1
     db 7ch,7,1,1
     db 5fh,7,0,-1
 s11 db 2
     db 03h,7,1,0
     db 03h,7,1,0

data ends
stack segment 
         db 200 dup (0)
stack ends
code segment
     assume cs:code,ss:stack,ds:data

start:
        mov ax,data
        mov ds,ax
        mov ah,0         ;設定顯示方式
        mov al,4
        int 10h
        mov ah,0bh        ;置彩色調板
        mov bh,0
        mov bl,1
        int 10h
        mov ah,0bh        ;置彩色調板
        mov bh,1
        mov bl,4
        int 10h

        mov ah,2               ;取系統時間
        int 1ah
        mov ax,0
        mov bx,0
        mov ah,ch            ;取系統時間
        mov al,cl                ;儲存分鐘
        mov bh,dh          ;儲存秒
        mov bl,dl           ;儲存百分之一秒
       
  tt:  
push ax
       push bx
       mov ah,0
       mov al,4
       int 10h
mov ah,0bh
        mov bh,0
        mov bl,1
        int 10h
        mov ah,0bh
        mov bh,1
        mov bl,4
        int 10h
 
       MOV       AH,1              ;讀鍵盤緩衝區字元
       INT       16h
       JZ        ww
       MOV       AH,8                ;從鍵盤輸入字元
       INT       21h
       CMP       AL,'s'             ;輸入s,結束程式
       JE       tu
  ww:  pop bx
       pop ax

  call xian                       ;呼叫顯示子程式
       push ax
       
       
  qu:  mov ah,2                   ;取系統時間
       int 1ah
       cmp bh,dh
       je qu
       pop ax
       inc bh                       ;對時間進行加1
       cmp bh,100
       jl tt
       mov  bh,0
       inc al
       call beep
            cmp al,100
       
            jl tt
       mov al,0
       inc ah
       cmp ah,24
       jl tt
      mov ah,0
    
      jmp tt
   tu: mov ah,4ch
      int 21h
      ret
code ends
     end start

xian.asm

extrn ff:far
public xian
code segment
     assume cs:code
 xian proc  far                        ;對時間值進行處理並顯示的子程式
      push si
      push di
      push dx
      push cx
      push bx
      push ax
      pop ax
      push ax
      mov bx,ax
      mov ax,0
      mov al,bh
    
      mov cl,4
      shr al,cl
      mov bl,al     
      mov dh,10
      mov dl,2
      call ff
      pop ax
      push ax
      mov bx,ax
      mov ax,0
      mov al, bh
      and al,0fh
      mov bl,al
      mov dh,10 
      mov dl,6
      call ff
      mov dh,10
      mov dl,10
      mov bl,10 
      call ff
      pop ax
      push ax
      mov cl,4
      shr al,cl
      mov bl,al
      mov dh,10 
      mov dl,14
      call ff
      pop ax
      push ax
      and al ,0fh
    
    
      mov bl,al
      mov dh,10 
      mov dl,18
      call ff
      mov dh,10
      mov dl,22
      mov bl,10
      call ff
      pop ax
      pop bx
      push bx
      push ax

      mov ax,0
      mov al,bh
      mov cl,4
      shr al,cl
     
      mov bl,al
      mov dh,10
      mov dl,26
      call ff
      pop ax
      pop bx
      push bx
      push ax
      mov al,bh
      and al,0fh
  
      mov bl,al
      mov dh,10 
      mov dl,30
      call ff
      mov dh,0
      mov dl,15
      mov bl,22
      call ff
      pop ax
      pop bx
      pop cx
      pop dx
      pop di
      pop si
      ret
xian endp
code ends
     end

函式功能
主函式功能為顯示當前系統時間,並在整分鐘時報時;當鍵盤輸入’s’時,退出程式。
1)首先通過BIOS中斷設定顯示格式(320*200彩色),並獲取當前系統時間,儲存在CH(小時),CL(分鐘),DH(秒),DL(百分秒)中。隨後,令AH=CH,AL=CL,BH=DH,BL=DL,將時間數值存放在AX,BX兩暫存器的對應位置中。
2)儲存當前暫存器數值,設定顯示格式,並檢測鍵盤緩衝區。若有內容,且為’s’,則退出程式;否則,繼續進行。
3)再次讀取當前時間,若秒(BH)處發生變化(即cmp bh,dh,此時bh儲存的為步驟1中獲得的秒時間,而dh為當前新獲得的時間),則修改秒暫存器內容。
4)若達整分鐘,將BH置0,分鐘(AL)加1,並呼叫BEEP發出蜂鳴。
5)若達整小時,同理,修改對應暫存器。
6)若達24小時(即新的一天),AH置0,回到步驟2處繼續進行。
此程式在時間到達60分鐘(整小時)時會出現錯誤,表現為分鐘位顯示錯亂。

子程式xian:由主程式呼叫,用於處理並顯示當前時間。此程式為順序結構,分別顯示小時位數字1、2,分隔符1,分鐘位數字1、2,分隔符2,秒位數字1、2及其餘空白。

子程式ff:由子程式XIAN呼叫,用於顯示對應的時間數字。程式結構類似C語言的switch-case-break結構。通過判斷bl中的數字,顯示對應的數字。

子程式BEEP:由主程式呼叫,用於發出蜂鳴。

子程式CLEAR:僅在原始碼中出現,未被呼叫。

引數傳遞
主程式與子程式XIAN之間的引數傳遞為暫存器法,使用暫存器為AX(AH,AL),BX(BH,BL)。
子程式XIAN與子程式FF之間的引數傳遞為暫存器法,使用暫存器為BL;子程式使用的輸出字串存在
子程式BEEP不需要引數。

中斷向量:
本題用到了10H、16H、1AH、21H函式庫的函式呼叫。
10H號函式庫:CS=410h+2h=42h , IP=410h=40h 。中斷向量為0:40H。
16H號函式庫:CS = 416H + 2H =60H, IP =416H=58H。中斷向量為0:58H。
1AH號函式庫:CS = 41AH + 2H =70H, IP =41AH=68H。中斷向量為0:68H。
21H號函式庫:CS=421h+2h=86h , IP=421h=84h 。中斷向量為0:84h。