【X86組語】上課用講義內容
徐泓洋老師上課教學用的程式碼。
功能說明:程式每次執行會自動抓取亂數設定牌值與花色,並且依照亂數產生的牌判斷得到的是one pair、two pair...等獎項或是槓龜...
.MODEL SMALL
.DATA
abc equ 61
cde 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,7, 2,3,4,5,7, 1,2,3,6,7
db 1,2,4,6,7, 1,3,4,6,7, 2,3,4,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
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 kind $'
db 'flush straight $'
db 'flush royal $'
db 07h, '%% data error %% $'
;----------------------------------------------------------
.STACK 1000h
.CODE
main proc far
;
mov ax,@data
mov ds,ax
;
call far ptr cdgen
;
call far ptr check_7
;
call far ptr p_result
;
mov ah,4ch
int 21h
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 ;74
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 + si],10
jne strexit
cmp [temp_5 + si],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 ah,02h
mov dl,[card_tab + bx + 0]
or dl,30h
int 21h
mov si,1
prloop: mov dl,","
int 21h
mov dl,[card_tab + bx + si]
or dl,30h
inc si
cmp si,5
jne prloop
pr_exit: ret
p_result endp
;----------------------------------------------------
cdgen proc far
call randomize
mov cx,7
mov dh,52
rndloop: call random
lea bx,card_52
xor ah,ah
add bx,ax
mov ah,[bx]
mov si,cx
mov [card_7 - 1 + si],ah
xchg ah,[card_52 + 44 + si]
mov [bx],ah
dec dh
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,cde
mov dl,al
mul dh
mov al,ah
ret
random endp
END main
沒有留言:
張貼留言