从概念上讲,我试图将值推入堆栈,并以“相反”的顺序弹出它们。实际上,我认为我正在这样做,但我不确定。我传递的数组在section .data
中定义为array: dd 1, 2, 3, 4, 5, 6
,并将大小作为int值传递(3 = 3个元素)。当我运行应用程序时,它会链接和组装,但数组不会颠倒。NASM反向整数阵列
push dword array
push dword [arrayLen]
call reverse
add esp, 8
; other stuff
reverse:
push ebp ; setup stack
mov ebp,esp
sub esp,0x40 ; 64 bytes of local stack space
; put parameters into registers
mov ebx, [ebp+12] ; array
mov edx, [ebp+8] ; len
; set up loop
mov ecx, 0
; push all the values onto the stack
.loopPush:
mov eax, 4
mul ecx
push dword [ebx]
add ecx, 1
cmp ecx, edx
jl .loopPush
mov ecx, 0
; pop all the values from the stack
.loopPop:
mov eax, 4
mul edx
pop dword [ebx+edx]
add ecx, 1
cmp ecx, edx
jl .loopPop
; print the array
push dword [ebp+12]
push dword [ebp+8]
call printArray
add esp, 8
.end:
mov esp,ebp ; undo "sub esp,0x40" above
pop ebp
mov eax, ebx ; return the reversed array
ret
打印功能准确地打印任何我给它,所以我约90%肯定这不是一个问题,当打印。提前致谢!
使用调试器找出你的代码出错的地方。此外,请评论你的代码,特别是如果你期望别人来帮助你。此外,颠倒数组的简单算法只是从两端走过并交换项目,直到你在中间相遇。不需要计算中途点,也不需要弄乱堆栈。 – Jester 2015-02-09 22:34:32
我不需要像这样计算中途点,只需按下所有值,然后弹出所有值。可能还有其他方法可以做到这一点,但我大多好奇为什么这不起作用。我现在没有调试器,任何建议都会很棒。 – dlkulp 2015-02-09 22:36:46
编写没有调试器的汇编代码 - 祝你好运 – pm100 2015-02-09 22:44:39