Hi, everyone! This is my first post ever here in Daniweb. I experienced a problem in printing the sorted array of 5 numbers, as the following code only prints the first iteration of the loop, and it does not print the array input.

Here's the code:

section .data
    i           db 0                    ; Value to be incremented
    question    db  'Enter a number: '  ; Prompt
    questionLen equ $-question
    newLine     db 10, 10, 0            ; New blank line
    newLineLen  equ $-newLine

section .bss
    num resb 5          ; Array of size 5
    counter resb 1      ; Value to be incremented
    counter2 resb 1     ; Value to be incremented
    temp resb 1
    temp2 resb 1

section .text
    global _start

_start:
    mov esi, 0

getInput:
    mov eax, 4
    mov ebx, 1
    mov ecx, question           ; Prints the question
    mov edx, questionLen
    int 80h

    add byte[i], 30h            ; I'll retain this expression, since the program experienced an error
                                ; when this expression is deleted
    sub byte[i], 30h            ; Converts the increment value to integer

    mov eax, 3
    mov ebx, 0
    lea ecx, [num + esi]        ; Element of the array
    mov edx, 2
    int 80h

    inc esi
    inc byte[i]
    cmp byte[i], 5              ; As long as the array hasn't reached the size of 5,
    jl getInput                 ; the program continues to ask input from the user

    mov esi, 0
    mov byte[i], 0
    mov edi, 0                  ; Index of the array

bubble_sort:
    mov byte[counter], 0
    mov byte[counter2], 0

    begin_for_1:
        mov al, 0
        mov al, [counter]       ; Acts as the outer for loop
        cmp al, 5
        jg printArray           ; Prints the sorted list when the array size has reached 5
    begin_for_2:
        mov edi, [counter2]     ; Acts as the inner for loop
        cmp edi, 4
        jg end_for_2
        mov bl, 0               ; Acts as the if statement
        mov cl, 0
        mov bl, [num + edi]
        mov cl, [num + edi + 1]
        mov byte[temp], cl      ; This is the same as if(a[j] > a[j + 1]){...}
        cmp bl, [temp]
        jg bubbleSortSwap
    return:
        inc edi                 ; Same as j++
        jmp begin_for_2         ; Goes out of the inner for loop
    end_for_2:
        inc byte[counter]       ; Same as i++
        jmp begin_for_1         ; Goes out of the outer for loop

bubbleSortSwap:
    mov [num + edi + 1], bl
    mov [num + edi], cl         ; The set of statements is the same as swap(&a[j], &a[j + 1]);
    jmp return

printArray:
    mov eax, 4
    mov ebx, 1
    mov ecx, [num + esi]        ; Prints one element at a time
    mov edx, 1
    int 80h

    inc esi
    inc byte[i]
    cmp byte[i], 5
    jl printArray               ; As long as the array size hasn't reached 5, printing continues

    mov eax, 4
    mov ebx, 1
    mov ecx, newLine            ; Displays a new blank line after the array
    mov edx, newLineLen
    int 80h

    mov eax, 1                  ; Exits the program
    mov ebx, 0
    int 80h

But the output goes like:
Enter a number: 7
Enter a number: 1
Enter a number: 4
Enter a number: 3
Enter a number: 5
17435

Another one:
Enter a number: 7
Enter a number: 1
Enter a number: 4
Enter a number: 3
Enter a number: 5
47132

How can I print the array input and the final output after several iterations? I hope you could help me in this matter.

Thanks and best regards!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.