1. 程式人生 > >彙編程式設計:字元圖形程式陽泉學院

彙編程式設計:字元圖形程式陽泉學院

程式說明:
本程式在文字顯示方式下實現了顯示字元圖形“陽泉學院”。
按回車鍵即可返回作業系統。
函式圖:

;資料段
data segment
       car  db  238         ;陽泉學院字元圖形表
       DB  2ah,238,0,0
       DB  2ah,238,1,0
       DB  2ah,238,1,0
       DB  2ah,238,1,0
       DB  2ah,238,1,0
       DB  2ah,238,1,0
       DB  2ah,238,1,0
       DB  2ah,238,1,0
       DB  2ah,238,1,0
       DB  2ah,238,1,0
       DB  2ah,238,1,0
       DB  2ah,238,1,0
       DB  2ah,238,1,0
       db  2ah,238,1,0
       DB  2ah,238,-13,1
       DB  2ah,238,0,1
       DB  2ah,238,0,1
       DB  2ah,238,0,1
       DB  2ah,238,1,0
       DB  2ah,238,1,0
       DB  2ah,238,1,0
       DB  2ah,238,1,0
       DB  2ah,238,1,0
       DB  2ah,238,1,0
       DB  2ah,238,1,0
       db  2ah,238,1,0
       DB  2ah,238,-4,-3
       DB  2ah,238,0,1
       DB  2ah,238,0,1
       DB  2ah,238,4,-2
       DB  2ah,238,0,1
       DB  2ah,238,0,1
       DB  2ah,238,0,3
       DB  2ah,238,-1,0
       DB  2ah,238,-1,0
       DB  2ah,238,-1,0
       DB  2ah,238,-1,0
       DB  2ah,238,-1,0
       DB  2ah,238,-1,0
       DB  2ah,238,-1,0
       DB  2ah,238,0,1
       DB  2ah,238,0,1
       DB  2ah,238,0,1
       DB  2ah,238,0,1
       DB  2ah,238,1,0
       DB  2ah,238,1,0
       DB  2ah,238,1,0
       DB  2ah,238,1,0
       DB  2ah,238,1,0
       DB  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,-4,-3
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,4,-2
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,-10,7
       db  2ah,238,0,1
       db  2ah,238,1,-3
       db  2ah,238,0,1
       db  2ah,238,1,-3
       db  2ah,238,0,1
       db  2ah,238,1,-1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,1,-8
       db  2ah,238,1,0 
       db  2ah,238,1,0 
       db  2ah,238,1,0 
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,-1,0
       db  2ah,238,-1,0
       db  2ah,238,-1,0
       db  2ah,238,1,-7
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,3,-1
       db  2ah,238,1,-4
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,1,-4
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,0,-2
       db  2ah,238,1,1
       db  2ah,238,-4,0
       db  2ah,238,1,-1
       db  2ah,238,1,-1
       db  2ah,238,1,-1
       db  2ah,238,1,-1
       db  2ah,238,-4,6
       db  2ah,238,1,1
       db  2ah,238,1,1
       db  2ah,238,1,1
       db  2ah,238,1,1
       db  2ah,238,-12,1
       db  2ah,238,-2,1
       db  2ah,238,1,0
       db  2ah,238,0,1
       db  2ah,238,-1,2
       db  2ah,238,0,1
       db  2ah,238,1,0
       db  2ah,238,0,1
       db  2ah,238,0,2
       db  2ah,238,0,1
       db  2ah,238,-1,0
       db  2ah,238,0,1
       db  2ah,238,3,-10
       db  2ah,238,-1,0
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,1,0
       db  2ah,238,1,-8
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,1,-1
       db  2ah,238,1,-1
       db  2ah,238,1,-1
       db  2ah,238,1,-4
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,1,-5
       db  2ah,238,1,1
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,1,-1
       db  2ah,238,-1,-1
       db  2ah,238,-13,8
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,-14,1
       db  2ah,238,0,1
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,-4,-1
       db  2ah,238,4,0
       db  2ah,238,-8,5
       db  2ah,238,0,1
       db  2ah,238,1,0
       db  2ah,238,0,1
       db  2ah,238,2,-4
       db  2ah,238,-1,0
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,1,0
       db  2ah,238,1,-2
       db  2ah,238,0,-1
       db  2ah,238,0,-1
       db  2ah,238,0,-1
       db  2ah,238,2,-1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,0,1
       db  2ah,238,1,-4
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,1,-1
       db  2ah,238,1,-1
       db  2ah,238,1,-1
       db  2ah,238,-7,6
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,1,0
       db  2ah,238,1,1
       db  2ah,238,1,1
       db  2ah,238,1,1
char_cnt dw ?
pointer  dw ?
line_on  db ?
col_on   db ?
data     ends
;堆疊段
stack  segment
     db  2000 dup (0)
