2012-09-04 3 views
0

NASM Ubuntu에서 버블 정렬 프로그램을 만들도록 요청 받았다. 여기에 코드입니다 :버블 정렬 in NASM 우분투

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 

그러나 유일한 문제는 그것이 단지 같은 제 1 회 반복 인쇄 때문에는 반복의 나머지 부분을 인쇄 할 수있다 :

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

은 내가 출력하고자하는 것은입니다 배열 입력과 첫 번째 반복에서 마지막까지의 최종 출력을 비교합니다.

+3

답변

0

Naw ... 그는 물건을 분류해야합니다. :)

글이 나에게 전혀 출력되지 않습니다. 문제는 ecx에 "[contents]"를 두는 것입니다. 주소를 원하면 입력 루틴에서 바로 처리하십시오.

"count"와 "index"둘 다 esi 또는 edi를 사용하십시오. 변수를 사용하는 경우 변수의 크기가 해당 레지스터의 크기와 일치하는지 확인하십시오! ("mov edi, [counter2]"는 당신이 원하는 것을하지 않습니다) 용기! 그것이 쉬운 wuz 경우에, 모두는 그것을하고있다. 이 숙제는

보다도, 프랭크