gordon sung
gordon sung

Reputation: 605

Finding Smallest Number in List

My goal in this code is to find the smallest number in the list. I used bubble sort method in this case; unfortunately, the code is not giving me the smallest/minimum number. Please take a look, Thanks:

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

Upvotes: 2

Views: 120

Answers (1)

Shift_Left
Shift_Left

Reputation: 1243

I haven't looked over your code, because sorting is an over complicated method for what you want to do. Not only that, but most of us don't pay too much attention to uncommented code. Just takes to long to figure out what you're trying to do.

Simply iterate through the entire list and start with 255 (FFH) in AL let's say. Each time you come across a number that is smaller than the one in AL, then replace it with that value and then when loop is finished, AL will have the lowest value.

If you need to know where it is in the list, you could maybe use AH which would be the difference between start address and current address. Knowledge of the instruction set is essential as finding the length of the string can be simplified by;

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.

Remember, ES needs to point to @DATA

Upvotes: 1

Related Questions