stack  ends
;程式碼段
code segment
     assume cs:code,ds:data
main proc far
     push ds
     sub ax,ax			
     push ax				;push ds
							;sub ax,ax
							;push ax 這三條語句是為後面的ret做準備,ret即執行pop ip, pop cs,執行完ip的值為0,cs的值為ds。程式轉移到程式PSP的起始地址,起始地址的前兩個位元組放的是int 20指令,用於返回DOS。
     mov ax,data          ;初始化DS
     mov ds,ax
     mov ah,0             ;設定300*200彩色圖形方式
     mov al,3
     int 10h
     mov ah,0bh           ;設定背景顏色
     mov bh,0
     mov bl,2
     int 10h
     mov ah,0bh           ;設定彩色調板
     mov bh,01
     mov bl,00
     int 10h
     call clear_screen    ;清屏,呼叫clear_screen函式
     lea di,car           ;di指向字元圖形表
     mov dh,5             ;從第5行第5列位置開始
     mov dl,5             ;顯示汽車
     call move_shape		;呼叫move_shape函式
     mov ah,01h           ;從鍵盤輸入字元
     int 21h
     cmp al,0dh				;字元=CR(回車)就跳轉exit
     je exit				
exit:mov ah,4ch				;帶返回碼的結束,返回碼在al中
     int 21h
     ret
main endp
clear_screen proc near    ;清屏子程式
     push ax              ;保護暫存器
     push bx
     push cx
     push dx
     mov ah,6             ;螢幕上卷功能,Scrolls a specified window up 
							;     <dist>	  8-bit number of lines to scroll; positive
							;			  scrolls down; negative scrolls up; 0 clears.
							;	      <attr>	  8-bit attribute for blank lines; if none
							;			  given, 07h (white on black).
							;	      <upcol>	  Upper left column; if none given, CL used.
							;	      <uprow>	  Upper left row; if none given, CH used.
							;	      <dncol>	  Lower right column; if none given, DL used.
							;	      <dnrow>	  Lower right row; if none given, DH used.
     mov al,6
     mov ch,0             ;左上角行號
     mov cl,0             ;左上角列號
     mov dh,250           ;右下角行號
     mov dl,200           ;右下角列號
     mov bh,7             ;捲入行屬性
     int 10h              ;呼叫顯示功能
     pop dx               ;恢復暫存器
     pop cx
     pop bx
     pop ax
     ret                  ;返回主程式
clear_screen endp
move_shape proc near		
     push ax				;保護現場
     push bx
     push cx
     push dx
     push di
     mov ah,0fh           ;取當前顯示方式
     int 10h
     sub ch,ch            ;清除暫存器CX的高位元組
     mov cl,[di]          ;CL放迴圈次數
     inc di               ;di指向字元表的第一個字元
     mov char_cnt,cx      ;儲存迴圈次數
     mov pointer,di
     mov line_on,dh
     mov col_on,dl
plot_next:
     add dh,[di+2]        ;改變行列指標
     add dl,[di+3]
     cmp dl,150           ;出屏否?

     jb mov_crsr			;出屏跳轉mov_crsr函式
     call erase				;呼叫erase函式
     pop di					;恢復現場
     pop dx
     pop cx
     pop bx
     pop ax
     ret
mov_crsr:
     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             ;di指向下一個字元
     loop plot_next
     call dly_qrtr        ;呼叫延時子程式
     call erase           ;呼叫擦除子程式
     jmp short plot_next	;無條件跳轉plot_next
move_shape endp
erase proc near           ;擦除子程式,在下一行開頭繼續顯示該內容
     mov cx,char_cnt		;保護內容
     mov di,pointer	
     mov dh,line_on
     mov dl,col_on
erase_next:
     add dh,[di+2]
     add dl,[di+3]
     mov ah,2
     int 10h
     mov al,[di]
     mov bl,0
     push cx
     mov cx,1
     mov ah,9
     int 10h
     pop cx
     add di,4
     loop erase_next
     mov cx,char_cnt		;在下一行開頭恢復內容
     mov di,pointer
     mov dh,line_on
     inc col_on
     mov dl,col_on
     ret
erase endp
dly_qrtr proc near         ;延時子程式
     push cx
     push dx
     mov dx,50000			;延時50000*8000個cpu週期
 d11:mov cx,8000
 d12:loop d12
     dec dx
     jnz d11
     pop dx
     pop cx
     ret
dly_qrtr endp
code ends
     end main

引數傳遞
本題使用暫存器法進行引數傳遞,暫存器為AX,BX,CX,DX,DI。

中斷向量計算
本題用到了10H、21H函式庫的函式呼叫,入口地址為0:40h ,0:84h。
10H號函式庫:CS=410h+2h=42h , IP=410h=40h 。中斷向量為0:40H。
21H號函式庫:CS=421h+2h=86h , IP=421h=84h 。中斷向量為0:84h。