NASM Ubuntu中的冒泡排序

时间:2012-09-04 15:30:09

标签: sorting ubuntu assembly nasm bubble-sort

我被要求在NASM Ubuntu中创建一个冒泡排序程序。这是代码:

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

但唯一的问题是,它无法打印剩余的迭代,因为它只打印第一次迭代:

Enter a number: 7
Enter a number: 1
Enter a number: 4
Enter a number: 3
Enter a number: 5
17435

我想输出的是数组输入和最终输出,从第一次迭代到最后一次。

1 个答案:

答案 0 :(得分:0)

Naw ......他只需要排序一些东西! :)

根据发布,我根本不打印任何输出。问题是你在ecx中放了“[contents]” - 你想要地址 - 你在输入例程中正确地做。

您可以使用更少的变量 - 使用esi和/或edi作为“count”和“index”。如果使用变量,请确保变量的大小与您将其移入/移出的寄存器的大小相匹配! (“mov edi,[counter2]”没有做你想要的)勇气!如果它很容易,那么每个人都会这样做。

最佳, 弗兰克