2007-11-12

【X86組語】作業三

把七張牌值印出來,並且將最大獎的組合用顏色強調。

NEW_LINE MACRO
push ax
push dx
mov ah,02
mov dl,10
int 21h
mov dl,13
int 21h
pop dx
pop ax
ENDM

GET_CHAR MACRO char
push ax
mov ah,01
int 21h
mov char,al
pop ax
ENDM


PUT_CHAR MACRO char
mov dl,char
mov ah,02h
int 21h
ENDM

END_PROCESS MACRO
mov ah,4ch
int 21h
ENDM


.model small

.data
abc equ 61
cba equ 55

card_7 db 7 dup(?)
card_5 db 5 dup(?)
temp_5 db 5 dup(?)
max_prize db ?
max_num db ?
ceq db ?
prize db ?
_mask db 00001000b,00000100b,00000010b,00000001b
pr_tab db 0,1,1,3,1,2,3,7,1,2,2,6,3,6,7,10
card_tab db 1,2,3,4,5, 1,2,3,4,6, 1,2,3,5,6
db 1,2,4,5,6, 1,3,4,5,6, 2,3,4,5,6
db 1,2,3,4,7, 1,2,3,5,7, 1,2,4,5,7
db 1,3,4,5,6, 2,3,4,5,7, 1,2,3,6,7
db 1,2,4,6,7, 1,3,4,6,7, 2,3,5,6,7
db 1,2,5,6,7, 1,3,5,6,7, 2,3,5,6,7
db 1,4,5,6,7, 2,4,5,6,7, 3,4,5,6,7
card_52 db 01h,02h,03h,04h,05h,06h,07h
db 08h,09h,0ah,0bh,0ch,0dh
db 11h,12h,13h,14h,15h,16h,17h
db 18h,19h,1ah,1bh,1ch,1dh
db 21h,22h,23h,24h,25h,26h,27h
db 28h,29h,2ah,2bh,2ch,2dh
db 31h,32h,33h,34h,35h,36h,37h
db 38h,39h,3ah,3bh,3ch,3dh
card_28 db 01h,11h,2dh,34h,35h,26h,08h
db 01h,11h,2ch,05h,15h,26h,27h
db 21h,31h,2bh,34h,35h,36h,37h
db 01h,11h,03h,15h,05h,25h,0ah
mess db ' ** sorry !!! ** $'
db 'one pair. $'
db 'two pairs. $'
db 'three of a kind. $'

db 'straight. $'
db 'flush. $'
db 'full house. $'
db 'four of a king. $'
db 'flush straight. $'

db 'flush royal. $'
db 07h,'%% data error %% $'
;============================
.stack 1000h
;============================
.code
main proc far

mov ax,@data
mov ds,ax

bbloop:
call far ptr cdgen
call far ptr check_7
call far ptr p_result
NEW_LINE
GET_CHAR dl
cmp dl,13 ;press ENTER for next turn
je bbloop
END_PROCESS
main endp
;
;---------------------------------------
;
check_7 proc far


mov [max_prize],0
mov [max_num],0

mov cx,21
ckloop1: mov al,05h
mov dl,cl
dec dl
mul dl
mov bx,ax
mov si,5
ckloop2: dec si
mov al,[card_tab + bx + si]
mov di,ax
mov al,[card_7 - 1 + di]
mov [card_5 + si],al
xor si,0
jnz ckloop2

call check_5

mov ah,[prize]
cmp ah,[max_prize]
jbe nextturn

mov [max_prize],ah
mov [max_num],cl
nextturn: loop ckloop1
ret
check_7 endp
;-------------------------------
check_5 proc near
mov [prize],0
mov ah,0fh
call copypure
call sorttemp_5
call compare
cmp [ceq],0
jne setprize

call checkstr


mov ah,30h
call copypure
call compare

cmp [ceq],0fh
jne noflush
cmp1: cmp bh,0
jne cmp2
mov [prize],5
jmp checkexit
cmp2: cmp bh,1
jne cmp3
mov [prize],8
jmp checkexit
cmp3: mov [prize],9
jmp checkexit

noflush: cmp bh,0
je nostraight
mov [prize],4
jmp checkexit

nostraight: mov [ceq],0

setprize: lea bx,pr_tab
mov al,[ceq]
xlat
mov [prize],al

checkexit: ret
check_5 endp
;----------------------------
copypure proc
mov si,5
pureloop: dec si
mov al,[ card_5 + si ]
and al,ah
mov [ temp_5 + si ],al
cmp si,0
jne pureloop
ret
copypure endp
;--------------------------
sorttemp_5 proc near
mov si,4
sortloop1: mov al,[ temp_5 + si ]
mov di,0
sortloop2: cmp al,[ temp_5 + di ]
jge sortnext
xchg al,[ temp_5 + di ]
sortnext: inc di
cmp di,si
jne sortloop2
mov [ temp_5 + si ],al
dec si
jnz sortloop1

ret
sorttemp_5 endp
;----------------------------
compare proc
mov [ceq],0
mov si,0


cmploop: mov ah,[temp_5 + si]
xor ah,[temp_5 + si + 1]
jnz cmpnext
mov al,[_mask + si ]
or [ceq],al
cmpnext: inc si
cmp si,4
jnz cmploop
ret
compare endp
;------------------------------
checkstr proc near

mov bh,0
mov al,[temp_5 +4]
sub al,[temp_5 +0]
cmp al,4
je addbh
cmp al,12
jne strexit
cmp [temp_5 + 1],10 ;@@?
jne strexit
inc bh
addbh: inc bh
strexit: ret
checkstr endp
;---------------------------
p_result proc far

mov bl,[max_prize]
mov al,20
mul bl
lea dx,mess
add dx,ax
mov ah,09h
int 21h
cmp bl,0
je pr_exit

mov bl,[max_num]
dec bl
mov al,05h
mul bl
mov bx,ax
mov si,0
temploop: mov dl,[card_tab + bx + si]
mov [temp_5 + si],dl
inc si
cmp si,5
jne temploop

mov si,0
print_7: mov di,0
temploop2: mov dl,[temp_5 + di]
dec dl
mov dh,0
cmp si,dx
jne tempnext2
mov ah,09h
mov bh,0
mov bl,0001100b
mov cx,3
int 10h
tempnext2:
inc di
cmp di,5
jne temploop2


mov cl,[card_7 + si]
mov ch,cl
and ch,0f0h
or ch,30h
PUT_CHAR ch
mov ch,cl
and ch,0fh
cmp ch,9
jbe not_convert
add ch,7h
not_convert:
add ch,30h
PUT_CHAR ch
PUT_CHAR ' '


inc si
cmp si,7
jne print_7

pr_exit: ret
p_result endp
;-----------------------------
cdgen proc far

call randomize

mov cx,7
mov dh,4
rndloop: call random
lea bx,card_28
mul cl
xor ah,ah
add bx,ax
mov si,cx
mov ah,[bx + si - 1]
mov [card_7 - 1 + si],ah
loop rndloop
ret
cdgen endp
;-------------------------
randomize proc near
mov ah,2ch
int 21h
;shl dh,1
add dl,dh
ret
randomize endp
;-------------------------
random proc near


mov al,abc
mul dl
add ax,cba
mov dl,al
mul dh
mov al,ah
ret
random endp
end main

沒有留言: