2007-10-22

【X86組語】作業二-第二版

;;修正第二運算元,第二運算元如果只有一位元,可以選擇按enter鍵即進行運算。
;;前一版請參考http://new-acos.blogspot.com/2007/10/x86_21.html
GETCHAR MACRO char
push ax
mov ah,01h
int 21h
mov char,al
pop ax
ENDM
PUTCHAR MACRO char
push dx
push ax
mov dl,char


mov ah,02h
int 21h
pop ax
pop dx
ENDM
END_PROCESS MACRO
mov ah,4ch
int 21h
ENDM

.MODEL SMALL
.DATA
values db 2 dup(?)
operator db 1 dup(?)
answer db 1 dup(?)

.STACK

.CODE
main_pro proc far


mov dl,3
count_loop: mov si,0
keyin_loop: GETCHAR bl
cmp bl,'+'
je go_1
cmp bl,'-'
jne continue
go_1: cmp si,0
je invalid
mov [operator],bl
jmp keyin_loop
go_2: mov [operator],bl
jmp finish

continue: call check_num
mov cl,bl
GETCHAR bl
cmp bl,'+'
je go_2
cmp bl,'-'
je go_2
cmp bl,0dh
je finish
mov al,10
mul cl
mov cl,al
call check_num
add cl,bl
finish: mov [values+si],cl
inc si
cmp si,2
jne keyin_loop

mov al,[values]
mov bl,[values+1]
mov cl,[operator]
cmp cl,'-'

jne do_add
sub al,bl
mov [answer],al
call print_ans
jmp ok
do_add: add al,bl
mov [answer],al
call print_ans
ok: dec dl
jnz count_loop

END_PROCESS
main_pro endp

check_num proc far
cmp bl,'0'

jl invalid
cmp bl,'9'
jg invalid
sub bl,'0'

ret
check_num endp

print_ans proc far
call cr
xor ax,ax
mov al,[values]
call print_num
PUTCHAR [operator]
xor ax,ax
mov al,[values+1]
call print_num
PUTCHAR '='
xor ax,ax
mov al,[answer]
call print_num
call lf
ret
print_ans endp

print_num proc far
push cx
mov ch,1
cmp al,100
jb ignore_h
xor ch,ch
PUTCHAR '1'
sub al,100
ignore_h: mov cl,10
div cl
cmp al,0
jne go_3
cmp ch,0
jne go_4
go_3: add al,'0'

PUTCHAR al
go_4: add ah,'0'
PUTCHAR ah
pop cx
ret
print_num endp

cr proc near
push ax
push dx
mov ah,2
mov dl,0dh
int 21h
pop dx
pop ax
ret
cr endp

lf proc far
push ax
push dx
call cr
mov ah,2
mov dl,0ah
int 21h
pop dx
pop ax
ret
lf endp


invalid: PUTCHAR '!'
END_PROCESS
END

沒有留言: