排序和进制转换的汇编实现

进制转换

可以实现各个进制之间的转换

data segment
	mess1 db 'Input base number:',10,13,'$'  ;输入原数的基数
	mess2 db 'Input your number',10,13,'$'   ;输入原数,字母必须大写
	mess3 db 'Input target base',10,13,'$' 	 ;输入目的数的基数
	mess4 db 'Turned number is ',10,13,'$'   ;目的数
	fun_base dw 1 dup(?)
	base dw 1 dup(?)
	number dw 1 dup(?)
	target dw 1 dup(?)
	turned db 10 dup(?)
	mess5 db 'this'
	temp dw 1 dup(?)
	cnt dw 1 dup(?)
data ends 
stack segment
	dw 100h dup(?)
stack ends

code segment
main proc far
	assume cs:code,ds:data,ss:stack
start:
	mov ax,stack
	mov ss,ax
	mov sp,200h
	push ds
	sub ax,ax
	push ax
	mov ax,data
	mov ds,ax
	lea dx,mess1
	mov ah,09h
	int 21h
	mov fun_base,10
	call input
	mov ax,temp
	mov base,ax
	lea dx,mess2
	mov ah,09h
	int 21h
	mov ax,base
	mov fun_base,ax
	call input
	mov ax,temp
	mov number,ax
	lea dx,mess3
	mov ah,09h
	int 21h
	mov fun_base,10
	call input
	mov ax,temp
	mov target,ax
	call turn
	lea dx,mess4
	mov ah,09h
	int 21h
	call output_turned
	ret
main endp

output_turned proc near
	push ax
	push bx
	push cx
	push dx
	mov cx,cnt 
	mov di,cnt 
output_turned_s:
	mov dl,turned[di]
	add dl,30h
	cmp dl,39h
	jle print
	add dl,7
print:
	mov ah,02h
	int 21h
	dec di
	loop output_turned_s
	mov ax,4C00h
	int 21h
output_turned endp

turn proc near
	push ax
	push bx
	push cx
	push dx
	;把number(base) 转换成 turned(target),其实现在已经是二进制了
	;相当于要把二进制转换成traget进制
	xor di,di
	mov ax,number
	mov bx,target
turn_div:
	div bl  ;al=商,ah=余数
	inc di
	mov turned[di],ah
	mov ah,0
	add ax,0 ;如果要判断一个数是不是0,就这样处理
	jnz turn_div
	mov cnt,di
	pop dx
	pop cx
	pop bx
	pop ax
	ret
turn endp

input proc near  ;读入一个fun_base进制数字,存在temp里
	push ax
	push bx  ;存结果
	push cx
	push dx
	mov temp,0
	mov bx,0
	mov cl,byte ptr fun_base
getchar:
	mov ah,01h
	int 21h
	sub al,30h
	jl getchar_end
	cmp al,9
	jle getchar_ok
	sub al,7
getchar_ok:
	mov ah,0
	mov dx,ax
	mov ax,bx
	mul cl
	add ax,dx
	mov bx,ax
	jmp getchar
getchar_end:
	mov temp,bx
	pop dx
	pop cx
	pop bx
	pop ax
	ret
input endp	

code ends
end start

冒泡排序

data segment
	nums dw 50 dup(?) ; 50 numbers
    count dw ?
    mess1 db 'Enter numbers', 0dh, 0ah, '$'
    mess2 db 'Input error ', 0dh, 0ah, '$'
    mess3 db 'Output numbers ', 0dh, 0ah, '$'
data ends
;二进制在数字后面加上B,十六进制在数字后面加上H,八进制在数字后面加上O
;由此可以实现进制转换
code segment
main proc far
	assume cs:code,ds:data
start:
	push ds
	sub ax,ax
	push ax
	mov ax,data
	mov ds,ax
	call input
    call sort
    call output
    ret
main endp

input proc near  ;输入若干数,以回车作为结束标志
    lea dx,mess1
    mov ah,09h
    int 21h
    mov si,0
    mov count,0
input_enter:
    call decibin
    inc count
    cmp dl,' '  ;cmp可以直接写字符,不用直接写ASCII码
    je input_store 
    cmp dl,13
    je input_exit2
    jne input_error
input_store:
    mov nums[si],bx
    add si,2
    jmp input_enter
input_error:
    lea dx,mess2
    mov ah,09h
    int 21h
input_exit2:
    mov nums[si],bx
    call crlf
    ret
input endp

decibin proc near ;将输入的一个字符串转换为10进制数,得到的数存放在bx里
    mov bx,0 
newchar:    
    mov ah,1
    int 21h
    mov dl,al
    sub al,30h
    jl decibin_exit1
    cmp al,9
    jg decibin_exit1
    cbw

    xchg ax,bx
    mov cx,10   ;可以换成其他数,相当于读不同进制的数
    mul cx
    xchg ax,bx

    add bx,ax
    jmp newchar
decibin_exit1:
    ret
decibin endp

output proc near ;输出数组中的数
    lea dx,mess3
    mov ah,09h
    int 21h
    mov si,0
    mov di,count
output_next1:
    mov bx,nums[si]
    call bindec
    mov dl,' '
    mov ah,02h
    int 21h
    add si,2
    dec di
    jnz output_next1
    call crlf
    ret
output endp

bindec proc near ;将10进制数转换为字符串
    push bx
    push cx
    push si
    push di
    mov cx,100d  ;这里可以换成其他进制,比如100h,100o,100b
    call dec_div
    mov cx,10d
    call dec_div
    mov cx,1d
    call dec_div
    pop di
    pop si
    pop cx
    pop bx
    ret
bindec endp

dec_div proc near ;输出bx/cx
    mov ax,bx
    mov dx,0
    div cx
    mov bx,dx
    mov dl,al
    add dl,30h
    cmp dl,39h
    jle dec_div_out
    add dl,7h
dec_div_out:
    mov ah,02h
    int 21h
    ret
dec_div endp

sort proc near
	push ax		 ;作为交换的中间变量
	push bx      ;作为交换的中间变量
	push cx
	push dx
    mov cx,count
    cmp cx,1
    jle sort_exit

wide_loop:
    mov si,0
    push cx
    mov cx,count
    dec cx
inside_loop:
    mov ax,nums[si]
    mov bx,nums[si+2]
    cmp ax,bx
    jle continue
    ;swap
    xchg ax,bx
    mov nums[si],ax
    mov nums[si+2],bx
continue:
    add si,2
    loop inside_loop
    pop cx
    loop wide_loop
sort_exit:
	pop dx
	pop cx
	pop bx
	pop ax
	ret
sort endp

crlf proc near  ;输出回车换行
    mov dl,10
    mov ah,02h
    int 21h

    mov dl,13
    mov ah,02h
    int 21h
    ret
crlf endp

code ends
end start
posted @ 2023-02-28 17:30  兮何其  阅读(15)  评论(0)    收藏  举报