【问题标题】:Finding Smallest Number in List在列表中查找最小的数字
【发布时间】:2017-03-31 02:36:04
【问题描述】:

我在这段代码中的目标是在列表中找到最小的数字。在这种情况下,我使用了冒泡排序方法;不幸的是,代码没有给我最小/最小的数字。请看一下,谢谢:

include irvine32.inc

.data
input               byte        100 dup(0)                         
stringinput         byte        "Enter any string: ",0         
totallength         byte        "The total length is: ",0   
minimum             byte        "The minimum value is: ",0
.code 


stringLength        proc
                    push        ebp
                    mov         ebp, esp
                    push        ebx
                    push        ecx
                    mov         eax, 0
                    mov         ebx, [ebp+8]
L1:
                    mov         ecx, [ebx] ;you can use ecx, cx, ch, cl 
                    cmp         ecx, 0     ;you can use ecx, cx, ch, cl 
                    JE          L2
                    add         ebx, 1
                    add         eax, 1
                    jmp         L1
L2:
                    pop         ecx
                    pop         ebx
                    mov         ebp, esp
                    pop         ebp
                    ret         4

stringLength        endp



BubbleSort PROC uses ECX
    push edx 
    xor ecx,ecx 
    mov ecx, 50

    OUTER_LOOP: 
        push ecx
        xor ecx,ecx
        mov ecx,14
        mov esi, OFFSET input

        COMPARE:
            xor ebx,ebx
            xor edx,edx
            mov bl, byte ptr ds:[esi]
            mov dl, byte ptr ds:[esi+1]
            cmp bl,dl
            jg SWAP1 

            CONTINUE:      
                add esi,2      
                loop COMPARE

        mov esi, OFFSET input

        pop ecx     
        loop OUTER_LOOP

    jmp FINISHED

    SWAP1:
        xchg bl,dl 
        mov byte ptr ds:[esi+1],dl 
        mov byte ptr ds:[esi],bl
        jmp CONTINUE 

    FINISHED:



    pop edx 

    ret 4
BubbleSort ENDP




main proc

                    call        clrscr              
                    mov         edx, offset stringinput     
                    call        writeString
                    mov         edx, offset input 
                    call        writeString      
                    call        stringLength
                    mov         edx, offset input 
                    mov         ecx, sizeof input 
                    call        readstring        
                    call        crlf
                    mov         edx,offset totallength
                    call        writestring
                    call        writedec    
                    call        crlf
                    mov         edx, offset minimum
                    call        crlf
                    call        writeString
                    push        offset input
                    call        BubbleSort
                    mov         edx, offset input 
                    call        writeString     
                    call        crlf

                    exit 
main                endp


                    end         main

【问题讨论】:

    标签: sorting assembly x86 masm irvine32


    【解决方案1】:

    我没有查看您的代码,因为对于您想要做的事情,排序是一种过于复杂的方法。不仅如此,我们大多数人都不会过多关注未注释的代码。只需要很长时间才能弄清楚您要做什么。

    简单地遍历整个列表并从 AL 中的 255 (FFH) 开始,比方说。每次遇到小于 AL 中的数字时,将其替换为该值,然后当循环结束时,AL 将具有最低值。

    如果您需要知道它在列表中的位置,您可以使用 AH 来区分起始地址和当前地址。了解指令集是必不可少的,因为可以通过以下方式简化查找字符串的长度;

    mov     di, input                ; Point to beginning of buffer
    mov     cx, -1                   ; for a maximum of 65535 characters
    xor     al,  al                  ; Looking for NULL
    rep     scasb
    neg     cx
    dec     cx                       ; CX = length of string.
    

    记住,ES 需要指向@DATA

    【讨论】:

    • 对“start with”使用固定值不是一个好主意,使用尽可能高的值是不安全的(例如,您更改了数组的值范围,却忘记调整它)。更好的是:将第一个数字设为最低,并开始与#2 进行比较
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-29
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多