;---------------------------------------------------------
;条件转移,溢出测试,简单减法,数制转换
;---------------------------------------------------------
.model small
data segment
MAX dw 100
x dw 30
y dw 25
result dw ?
msg_th db 'The x too high !$'
msg_of db 'Overflow !$'
rlt_asc db 10 dup(?)
temp_asc db 10 dup(?)
data ends
code segment
assume cs:code,ds:data
;---------------------------------------------------------
;屏幕打印回车换行符,用到寄存器 ax,dx
;---------------------------------------------------------
macPutEnt macro
mov dl,0dh
mov ah,02h
int 21h
mov dl,0ah
mov ah,02h
int 21h
endm
;---------------------------------------------------------
;屏幕输出一个字符 OPRCH,用到寄存器 dx
;---------------------------------------------------------
macPutChar macro OPRCH
mov dl,OPRCH
mov ah,02h
int 21h
endm
;---------------------------------------------------------
;屏幕输出以 '$' 结尾的字符串,OPR为字符串变量
;用到寄存器 ax,dx
;---------------------------------------------------------
macPutTxt macro OPR
mov dx,offset OPR
mov ah,09h
int 21h
endm
;---------------------------------------------------------
;结束程序,回到 DOS,用到寄存器 ax,dx
;---------------------------------------------------------
macExit macro
mov ah,4ch
int 21h
endm
;---------------------------------------------------------
;将 ax 中的二进制转为十进制ASCALL 码(带符号数)
;结果存于 rlt_asc 中
;---------------------------------------------------------
bin_to_hec_asc proc far
mov bx,0
test ax,8000h
jz positive
mov byte ptr rlt_asc[bx],'-' ;负数
inc bx
neg ax
positive: mov di,0
mov dl,10
nextbh: div dl ;字节操作
add ah,30h
mov byte ptr temp_asc[di],ah
inc di
mov ah,0
cmp al,0
jnz nextbh
nextinbh: dec di
js overbth
mov al,byte ptr temp_asc[di]
mov byte ptr rlt_asc[bx],al
inc bx
jmp nextinbh
overbth: mov byte ptr rlt_asc[bx],' '
inc bx
mov byte ptr rlt_asc[bx],'$'
ret
bin_to_hec_asc endp
;---------------------------------------------------------
;主过程
;---------------------------------------------------------
main proc far
mov ax,data
mov ds,ax
mov ax,x
cmp ax,MAX
jg too_high
sub ax,y
jo over_flow
mov result,ax ;输出结果
mov ax,x
call bin_to_hec_asc
macPutTxt rlt_asc
macPutChar '-'
macPutChar ' '
mov ax,y
call bin_to_hec_asc
macPutTxt rlt_asc
macPutChar '='
macPutChar ' '
mov ax,result
call bin_to_hec_asc
macPutTxt rlt_asc
macPutEnt
jmp exit
too_high:
macPutTxt msg_th
macPutEnt
jmp exit
over_flow:
macPutTxt msg_of
macPutEnt
jmp exit
exit: macExit
main endp
code ends
end main
评论