彙編程式設計:數字方陣程式
阿新 • • 發佈:2019-01-02
程式說明:
該程式實現顯示一個數字的方陣,程式中的清屏功能可將上次執行的結果
清除,重新顯示下次執行結果。本程式在dos中或windows98中執行。
流程圖:
原始碼:
data segment buf1 db '1 2 38 9 47 6 5' buf2 db '1 2 3 412 13 14 511 16 15 610 9 8 7' buf3 db '1 2 3 4 516 17 18 19 615 24 25 20 714 23 22 21 813 12 11 10 9' dbuf db 14 dup(?) i1 db 0dh,0ah,'this is a fangzhen programme' db 0dh,0ah,'input q to exit' db 0dh,0ah,'Please input a number(3--5):','$' i2 db 0dh,0ah,'input error,please reinput!','$' n db ? b db 1 data ends stack segment db 100 dup(?) stack ends code segment assume ds:data,cs:code,ss:stack main: mov ax,data mov ds,ax call clear lop: lea dx,i1 mov ah,9 int 21h mov ah,1 int 21h cmp al,'q' jz quit lea si,buf1 mov n,7 mov cl,3 call clear cmp al,'3' jz s lea si,buf2 mov n,10 mov cl,4 cmp al,'4' jz s lea si,buf3 mov cl,5 mov n,13 cmp al,'5' jz s lea dx,i2 mov ah,9 int 21h call clear jmp lop s: mov bl,n lea di,dbuf l: mov al,[si] mov [di],al inc si inc di dec bl jne l mov [di],byte ptr '$' mov ah,2 mov dh,b mov dl,0 int 10h lea dx,dbuf mov ah,9 int 21h inc b loop s jmp lop quit: mov ah,4ch int 21h ;***清屏*** clear proc near push ax push bx push cx push dx mov ah,6 mov al,0 mov ch,0 mov cl,0 mov dh,24 mov dl,79 mov bh,7 int 10h pop dx pop cx pop bx pop ax ret clear endp code ends end main
main.asm的程式碼
data segment buf1 db '1 2 38 9 47 6 5' buf2 db '1 2 3 412 13 14 511 16 15 610 9 8 7' buf3 db '1 2 3 4 516 17 18 19 615 24 25 20 714 23 22 21 813 12 11 10 9' dbuf db 14 dup(?) i1 db 0dh,0ah,'this is a fangzhen programme' db 0dh,0ah,'input q to exit' db 0dh,0ah,'Please input a number(3--5):','$' i2 db 0dh,0ah,'input error,please reinput!','$' n db ? b db 1 data ends stack segment db 100 dup(?) stack ends code segment assume ds:data,cs:code,ss:stack main: mov ax,data mov ds,ax call clear lop: lea dx,i1 mov ah,9 int 21h mov ah,1 int 21h cmp al,'q' jz quit lea si,buf1 mov n,7 mov cl,3 call clear cmp al,'3' jz s lea si,buf2 mov n,10 mov cl,4 cmp al,'4' jz s lea si,buf3 mov cl,5 mov n,13 cmp al,'5' jz s lea dx,i2 mov ah,9 int 21h call clear jmp lop s: mov bl,n lea di,dbuf l: mov al,[si] mov [di],al inc si inc di dec bl jne l mov [di],byte ptr '$' mov ah,2 mov dh,b mov dl,0 int 10h lea dx,dbuf mov ah,9 int 21h inc b loop s jmp lop quit: mov ah,4ch int 21h code ends end main
clear.asm程式碼:
data segment buf1 db '1 2 38 9 47 6 5' buf2 db '1 2 3 412 13 14 511 16 15 610 9 8 7' buf3 db '1 2 3 4 516 17 18 19 615 24 25 20 714 23 22 21 813 12 11 10 9' dbuf db 14 dup(?) i1 db 0dh,0ah,'this is a fangzhen programme' db 0dh,0ah,'input q to exit' db 0dh,0ah,'Please input a number(3--5):','$' i2 db 0dh,0ah,'input error,please reinput!','$' n db ? b db 1 data ends stack segment db 100 dup(?) stack ends code segment assume ds:data,cs:code,ss:stack clear proc near push ax push bx push cx push dx mov ah,6 mov al,0 mov ch,0 mov cl,0 mov dh,24 mov dl,79 mov bh,7 int 10h pop dx pop cx pop bx pop ax ret clear endp code ends end main
程式功能
主程式功能為根據輸入的字元(數字)確定輸出方陣,當輸入’q’時,退出程式;否則,當輸入數字為3~5之間整數時,輸出對應維數的方陣,並跳至接收輸入的部分等待下次輸入。
類似於C語言中的switch - case - break結構。
子程式CLEAR的功能為清屏(滾屏)。具體實現為通過呼叫10H號函式庫中6號子函式進行滾屏操作。
引數傳遞
本題中主程式-子程式間不存在引數傳遞,子程式的作用僅為清屏。
中斷向量及中斷處理函式位置計算
中斷向量:
本題用到了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。