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

标签 string sorting assembly x86 x86-16

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

关于string - 在 8086 汇编中对字符串进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14507086/

相关文章:

Java字符串文本规范化重复韩文字符

SQL Select 语句在两个列表之间

控制 GCC 优化

c - 与过程调用和汇编语言相关

Java 按字段对 CSV 文件进行排序

linux - 在 libc 的 ARM 程序集中收到 SIGSTOP 信号?

javascript - 在Javascript中使用正则表达式匹配特殊字符 '-'

java - 重用 String.format 中的参数?

shell - grep 在目录中,并为 Unix Shell 脚本中的每个文件仅返回 1 个结果

algorithm - 快速排序 - 最坏情况