前文
[085][匯編語言]課程設計2 :(1)從軟盤啟動,開機后主界面用含,列出選項
http://www.reibang.com/p/0a99d34113c0
[086][匯編語言]課程設計2 :(2)從軟盤啟動矮慕,功能化選項按鍵 "1"(測試版)
http://www.reibang.com/p/bfe3c83359e8
[087][匯編語言]課程設計2 :(3)從軟盤啟動,功能化選項按鍵 "3"啄骇,F(xiàn)1鍵改顯示顏色痴鳄、ESC鍵返回主選單(測試版)
http://www.reibang.com/p/6adb66c62b81
[088][匯編語言]課程設計2 :(4)動態(tài)顯示當前時間(動態(tài)時鐘) F1鍵改顯示顏色 ESC鍵返回(測試版)
http://www.reibang.com/p/f470d973de1e
課程設計2 :(5)進入選項4,輸入字符串缸夹,同步顯示(測試版)
- 新增:進入選項4痪寻,輸入字符串可以同步顯示(尚未完成時間修改的功能)
1、從主選單虽惭,按下按鍵4橡类,進入選項4;
2芽唇、輸入字符串顾画,可以同步顯示;
3匆笤、按下 Backpace 退格鍵可以刪除字符研侣;
4、按下Enter 鍵返回到主選單炮捧;
運行測試
代碼修改:taskcode.asm文件 task 功能程序部分
- 增加入口
key4: call setclock
jmp short taskstart
- 新增 子程序 setclock
;--------------------------------------------------------------
setclock: call clear_screen
call clear_stack
call getstr
ret
clear_stack: push di
push bx
mov di,offset top - offset task + 7E00H
mov bx,0
mov ds:[di],bx
pop di
pop bx
ret
;--------------------------------------------------------------
; 子程序: 字符串輸入
;(1)庶诡、調用16H讀取鍵盤輸入
;(2)、如果是字符咆课,進入字符棧末誓,顯示字符棧中的所有字符;繼續(xù)執(zhí)行 (1)
;(3)书蚪、如果是退格鍵喇澡,從字符棧中彈出一個字符,顯示字符棧中的所有字符來善炫,繼續(xù)執(zhí)行(1)
;(4)撩幽、如果是enter鍵,向字符棧中壓入一個0箩艺,返回窜醉。
;--------------------------------------------------------------
getstr: push ax
getstrs: mov ah,0
int 16H
cmp al,20H
jb nochar ; ASCII碼小于20H,說明不是字符
mov ah,0
call charstack ;字符入棧
mov ah,2
call charstack ;顯示棧中的字符
jmp getstrs
nochar: cmp ah,0eh ;退格鍵的掃描碼
je backspace
cmp ah,1ch ;Enter鍵的掃描碼
je enter2
jmp getstrs
backspace: mov ah,1
call charstack ;字符出棧
mov ah,2
call charstack ;顯示棧中的字符
jmp getstrs
enter2: mov al,0
mov ah,0
call charstack ;0 入棧
mov ah,2
call charstack ;顯示棧中的字符
pop ax
ret
;--------------------------------------------------------------
; 字符棧的入棧艺谆、出棧和顯示
; 參數(shù): (ah)=功能號榨惰,0表示入棧,1表示出棧静汤,2表示顯示
; ds:si 指向字符椑糯撸空間
; 對于0號功能居凶,(al)=入棧字符
; 對于1號功能,(al)=返回字符串
; 對于2號功能藤抡,(dh)侠碧、(dl)=字符串在屏幕上顯示的行、列位置
;--------------------------------------------------------------
charstack: jmp short charstart
table dw offset charpush - offset task + 7E00H,offset charpop - offset task + 7E00H,offset charshow - offset task + 7E00H
top dw 0 ;棧頂
timestack db 12 dup(0)
charstart: push bx
push dx
push di
push es
push bp
cmp ah,2
ja sret
mov bl,ah
mov bh,0
add bx,bx
mov di,offset table - offset task + 7E00H
jmp word ptr ds:[di+bx]
;--------------------------------------------------------------
charpush: mov di,offset top - offset task + 7E00H
mov bx,ds:[di]
mov si,offset timestack - offset task + 7E00H
mov ds:[si][bx],al
inc bx
mov ds:[di],bx
jmp sret
;--------------------------------------------------------------
charpop: mov di,offset top - offset task + 7E00H
mov bx,ds:[di]
cmp bx,0
je sret
dec bx
mov ds:[di],bx
mov bx,ds:[di]
mov si,offset timestack - offset task + 7E00H
mov al,ds:[si][bx]
jmp sret
;--------------------------------------------------------------
charshow: mov di,160*10+20*2
mov bx,0
charshows: mov bp,offset top - offset task + 7E00H
cmp bx,ds:[bp]
jne noempty
mov byte ptr es:[di],' '
jmp sret
noempty: mov si,offset timestack - offset task + 7E00H
mov al,ds:[si][bx] ; bx = 0 表示棧底
mov es:[di],al
mov al,02H
mov es:[di+1],al ; 設置顏色屬性 為綠色
mov byte ptr es:[di+2],' '
inc bx
add di,2
jmp charshows
;--------------------------------------------------------------
sret: pop bp
pop es
pop di
pop dx
pop bx
ret
;--------------------------------------------------------------
完整源碼
bootcode.asm 不做修改 參見
taskcode.asm : 尚未功能化對時間的修改
assume cs:code
data segment
db 1024 dup (0)
data ends
code segment
start:
call setup
call write
mov ax,4c00h
int 21H
setup:
; 任務程序 安裝步驟
; 將任務程序機器碼安裝到data段開辟的內存空間
mov ax,cs
mov ds,ax
mov si, offset task
mov ax,data
mov es,ax
mov di,0
mov cx,offset taskend - offset task
cld
rep movsb
ret
write:
; 利用 int 13H 中斷
; 將內存 data 處的任務程序
; 寫入 軟盤A 0面 0道 2扇區(qū)
mov ax,data
mov es,ax
mov bx,0
mov al,2 ; 扇區(qū)數(shù)
mov ch,0
mov cl,2
mov dl,0
mov dh,0
mov ah,3
int 13h
ret
;--------------------------------------------------------------
; 任務程序 開始
; 任務程序 從 標號 task 開始 到 標號 taskend 結束
;--------------------------------------------------------------
task: jmp short taskstart
menu_1 db '1) reset pc',0
menu_2 db '2) start system',0
menu_3 db '3) clock',0
menu_4 db '4) set clock',0
menu_address dw offset menu_1 - offset task + 7E00H
dw offset menu_2 - offset task + 7E00H
dw offset menu_3 - offset task + 7E00H
dw offset menu_4 - offset task + 7E00H
;stack db 50 dup(0)
time db 9,8,7,4,2,0
blank db '// ::'
adddata dw 0,0
aESC db 0
taskstart:
call init_reg
call clear_screen
call toppage
jmp short key_toppage
mov ax,4c00h
int 21H
;--------------------------------------------------------------
; 選項控制: key_toppage
; 功能: 針對首頁 toppage 的鍵盤操作
; 實現(xiàn): 使用 BIOS int 16h 0號功能缠黍,讀取一個鍵盤輸入
; 返回值 (ah) = 掃描碼 , (al) = ASCII碼
; 鍵 1 2 3 4
; 掃描碼 02 03 04 05
;--------------------------------------------------------------
key_toppage: mov ah,0
int 16h
cmp ah,02H
je key1
cmp ah,03h
je key2
cmp ah,04h
je key3
cmp ah,05h
je key4
; 本意是弄兜,這時候讀取的輸入,如果不是1234就忽略瓷式,再讀取一個
jmp short key_toppage
key1: call test1
jmp short taskstart
key2:
key3: call clock
jmp short taskstart
key4: call setclock
jmp short taskstart
;--------------------------------------------------------------
test1: call clear_screen
mov di,160*12+40*2
mov al,03H
mov byte ptr es:[di],'t'
mov byte ptr es:[di+1],al
mywait: mov ah,0
int 16H
cmp ah,3BH ; 3BH : F1鍵掃描碼
je ok
jmp short mywait
ok: ret
;--------------------------------------------------------------
; 子程序:clock
; 功能: 循環(huán)顯示當前時間
; F1鍵-改變顯示顏色
; ESC鍵-返回到主選單
;--------------------------------------------------------------
clock: call clear_screen
call clockstart
ret
;--------------------------------------------------------------
; 子程序:clockstart
; 功能: 循環(huán)顯示當前時間
;--------------------------------------------------------------
clockstart: ;ds = 0
push bx
push ax
; (int 9H)1替饿、將原始的int 9H 入口地址保存到新開辟的 adddata 表中,
; 以便日后模擬指令的調用和還原贸典;
mov bx,offset adddata - offset task + 7E00H
push ds:[9*4]
pop ds:[bx]
push ds:[9*4+2]
pop ds:[bx+2]
; (int 9H)2视卢、設置新的int 9H 的入口地址;
cli ; TF = 0
mov word ptr ds:[9*4],offset int9 - offset task + 7E00H
mov word ptr ds:[9*4+2],cs
sti ; TF = 1
mov bx,offset aESC - offset task + 7E00H
mov byte ptr ds:[bx],0
mov ah,'a'
dateloop: call date
mov al,ds:[bx]
cmp al,01H
je clockend
call delay
cmp ah,'z'
jna dateloop
clockend: ; (int 9H)5廊驼、還原原始的int 9H 入口地址据过;
mov bx,offset adddata - offset task + 7E00H
cli ; TF = 0
push ds:[bx]
pop ds:[9*4]
push ds:[bx+2]
pop ds:[9*4+2]
sti ; TF = 1
pop ax
pop bx
ret
;--------------------------------------------------------------
delay: push ax
push dx
mov dx,0003H
mov ax,0
s1: sub ax,1
sbb dx,0
cmp ax,0
jne s1
cmp dx,0
jne s1
pop dx
pop ax
ret
;--------------------------------------------------------------
int9: push bx
in al,60H
pushf
pushf
pop bx
and bh,11111100B
push bx
popf
; (int 9H)3、模擬對原始 int 9H 指令的調用蔬充;
mov bx,offset adddata - offset task + 7E00H
call dword ptr ds:[bx]
; (int 9H)4蝶俱、編寫代碼班利,實現(xiàn)指定按鍵的功能饥漫;
cmp al,01H ; ESC
jne maybeF1
mov bx,offset aESC - offset task + 7E00H
mov ds:[bx],al
maybeF1: cmp al,3BH ; F1
jne int9ret
call changcolor
int9ret: pop bx
iret
;--------------------------------------------------------------
; 子程序:changcolor
; 功能: 改變顯示顏色
;--------------------------------------------------------------
changcolor: push cx
push bx
mov bx,1
mov cx,2000
colors: inc byte ptr es:[bx]
add bx,2
loop colors
pop bx
pop cx
ret
;--------------------------------------------------------------
; 子程序:date
; 功能: 顯示當前時間
;--------------------------------------------------------------
date:
push di
push si
push cx
push ax
mov di,160*12+40*2
mov si,offset time - offset task + 7E00H
mov cx,6
showdate: push cx
mov al,[si]
out 70H,al
in al,71H
mov ah,al
mov cl,4
shr ah,cl
and al,00001111B
add ah,30H
add al,30H
mov byte ptr es:[di],ah
mov byte ptr es:[di+2],al
add di,6
inc si
pop cx
loop showdate
mov di,160*12+40*2
mov si,offset blank - offset task + 7E00H
mov cx,5
showblank: push cx
mov al,[si]
mov byte ptr es:[di+4],al
add di,6
inc si
pop cx
loop showblank
dateend: pop ax
pop cx
pop si
pop di
ret
;--------------------------------------------------------------
setclock: call clear_screen
call clear_stack
call getstr
ret
clear_stack: push di
push bx
mov di,offset top - offset task + 7E00H
mov bx,0
mov ds:[di],bx
pop di
pop bx
ret
;--------------------------------------------------------------
; 子程序: 字符串輸入
;(1)、調用16H讀取鍵盤輸入
;(2)罗标、如果是字符庸队,進入字符棧,顯示字符棧中的所有字符闯割;繼續(xù)執(zhí)行 (1)
;(3)彻消、如果是退格鍵,從字符棧中彈出一個字符宙拉,顯示字符棧中的所有字符來宾尚,繼續(xù)執(zhí)行(1)
;(4)、如果是enter鍵谢澈,向字符棧中壓入一個0煌贴,返回。
;--------------------------------------------------------------
getstr: push ax
getstrs: mov ah,0
int 16H
cmp al,20H
jb nochar ; ASCII碼小于20H锥忿,說明不是字符
mov ah,0
call charstack ;字符入棧
mov ah,2
call charstack ;顯示棧中的字符
jmp getstrs
nochar: cmp ah,0eh ;退格鍵的掃描碼
je backspace
cmp ah,1ch ;Enter鍵的掃描碼
je enter2
jmp getstrs
backspace: mov ah,1
call charstack ;字符出棧
mov ah,2
call charstack ;顯示棧中的字符
jmp getstrs
enter2: mov al,0
mov ah,0
call charstack ;0 入棧
mov ah,2
call charstack ;顯示棧中的字符
pop ax
ret
;--------------------------------------------------------------
; 字符棧的入棧牛郑、出棧和顯示
; 參數(shù): (ah)=功能號,0表示入棧敬鬓,1表示出棧淹朋,2表示顯示
; ds:si 指向字符楏细鳎空間
; 對于0號功能,(al)=入棧字符
; 對于1號功能础芍,(al)=返回字符串
; 對于2號功能杈抢,(dh)、(dl)=字符串在屏幕上顯示的行仑性、列位置
;--------------------------------------------------------------
charstack: jmp short charstart
table dw offset charpush - offset task + 7E00H,offset charpop - offset task + 7E00H,offset charshow - offset task + 7E00H
top dw 0 ;棧頂
timestack db 12 dup(0)
charstart: push bx
push dx
push di
push es
push bp
cmp ah,2
ja sret
mov bl,ah
mov bh,0
add bx,bx
mov di,offset table - offset task + 7E00H
jmp word ptr ds:[di+bx]
;--------------------------------------------------------------
charpush: mov di,offset top - offset task + 7E00H
mov bx,ds:[di]
mov si,offset timestack - offset task + 7E00H
mov ds:[si][bx],al
inc bx
mov ds:[di],bx
jmp sret
;--------------------------------------------------------------
charpop: mov di,offset top - offset task + 7E00H
mov bx,ds:[di]
cmp bx,0
je sret
dec bx
mov ds:[di],bx
mov bx,ds:[di]
mov si,offset timestack - offset task + 7E00H
mov al,ds:[si][bx]
jmp sret
;--------------------------------------------------------------
charshow: mov di,160*10+20*2
mov bx,0
charshows: mov bp,offset top - offset task + 7E00H
cmp bx,ds:[bp]
jne noempty
mov byte ptr es:[di],' '
jmp sret
noempty: mov si,offset timestack - offset task + 7E00H
mov al,ds:[si][bx] ; bx = 0 表示棧底
mov es:[di],al
mov al,02H
mov es:[di+1],al ; 設置顏色屬性 為綠色
mov byte ptr es:[di+2],' '
inc bx
add di,2
jmp charshows
;--------------------------------------------------------------
sret: pop bp
pop es
pop di
pop dx
pop bx
ret
;--------------------------------------------------------------
;--------------------------------------------------------------
; 子程序: toppage
; 功能: 在首頁列出4個選項
;--------------------------------------------------------------
toppage: push bx
push di
push cx
push ax
push ds
push es
push si
mov bx, offset menu_address - offset task + 7E00H
mov di,160*8+25*2
mov cx,4 ; 主頁顯示 4行 功能選項
mov ax,0
mov ds,ax
mov ax,0B800H
mov es,ax
showtoppage: mov si,ds:[bx]
call oneline
add bx,2
add di,160
loop showtoppage
pop si
pop es
pop ds
pop ax
pop cx
pop di
pop bx
ret
oneline: push cx
push di
push si
onelines: mov cl,ds:[si]
mov ch,0
jcxz onelineok
mov byte ptr es:[di],cl
mov ch,02H
mov byte ptr es:[di+1],ch
inc si
add di,2
jmp short onelines
onelineok: pop si
pop di
pop cx
ret
;--------------------------------------------------------------
;--------------------------------------------------------------
; 常用子程序集合 開始
;--------------------------------------------------------------
; 子程序: clear_screen
; 功能: 清屏春感,將顯存中當前屏幕中的字符設為空格符
;--------------------------------------------------------------
clear_screen: push bx
push cx
push es
push ax
mov bx,0B800H
mov es,bx
mov bx,0
mov cx,2000
mov ah,'a'
mov al,00000111B ;黑底白字
clear_screens: mov byte ptr es:[bx],ah
mov byte ptr es:[bx+1],al
add bx,2
loop clear_screens
pop ax
pop es
pop cx
pop bx
ret
;--------------------------------------------------------------
; 子程序:init_reg
; 功能: 寄存器設置
;--------------------------------------------------------------
init_reg: mov ax,0
mov ds,ax
mov ax,0B800H
mov es,ax
ret
;--------------------------------------------------------------
;--------------------------------------------------------------
; 常用子程序集合 結束
;--------------------------------------------------------------
taskend: nop
;--------------------------------------------------------------
; 任務程序結束
;--------------------------------------------------------------
code ends
end start
代碼說明
字符串顯示代碼
- 代碼參考:字符串顯示
-
代碼修改思路
主要是對 charstack 開始的部分做一些修改,把地址標號 table以及top 等結合功能代碼的安裝地址0000:7E00H虏缸,給替換成固定的內存地址鲫懒。
圖片顯示的修改思路摻雜了很多偽代碼,具體實現(xiàn)以【完整源碼】部分為準刽辙。