装配A86 - 冒泡排序

时间:2015-03-17 17:08:57

标签: sorting assembly x86 bubble-sort

我正在开发一个汇编程序来接受一串字符,用Bubble Sort对它们进行排序,然后输出已排序的字符串。我无法访问调试器,这就是我不得不寻求帮助的原因。我的程序似乎陷入了BSORT子程序的无限循环中,我似乎无法追踪造成它的原因。这是一项任务,所以我宁愿解释我出错的地方/我做错了什么而不仅仅是解决方案。

这是我的代码:

TITLE   BSORT   
PAGE    60, 132

;   This program gets a string of input
;   and sorts it by ASCII value, then outputs
;   the sorted string to the monitor
;
;   Define constants
;
CR  EQU     0DH     ;define carriage return
LF  EQU     0AH     ;define line feed
EOM EQU     '$'     ;define end of message marker
NULL    EQU     00H     ;define NULL byte
;
;   Define variables
;
JMP START
PROMPT  DB      CR, LF, "Enter a string of characters (max 100): ", EOM
INBUF   DB      100, ?, 100 DUP ?
MESSAGE DB      CR, LF, "The sorted string is: ", CR, LF, CR, LF, EOM
;
;
;   Program Code
;           
;   
BSORT:  
;|——————————
;| CX holds count
;| SI points to first item
;|____________

    MOV BX, CX      ;BX will be inner loop variable
    DEC BX      ;last value will be EOM marker

OUTER:  DEC CX
    JZ  DONE        ;when the count is zero, we’re done

    XOR DL, DL      ;DL (hasSwapped) = false
    MOV SI, DI      ;reset SI

INNER:  MOV AX, [SI]    ;load 2 characters
    CMP AL, AH      ;first char is in AL, second is AH
    JBE NOSWAP      ;if AL <= AH, no swap needed
    MOV DL, 1       ;else, hasSwapped = true
    XCHG    AL, AH      ;swap first and second chars
    MOV [SI], AX    ;put swapped chars back in string

NOSWAP: INC SI      ;next chars
    CMP SI, BX
    JL  INNER
    TEST    DL, DL      ;else, did we swap?
    JNZ OUTER       ;if yes, loop again

DONE:   RET         ;if done, return to main program
;End subroutine

READ MACRO
 MOV     AH, 0AH
 LEA     DX, #1
 INT     21H
 #EM


WRITE MACRO
 MOV     AH, 09H
 LEA     DX, #1
 INT     21H
 #EM
;
;main
START:  WRITE PROMPT
        READ INBUF

        LEA SI, INBUF+2 ;point SI to beginning of array
        MOV CX, SI      ;save SI in the CX register
        ADD CL, B[SI-1] ;add the count that the OS records to the CL register
        ADC CH, 0       ;if carry, add to upper bits
        MOV SI, CX      ;move back to SI and see what it points to
        MOV B[SI], '$'  ;replace carriage return with end of text marker
        MOV CX, SI  ;put count in CX

        LEA SI, INBUF+2 ;point SI to beginning of array
        LEA DI, INBUF+2 ;copy of beginning of array 
        CALL BSORT

        WRITE MESSAGE
        WRITE [INBUF+2]

        MOV AX, 2C00H
        INT 21H ;return control to OS
;
;   Program End
;
        END START

当它运行时,它打印提示并接收字符串而没有明显的问题,但它从不打印消息或排序的字符串。以前,我得到它来打印消息和字符串(不知道我改变了什么使它停止),但它没有完全排序。

非常感谢任何帮助/指导。

编辑1:好的,好消息是,我在循环中发现我的问题导致无限循环(我认为)。但排序并没有发生,现在我也得到了一些非常有趣的输出。这是输出时的输出:

Any ideas?

有什么想法吗?

编辑2:它正在排序! :d

但是,它仍然在我的字符串的开头插入一个小写的d和黑桃字符。一旦我找到了它的来源,我认为它会被解决!上面的代码已更新。

现在是我的输出:

update output

编辑3:发现我奇怪的字符 - 我打印所有[INBUF],而不仅仅是字符串[INBUF + 2]。谢谢你们的帮助! :)

1 个答案:

答案 0 :(得分:0)

我相信你不会重置SI寄存器。在完成内循环的第一次迭代之后,SI将永远不会小于或等于BX。看看是否存在问题。

MOV BX, CX              ;BX will be inner loop variable

OUTER:  DEC CX
JZ  DONE                ;when the count is zero, we’re done

XOR DL, DL              ;DL (hasSwapped) = false
                        ;You should see if resetting SI here will solve the infinite loop.

INNER:  MOV AX, [SI]    ;load 2 characters
                        ;What happens in the second iteration ?
CMP AL, AH              ;first char is in AL, second is AH
JBE NOSWAP              ;if AL <= AH, no swap needed
MOV DL, 1               ;else, hasSwapped = true
XCHG    AL, AH          ;swap first and second chars
MOV [SI], AX            ;put swapped chars back in string

NOSWAP: INC SI          ;next chars
CMP SI, BX              ;In the first iteration of the inner loop that should be ok.
JLE INNER
                        ;At this point SI kept its value
TEST    DL, DL          ;else, did we swap?
JNZ OUTER               ;if yes, loop again