首页 文章

在8086汇编中对字符串进行排序

提问于
浏览
1

我想编写一个8086汇编程序,它从用户那里获取5个字符串作为输入,然后对这些字符串进行排序并将排序后的结果打印为输出 . 我实际上做了所有事情,但我对排序部分有很大的问题 . 我知道如何使用例如冒泡排序来排序从特定地址开始的数组中的项目,但在这里我有5个不同的字符串不在同一个数组中 . 每个字符串都有自己的地址和自己的字符 . 我尝试比较每个字符串的最后一个字符,然后如果一个更大,那么另一个我交换整个字符串然后我继续为所有字符串的整个字符做到第一个 .

例如,如果我们的输入字符串是:

eab    
abe    
cbd    
cda    
adb

我将首先对每个字符串的最后一个字符进行排序,然后我想出了这个:

cda    
eab    
adb    
cbd    
abe

然后我将用中间字符比较它们:

eab    
cbd    
abe    
cda    
adb

最后是第一个字符,所有内容都排序:

abe
adb
cbd
cda    
eab

但它实际上是我的想法,我不知道为了我的工作实施谁 .

; multi-segment executable file template.

data segment 
    data1 db 64,?,64 dup(?)
    data2 db 64,?,64 dup(?)
    data3 db 64,?,64 dup(?)
    data4 db 64,?,64 dup(?)
    data5 db 64,?,64 dup(?)

    change db 66 dup(?)

    msg db 0ah,0dh,"You enter a wrong option",0ah,0dh,"try again",0ah,0dh,"$" 
    prompt db 0ah,0dh,"Choose an option:",0ah,0dh,"$"
    prompt1 db ".a: Sort in ascending order",0ah,0dh,"$" 
    prompt2 db ".d: Sort in descending order",0ah,0dh,"$"
    prompt3 db ".q: Quit",0ah,0ah,0dh,"$" 
    enter db 0ah,0ah,0dh,"Enter 5 strings:",0ah,0dh,"$"
    pkey db 0ah,0dh,"press any key...$"
ends

stack segment
    dw   128  dup(0)
ends

code segment
main proc far
; set segment registers:
    mov ax, data
    mov ds, ax
    mov es, ax

again:
; printing the prompts for the user
    lea dx, prompt
    mov ah, 09h
    int 21h   

    lea dx, prompt1
    mov ah, 09h
    int 21h

    lea dx, prompt2
    mov ah, 09h
    int 21h

    lea dx, prompt3
    mov ah, 09h
    int 21h   

; getting a character from the user as an input
    mov ah, 01h
    int 21h

; determining which option the user selects    
    cmp al, 'a'
    je ascending
    cmp al, 'd'
    je descending
    cmp al, 'q'
    je quit

; this is for the time that the user enters a wrong char    
    lea dx, msg
    mov ah, 09h
    int 21h
    jmp again     ; again calling the application to start

ascending:
    call input
    call AscendSort
    jmp again     ; again calling the application to start

descending:
    call input
    call DescendSort
    jmp again     ; again calling the application to start

quit:            
    lea dx, pkey
    mov ah, 9
    int 21h        ; output string at ds:dx

    ; wait for any key....    
    mov ah, 1
    int 21h

    mov ax, 4c00h ; exit to operating system.
    int 21h  
main endp
;.................................................
; this subroutine gets input from user
input proc

    lea dx, enter
    mov ah, 09h
    int 21h
    call newline

    mov ah, 0ah
    lea dx, data1
    int 21h      
    call newline

    mov ah, 0ah
    lea dx, data2
    int 21h
    call newline

    mov ah, 0ah
    lea dx, data3
    int 21h
    call newline

    mov ah, 0ah
    lea dx, data4
    int 21h
    call newline

    mov ah, 0ah
    lea dx, data2
    int 21h
    call newline

    ret 
input endp
;................................................
; sorting the strings in the ascending order
AscendSort proc         

    mov si, 65
    lea dx, change
    mov al, data1[si]
    cmp al, data2[si]
    ja l1    
    ?????

    ret
AscendSort endp 
;................................................
; sorting the strings in the descending order
DescendSort proc



    ret
DescendSort endp 
;................................................
; newline
newline proc

    mov ah, 02h
    mov dl, 0ah
    int 21h

    mov dl, 0dh
    int 21h   

    ret        
newline endp    
ends

end main ; set entry point and stop the assembler.

还将理解用于对这些整个字符串进行排序的任何其他算法 .

1 回答

  • 2

    我实际上自己找出了答案,我使用字符串命令将字符串2比2相互比较,以查看它们是否更大,更小或相等 . 类似下面特定宏中的代码,它接受两个字符串来检查它们并执行所需的操作,如交换字符串以使它们排序:

    check macro a, b
        local next, finish
        cld
        mov cx, 64  ; the size of our buffer that saves the string
        mov si, a
        mov di, b
    
        repe cmpsb  ; comparing two strings with each other
        ja next
        jmp finish
    
    next:
        ; swaping our strings if needed
        mov cx, 64
        mov si, a
        lea di, change
        rep movsb 
    
        mov cx, 64
        mov si, b
        mov di, a
        rep movsb
    
        mov cx, 64
        lea si, change
        mov di, b
        rep movsb 
    
    finish:
    
    endm
    

相关问题