我想編寫一個8086彙編程序,它將用戶的5個字符串作爲輸入,然後對這些字符串進行排序並打印排序後的結果作爲輸出。我實際上做了所有事情,但我在分揀部分遇到了很大的問題。我知道如何使用例如冒泡排序來排序從一個特定地址開始的數組中的項目,但在這裏我有5個不在同一個數組中的不同字符串。每個字符串都有自己的地址和自己的字符。我嘗試比較每個字符串的最後一個字符,然後如果一個字符大於另一個字符,我將整個字符串交換,然後繼續爲所有字符串的全部字符做第一個字符。在8086彙編中對字符串進行排序
例如,如果我們輸入字符串是:
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.
任何其他用於分揀這些整個字符串的算法也將被理解。
您所選擇的基數(又名分佈)排序是罰款字符串很短並且具有固定的長度。一般來說,排序是一個足夠複雜的問題,我會先用高級語言編寫一個版本,以便讓我的算法保持直線,然後將其移植到彙編器。 –
你會總是排序5個字符串,從不更多,永遠不會更少?那麼你應該調查[排序網絡](http://stackoverflow.com/questions/2748749/)。這些比一般的排序算法更快更簡單。 –
是的,對於輸入它只是需要排序的5個字符串,不多也不少。 – Haiku