📄 modules.asm
字号:
;
; Small tt shot game(asm final), modules section
public CALCUL, CHAR, CURSOR, PRODUCE, READKEY, SCROLL, TIMER, UPDATE,
extrn EXITGAME:far, FUC_M:far
include data.asm ; public data resources
code segment
assume cs:code, ds:data, ss:stack
ACTION proc far ; if hit the char, remove it in the screen and
push si ; re-init the relative CHARS and ROWS
push di
push ax ; ax was a param
push bx
push cx
push dx
lea bx, CHARS
xor si, si
lea di, ROWS
mov cx, UNITS
mov TMPLOW, 0ffh ; init the variable
mov TMPSI, 0
mov TMPDI, 0
AC_nextc: ; find the lowest hit char in CHARS
cmp al, [bx][si]
jne AC_preloop
cmp [di], byte ptr NULL
je AC_preloop
mov dl, [di] ; compare the lines
cmp TMPLOW, dl
jge AC_preloop ; test SF too
mov TMPLOW, dl ; if the new one was lowest, store its data
mov TMPSI, si
mov TMPDI, di
AC_preloop:
inc si
inc di
loop AC_nextc
cmp TMPLOW, 0ffh ; if not found, exit proc
je AC_exit
AC_hit:
mov di, TMPDI
push [di]
push TMPSI
call far ptr CURSOR ; mark the hit char
push ax
push MODE
call far ptr CHAR ; display the hit char
mov si, TMPSI ; get the hit one
mov byte ptr [bx][si], NULL ; re-init the data
mov byte ptr [di], NULL
inc CA_HIT ; record valid hit
cmp BELL, byte ptr 0 ; control the bell
je AC_exit
mov dl, 07h ; bell
mov ah, 2
int 21h
AC_exit:
inc TO_HIT ; record the total hit
push CA_HIT ; calculate hit rate: valid hit/total hit
push TO_HIT
mov cx, 36
mov dx, 73
push cx
push dx
call far ptr CALCUL
pop dx ; pop pushed
pop cx
pop bx
pop ax
pop di
pop si
ret
ACTION endp
CALCUL proc far ; calculate the information
push ax ; 4 arg: (x/y), x, y, row, column
push bx
push cx
push dx
push bp
mov bp, sp
mov ax, [bp+20] ; Y(x/y)
cmp ax, 0 ; if no hit, DO NOT continue
je CA_ascii
mov bx, 100 ; calculate the result
mul bx
mov bx, [bp+18] ; X(x/y)
div bx ; result stroe in ax
CA_ascii: ; store the relative ASCII
mov cl, 10
div cl
add al, 30h ; quotient
add ah, 30h
mov bl, ah
mov bh, 0
mov ah, 0
mov cx, [bp+16] ; row
mov dx, [bp+14] ; column
push cx
push dx
call far ptr CURSOR
cmp ax, 3ah
je CA_100
jb CA_run
CA_100:
mov ax, 31h
push ax
mov ax, 30h
jmp CA_fbit
CA_run:
push BLACK ; BLACK = 0
CA_fbit:
push RATE_CO
call far ptr CHAR
CA_hbit: ; print high 4 bit
push cx
inc dx
push dx
call far ptr CURSOR
push ax
push RATE_CO
call far ptr CHAR
CA_lbit: ; print low 4 bit
push cx
inc dx
push dx
call far ptr CURSOR
push bx
push RATE_CO
call far ptr CHAR
mov ax, 0 ; move the cursor to 0,0
push ax
push ax
call far ptr CURSOR
pop bp
pop dx
pop cx
pop bx
pop ax
ret 8
CALCUL endp
CHAR proc far ; display the Char in color mode.
push ax ; push char, push color
push bx
push cx
push bp
mov bp, sp
mov cx, 1 ; how many times we should display
mov bh, 0 ; zero page
mov bl, [bp+12] ; color
mov ah, 9h ; func
mov al, [bp+14] ; char
int 10h
pop bp
pop cx
pop bx
pop ax
ret 4
CHAR endp
CURSOR proc far ; display the cursor in a certain place
push ax ; push row, push column
push bx
push dx
push bp
mov bp, sp
mov bh, 0 ; page number
mov dh, [bp+14] ; row
mov dl, [bp+12] ; column
mov ah, 2
int 10h
pop bp
pop dx
pop bx
pop ax
ret 4
CURSOR endp
PRODUCE proc far ; generate random char in first line's random position
push ax
push bx
push si
push di
lea si, CHARS
lea di, ROWS
mov BORDER, UNITS ; generate random position in first line
call RANDOM
mov al, RANNUM
cbw
add si, ax ; get proper position in relative CHARS and ROWS
add di, ax
cmp byte ptr [si], NULL ; if there's a char, do not generate
jne PR_exit
mov bx, 0
push bx ; generate in the first line
push ax
call far ptr CURSOR
mov byte ptr [di], NULL ; init the relavive lines
mov BORDER, 26 ; generate random char
call RANDOM
mov al, RANNUM
add al, 61h ; display the low-case
cbw
push ax ; push char
cmp COLOR, 00h ; test whether colorful, then push color
je AC_nocol
mov BORDER, 7 ; get random color
call RANDOM
mov bl, RANNUM
or bl, 1b ; no black color
mov bh, 0
mov MODE, 0 ; when colorful is on, marker change to black
mov MARK, bx
AC_nocol:
push MARK ; mark colors
call far ptr CHAR
mov [si], al ; init the relative chars
inc GENE ; record in GENE variable
PR_exit:
pop di
pop si
pop bx
pop ax
ret
PRODUCE endp
RANDOM proc ; generate random number from 0 to BORDER:byte
push ax ; then store it in RANNUM
push cx
push dx
mov ah, 2ch ; read the clock
int 21h
RA_next:
cmp dl, BORDER ; dl is 1/100 sec
jb RA_exit
shr dl, 1
loop RA_next ; re-get
RA_exit:
mov RANNUM, dl
pop dx
pop cx
pop ax
ret
RANDOM endp
READKEY proc far ; read the keyboard
push ax
push cx
push dx
mov cx, 5 ; read buffer 5 times
RE_loop:
mov dl, 0ffh ; read the input
mov ah, 6
int 21h
jz RE_exit ; if no char was read in, exit
cmp al, 1bh ; if press ESC, exit game
je RE_quit
call far ptr FUC_M ; keystroke to change mode?
call far ptr ACTION ; other keys, pass to ACTION
loop RE_loop
jmp RE_exit
RE_quit:
call far ptr EXITGAME ; if press ESC, call EXITGAME func
RE_exit:
pop dx
pop cx
pop ax
ret
READKEY endp
SCROLL proc far ; scroll down one line, need an pushed arg(lines)
push ax
push bx
push cx
push dx
push bp
mov bp, sp
mov al, [bp+14] ; one line
mov bh, 0
mov cl, 0
mov ch, 0
mov dh, MAX_ROW
mov dl, UNITS
add dl, 3
mov ah, 7 ; func 7, scroll down
int 10h
pop bp
pop dx
pop cx
pop bx
pop ax
ret 2
SCROLL endp
TIMER proc far ; slow the speed, use BIOS's port 61h
push ax
push bx
push cx
mov cx, MOMENT ; count times
TI_loop:
push cx
mov cx, 19e7h ; slow 0.1 sec, number = 0.1s/(15.08us*10^-6)
TI_wait:
in al, 61h
and al, 10h ; check PB4
cmp al, ah ; did it just change
je TI_wait ; wait for change
mov ah, al ; save the new PB4 status
loop TI_wait ; continue until CX == 0
pop cx
loop TI_loop
pop cx
pop bx
pop ax
ret
TIMER endp
UPDATE proc far ; update every column's chars
push si ; and record the lost ones
push di
push ax
push bx
push cx
lea bx, CHARS
xor si, si
lea di, ROWS
mov cx, UNITS
UP_next: ; loop every chars then update the relative lines
cmp byte ptr [bx][si], NULL ; if there's no char in this column, check next position
je UP_nextC
cmp [di], byte ptr BOTTOM ; if the char was escaped the bottom, re-init the data
jb UP_inscr
push [di] ; mark the hit char in the screen
push si
call far ptr CURSOR
push [bx][si]
push BLACK
call far ptr CHAR
mov byte ptr [bx][si], NULL ; re-init the data, then record in the LOST
mov byte ptr [di], NULL
jmp UP_nextC
UP_inscr: ; update the screen chars' relative lines
inc byte ptr [di]
UP_nextC: ; if no char in this column, move to next position
inc si
inc di
loop UP_next ; check next char
UP_exit:
push CA_HIT ; calculate save rate: valid hit/puduced
push GENE
mov cx, 38
mov dx, 73
push cx
push dx
call far ptr CALCUL
pop cx ; pop pushed
pop bx
pop ax
pop di
pop si
ret
UPDATE endp
code ends
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -