從鍵盤輸入兩個四位十六進位數:
寫了一個input子程式,輸入一個四位十六進位,調用兩次可實現輸入兩個四位十六進位,輸入的結果第一個存放在num+2開始的字單元,第二個數存放在num 開始的字單元。
[code=Assembly]
data segment
stri db 'input a number :',0ah,0dh,'$'
max db 5
actlen db ?
string db 5 dup(?)
num dw 2 dup(?)
data ends
stack segment
dw 256h dup(0)
stack ends
code segment
assume ds:data,ss:stack,cs:code
start: mov ax,data
mov ds,ax
call input
mov ax,num
mov num+2,ax
mov dl,0ah ;換行
mov ah,2
int 21h
call input
mov ah,4ch
int 21h
input proc near ;輸入一個四位十六進位數,0~F之間的四個字元
push dx
push ax
push si
push cx
push bx
mov dx,offset stri ;顯示字串
mov ah,9h
int 21h
lea dx,max
mov ah,0ah
int 21h ;調用DOS中斷輸入一個四位十六進位
lea si,string ;取字串首址
mov cx,4
xor dx,dx
get: push cx
mov al,[si] ;取一個字元
cmp al,'0'
jb return
cmp al,'F' ;檢查輸入的字元是否在0~F之間
ja return ;否,返回
cmp al,'9' ;轉換為對應的數字
jle deci
sub al,07h
deci: sub al,30h
cbw
or dx,ax
mov bx,dx
and bx,0f000h
jnz nos
mov cl,4
shl dx,cl
nos: inc si
pop cx
loop get
return: mov num ,dx ;儲存結果
pop bx
pop cx
pop si
pop ax
pop dx
ret
input endp
code ends
end start
[/code]
繼續完善。