2007-10-15

【X86組語】作業一

作業一
說明:
  使用者每次輸入五個數值(五張撲克牌),若輸入錯誤牌值或是輸入'!'字元程式結束。
  程式不印中獎名稱,改印中獎編號。
PS. 僅接受01~0d, 11~1d, 21~2d, 31~3d(限小寫)

GETCHAR     MACRO   char
mov ah,01h
int 21h
mov char,al
ENDM
PUTCHAR MACRO char
mov ah,02h
mov dl,char
int 21h
ENDM
END_PROCESS MACRO
mov ah,4ch
int 21h
ENDM


.MODEL SMALL
.DATA
abc equ 61
cde equ 55
key_temp db 2 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 ' ===> 1 $'
db ' ===> 2 $'
db ' ===> 3 $'

db ' ===> 4 $'
db ' ===> 5 $'
db ' ===> 6 $'

db ' ===> 7 $'
db ' ===> 8 $'
db ' ===> 9 $'

db 07h, ' %% error %% $'
;----------------------------------------------------------
.STACK 1000h
.CODE
main proc far
;
mov ax,@data
mov ds,ax

continue: mov di,5
key_round: GETCHAR [key_temp]
cmp [key_temp], '!'
je exit
GETCHAR [key_temp+1]
cmp [key_temp+1], '!'
je exit
PUTCHAR ' '

call transform
dec di
xor di,0
jne key_round
call check_5
;
call far ptr p_result
;
jmp continue

exit: END_PROCESS
main endp
;

transform proc
mov si,2
trans_run: mov ch,[key_temp-1+si]
cmp ch,'0'
jl err
cmp ch,'9'
jle is_num
cmp ch,'a'

jl err
cmp ch,'f'
jg err
sub ch,87
jmp go_f
is_num: sub ch,'0'
go_f: mov [key_temp-1+si],ch
dec si
xor si,0
jnz trans_run
;
mov cl,[key_temp]
mov al,16
mul cl
mov cl,[key_temp+1]
add al,cl
mov [card_5+di-1],al
ret
transform endp

err: PUTCHAR 'E'
PUTCHAR 'R'

PUTCHAR 'R'
PUTCHAR 'O'
PUTCHAR 'R'
PUTCHAR '!'
PUTCHAR '!'

END_PROCESS
;-----------------------------------------------------------------
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

mov al,[temp_5+1]
cmp al,10
jne strexit
inc bh
addbh: inc bh
strexit: ret
checkstr endp
;----------------------------------------------------
p_result proc far
mov bl,[prize]
mov al,16
mul bl
lea dx,mess
add dx,ax
mov ah,09h
int 21h
cmp bl,0
ret
p_result endp
END main

沒有留言: