Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting nasmubuntu中的冒泡排序_Sorting_Ubuntu_Assembly_Nasm_Bubble Sort - Fatal编程技术网

Sorting nasmubuntu中的冒泡排序

Sorting nasmubuntu中的冒泡排序,sorting,ubuntu,assembly,nasm,bubble-sort,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

我被要求在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

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

不。。。他只是需要整理一些东西!:)

没有为我打印任何输出,如张贴。问题是你把“[contents]”放在ecx中-你想要地址-你在输入例程中做得对

您可以使用较少的变量-使用esi和/或edi作为“计数”和“索引”。如果使用变量,请确保变量的大小与要移入/移出的寄存器的大小相匹配!(“莫夫·伊迪,[counter2]“没有做你想做的)勇气!如果这很容易,每个人都会这么做

最好的, 坦率的