我正在使用 MASM 和 Irvine 的图书馆进行作业。在程序的这一步中,我试图对接收到的数组进行排序,但我在实现时遇到了问题。
ordr_array PROC
pushad
mov ebp, esp
mov edx, [ebp+40] ; array value
mov ebx, 0 ; array position
; [ebp+36] ; length of input_n
mov eax, [ebp+36]
; counts up to ecx
mov eax, 0
looper:
inc eax ; k++
mov ecx, eax
inc ecx ; j = k + 1
inner_looper:
push eax ; we need these registers,
push ecx ; save them
mov eax, [edx+4*eax] ; move our values into our registers for comparison
mov ecx, [edx+4*ecx]
cmp eax, ecx
jge inner_looper_end
mark: ; array[j] > array[i]
mov ebx, ecx ; put the inner counter value (j) to swap with the outer (i)
inner_looper_end:
pop ecx ; restore registers
pop eax ; ecx and eax
inc ecx
cmp ecx, [ebp+36]
jl inner_looper ; if we're done with the inner loop, just fall out
looper_end: ; finish looper
; exchange code
push [edx+4*eax]
push [edx+4*ecx]
call exchangeElements
; end exchange code
; see if we need to go again
inc eax
inc eax ; the '+ 1' then '- 1' was the best way I could think of doing '< result - 1' from memory
cmp eax, [ebp+36]
dec eax
jl looper
popad
ret 8
ordr_array ENDP
调用的交换函数是:
exchangeElements PROC
pushad
mov eax, [ebp+36] ;to be swapped
mov ebx, [ebp+40] ;to be swapped
push [eax]
push [ebx]
pop [eax] ;this should swap it
pop [ebx] ;this should swap it
popad
ret 8
exchangeElements ENDP
向正确方向推进将不胜感激。