比赛计算器
;--------------------------------------------------------------------
; Filename: calc.asm
; Version: V 1.0
; Writer: Liu Cong,Cui Yang
; Organization: Tianjin University ; Object: This is a demo for the header of assembly language source file.
; Compilation Options: None
; History: 09/20/2011 Init version ;--------------------------------------------------------------------
imr0 equ 0f7h
imr1 equ 00h
cnt0 equ 280h
cnt1 equ 281h
cntcmd equ 283h
p85a equ 290h
p85b equ 291h
p85c equ 292h
p85cmd equ 293h
stacked segment stack
sta dw 100 dup(?)
top equ length sta
stacked ends
data segment
mes1 db 'please input the question.',0ah,0dh,'$' mes2 db 'can not hold the number!',0ah,0dh,'$' mes3 db 'incorrect input!',0ah,0dh,'$' mes4 db 'two-operand support only!',0ah,0dh,'$' mes5 db 'overflow!',0ah,0dh,'$'
tab db 3fh,06h,5bh,4fh,66h,6dh,7dh,07h
db 7fh,67h,77h,7ch,39h,2eh,79h,71h,73h tab1 db 88h,48h,28h,18h,84h,44h,24h,14h
db 82h,42h,22h,12h,81h,41h,21h,11h int_seg dw ?
int_off dw ?
fmimr db 0
fmimr1 db 0
mdisp db 4 dup(?)
operand1 dw 0000h
operand2 dw 0000h
operator db 00h
result dw 0000h
numsys dw 10h
tfor db 60d
temp dw ?
tmp dw ?
time0 dw ?
time1 dw ?
flag0 db 00h
flag1 db 00h
flag2 db 01h
flag3 db 00h
flag4 db 00h
time dw 0000h
data ends
code segment
assume cs:code,ds:data,ss:stacked
start: mov ax, data
mov ds, ax
mov ax, stacked
mov ss, ax
mov sp, top
mov dx, cntcmd ;initialize8253,set interrupt frequency to 1s once
mov al, 37h
out dx, al
mov dx, cnt0
mov al, 00h
out dx, al
mov al, 10h
out dx, al
mov dx, cntcmd
mov al, 77h
out dx, al
mov dx, cnt1
mov al, 00h
out dx, al
mov al, 10h
out dx, al
mov dx, p85cmd
mov al, 88h
out dx, al
mov ax, 350bh
int 21h
mov ax, es
mov int_seg, ax
mov int_off, bx
push ds
mov ax, seg int_pr
mov ds, ax
mov dx, offset int_pr
mov ax, 250bh
int 21h
pop ds
in al, 21h
mov fmimr, al
and al, 0f7h
out 21h, al
in al, 0a1h
mov fmimr1, al
and al, 00h
out 0a1h, al
begin:or flag0, 08h
xor ax, ax
call disp
xor dx, dx
mov al, operator
cmp al, 0ah
je oadd
cmp al, 0bh
je osub
cmp al, 0ch
je omul
cmp al, 0dh
je odiv
oadd: mov ax, operand1
add ax, operand2
mov result, ax
jmp over0
osub: mov ax, operand1
sub ax, operand2
mov result, ax
jmp over0
omul: mov ax, operand1
mul operand2
mov result, ax
jmp over0
odiv: mov ax, operand1
div operand2
mov result, ax
over0: mov ah, 0
int 16h
mov time, 0000h
mov flag3, 01h
sti
lop: hlt
mov ah, 0bh
int 21h
add al, 1
jnz lop
mov flag3, 00h
cli
mov ax, time
div tfor
xchg ah, al
and flag0, 0f7h
mov si, 200h
mov cx, 2
mov numsys, 10d output:mov ax, result
call disp
mov dx, 9999h delay2:dec dx
cmp dx, 0
jnz delay2
mov flag4, 1
mov ax, time
call disp
mov flag4, 0
mov dx, 9999h delay1:dec dx
cmp dx, 0
jnz delay1
loop output
mov operand1, 0
mov operand2, 0
mov operator, 0
mov flag0, 0
mov flag1, 0
mov flag2, 1
mov flag3, 0
mov flag4, 0
jmp begin
hlt
mov dx, int_seg
mov ax, int_off
mov ds, ax
mov ax, 250bh
int 21h
mov al, fmimr
out 21h, al
mov al, fmimr1
out 0a1h, al
disp proc
push ax
push bx
push cx
push dx
push di
push si
pushf
newn: mov bx, offset mdisp
xor cx, cx sep: xor dx, dx
div numsys
mov di, cx
mov [bx][di], dl
inc cx
cmp ax,0
jnz sep
lc: inc di
xor dl, dl
mov [bx][di], dl
cmp di, 3
jne lc
mov al, flag4
cmp al, 0
jz xlt
mov cx, 4
xlt: mov temp, cx refh: mov cx, temp
mov di, cx
dec di
mov ah, 01h
shl ah, cl op: shr ah, 1
mov al, ah
mov dx, p85c
out dx, al
mov al, [bx][di]
mov tmp, bx
mov bx, offset tab
xlat
cmp di, 2
jne no
cmp flag4, 0
jz no
add al, 80h
no: mov bx, tmp
dec di
mov dx, p85b
out dx, al
mov al, flag0
and al, 08h
cmp al, 0
jnz iping
mov cx, 9999h ag: loop ag
jmp tst
iping:mov cx, 20h delay0:mov al, flag0
and al, 04h
cmp al, 0
jnz over
push ax
call readnum
call store
mov dl, flag0
and dl, 02h
cmp dl, 0
jz goon
and flag0, 0fdh
pop di
jmp newn
lim: jmp refh
goon: pop ax
loop delay0
tst: cmp ah, 01h
jnz op
mov dl, flag0
and dl, 08h
cmp dl, 0
jnz lim
dec si
cmp si, 0
jnz lim
mov dx, p85c
mov al, 00h
out dx, al
over: popf
pop si
pop di
pop dx
pop cx
pop bx
pop ax
ret
disp endp
store proc
push bx
push cx
push dx
mov bl, flag0
and bl, 02h
cmp bl, 0
jz jp0
mov bl, flag2
and bl, 01h
jz inopd2
cmp al, 09h
ja stopt
mov numsys,10d
mov bx, ax
mov ax, operand1
mul numsys
add ax, bx
mov operand1, ax jp0: jmp rtn
inopd2:mov bl, flag2
and bl, 04h
jz error0
cmp al, 09h
ja stopt
mov numsys, 10d
mov bx, ax
mov ax, operand2
mul numsys
add ax, bx
mov operand2, ax
jmp rtn
error0:mov dx, offset mes3
mov ah, 09h
int 21h
jmp rtn
stopt:and flag2, 11111110b
or flag2, 04h
cmp al, 0ah
jnz chk1
mov operator, 0ah
or flag2, 04h
mov numsys, 10h
jmp rtn
chk1: cmp al, 0bh
jnz chk2
mov operator, 0bh
or flag2, 04h
mov numsys, 10h
jmp rtn
chk2: cmp al, 0ch
jnz chk3
mov operator, 0ch
or flag2, 04h
mov numsys, 10h
jmp rtn
chk3: cmp al, 0dh
jnz chk4
mov operator, 0dh
or flag2, 04h
mov numsys, 10h
jmp rtn
chk4: cmp al, 0fh
jnz error0
or flag0, 04h
mov numsys, 10h
jmp rtn
rtn: pop dx
pop cx
pop bx
ret
store endp
readnum proc
push bx
push cx
push dx
push di
mov al, flag0
and al, 01h
jz ini0
mov al, flag1
not al
or al, 0f0h
mov dx, p85a
out dx, al
mov dx, p85c
in al, dx
not al
and al, 0f0h
cmp al, 0
jnz rtn1
and flag0, 0feh
mov flag1, 00h
jmp rtn1
ini0: mov ah, 01111111b
mov cx, 04h
conr0:mov dx, p85a
rol ah, 1
mov al, ah
out dx, al
mov dx, p85c
in al, dx
or al, 0fh
not al
cmp al, 0
loopz conr0
jz rtn1
not ah
add al, ah
xor ah, ah
mov flag1, al
or flag0, 03h
mov bx, data
mov es, bx
mov di, offset tab1
cld
mov cx, 10h
repnz scasb
mov si, 500d
mov ax, di
sub ax, offset tab1
dec ax
rtn1: pop di
pop dx
pop cx
pop bx
ret
readnum endp
int_pr proc
cli
push ax
pushf
mov al, flag3
cmp al, 0
jz nocount
mov ax, time
inc ax
mov time, ax nocount:mov al, 20h
out 20h, al
sti
popf
pop ax
iret int_pr endp
code ends
end start