在8086 Assembly中对字符串进行排序

我想编写一个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.

将理解用于分拣这些整个字符串的任何其他算法。


我实际上自己找出答案,我使用字符串命令来比较字符串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
链接地址: http://www.djcxy.com/p/62695.html

上一篇: Sorting strings in 8086 Assembly

下一篇: Accessing array members in assembler