关于汇编输入数排序的小程序!
写了一个关于汇编输入数排序的小程序!
.DOSSEG
.MODEL small, pascal
.STACK
.DATA
.386p
buffer dw 20t dup (0)
ipt_count db 0
temp_use db 0
msg_pro db 'Please input series numbers to compare!0'
taxis_method db 'Method to taxis:',0dh,0ah
db '1:Big --> Small',0dh,0ah
db '2:Small --> Big',0dh,0ah,'0'
exit_msg db 'Press any key to exit this program!0'
;----------------------------------------------------------------------------
;Target: Show LF & CR.
;Input: None
;Output: Print LF & CR.
;----------------------------------------------------------------------------
LF_CR MACRO
push ax
push dx
mov dl,0dh
mov ah,2
int 21h
mov dl,0ah ;next line
mov ah,2
int 21h
pop dx
pop ax
ENDM
;----------------------------------------------------------------------------
;Target: Show the string.
;Input: SI = offset of the string.
;Output: Print the string on the screen.
;----------------------------------------------------------------------------
Show_string MACRO str_off
push ax
push dx
mov si, offset str_off
@@:
cmp byte ptr [si],'0'
jz @F
mov dl,[si]
mov ah,2
int 21h
inc si
jmp @B
@@:
pop dx
pop ax
ENDM
.CODE
.STARTUP
Show_string msg_pro
LF_CR
mov di,offset buffer
call store_in_buf
inc ipt_count
mov word ptr[di+2],bx
LF_CR ;next line
Show_string taxis_method
mov di,offset buffer
movzx cx,ipt_count
mov temp_use,cl
@@:
mov ax,word ptr[di] ;ebullition
mov bx,word ptr[di+2]
.if ax < bx ;xchange
mov word ptr[di+2],ax
mov word ptr[di],bx
.endif
inc di
inc di
loop short @B
dec temp_use
jz short @F
movzx cx,temp_use
mov di,offset buffer
jmp short @B
@@:
mov ah,00
int 16h
cmp al,'1'
jz short taxisB2S
cmp al,'2'
jz short taxisS2B
jmp short @B ;wait a valid key input
taxisB2S:
mov di,offset buffer
movzx cx,ipt_count
@@:
mov ax,word ptr[di]
call ShowNum
inc di
inc di
mov dl,20h ;Output BAR on screen
mov ah,2
int 21h
loop short @B
jmp short @exit
taxisS2B:
mov di,offset buffer
movzx cx,ipt_count
push cx
dec cx
shl cx,1
add di,cx
pop cx
@@:
mov ax,word ptr[di]
call ShowNum
dec di
dec di
mov dl,20h ;Output BAR on screen
mov ah,2
int 21h
loop short @B
@exit:
LF_CR ;next line
Show_string exit_msg
mov ah,00 ;Wait for a key to exit this program
int 16h
.EXIT 0
;---------------------------------------------------------------------------
;Target: Input a HEX number,You can input decimalization & HEX No..
;Input: DI- offset of buffer to store.
;Output: BX-The input HEX number.
store_in_buf:
push ax
push dx
push cx
mov ch,04 ;Max size of input
xor bx,bx
next:
mov ah,0
int 16h
cmp al,0dh ;Input 'Enter',input end
je _exit
cmp al,20h ;Input 'BAR',input next
jz short ipt_next
mov dl,al ;To print the number that input
mov ah,2
int 21h
cmp al,'0'
jb next
cmp al,'9'
ja alpha
sub al,'0'
jmp short @F
alpha:
cmp al,'a' ;To check if input the 'a'-'f'
jb _next
cmp al,'f'
ja next
sub al,57h
jmp short @F
_next:
sub al,37h ;To check if input the 'A'-'F'
cmp al,0ah
jb short next
cmp al,0fh
ja short next
@@:
shl bx,4 ;*16
cbw
add bl,al ;Add bx the Hex
dec ch
jz short ipt_next
mov dh,bh ;To check bx max?
and dh,0f0h
jz short next ;No,next input
ipt_next:
mov dl,20h ;Output BAR on screen
mov ah,2
int 21h
mov word ptr[di],bx
inc di
inc di
inc ipt_count ;Record the number that you input
call store_in_buf ;Recursion invoke
_exit:
pop cx
pop dx
pop ax
ret
;---------------------------------------------------------------------
;Target: Turn a Hex to ASCII and show the number.
;Input: AX,if BX= -1,only show byte.
;Output: Print to screen in DX.
ShowNum:
push eax
push edx
push ecx
cmp bx,0ffffh
jz @F
; mov cl,4
mov cx,4
jmp short _next_near
@@:
; mov cl,2
mov cx,2
_next_near:
mov dx,ax
rol dx,4
lp0: push dx
and dl,0Fh
add dl,'0'
cmp dl,'9'
jbe @F
add dl,7
@@:
mov ah,2
int 21h
pop dx
rol dx,4
loop lp0
pop ecx
pop edx
pop eax
ret
end