【发布时间】:2016-02-19 13:03:52
【问题描述】:
我目前正在Assembly 中编写一个程序来查看一个数组是否已排序。这是我的代码:
.DATA
inputIntMessage BYTE "Enter an integer: ", 0
intArray DWORD 4 DUP (?)
integerInput DWORD ?
sorted DWORD ?
.CODE
main PROC
;Here is where I insert whatever the user input into the array
mov eax, 0
mov ecx, LENGTHOF intArray
L1:
intInput inputIntMessage, integerInput ;This is the user input
mov ebx, integerInput
mov intArray[eax*4], ebx
inc eax
loop L1
call if_sorted
intOutput sorted
INVOKE ExitProcess, 0
if_sorted PROC
mov esi, OFFSET intArray
mov ebx, 0
mov ecx, LENGTHOF intArray
L2:
mov eax, [esi + TYPE intArray * ebx]
inc ebx
cmp eax, [esi+ TYPE intArray * ebx]
jle less_than_or_equal
jg greater_than
less_than_or_equal:
mov sorted, 1
loop L2
greater_than:
mov sorted, -1
ret
if_sorted endp
main ENDP
if_sorted 过程根据数组是否已排序返回 1 或 -1(如果数组已排序,则返回 1,否则返回 -1)。我已经运行了调试器,当数组排序后,调试器转到call if_sorted 行,然后立即转到intOutput sorted 并输出-1(当它应该运行 if_sorted 过程时)。当数组未排序时,调试器转到call if_sorted 行并正确运行该过程,并输出-1。有什么想法吗?
【问题讨论】:
-
也许你的意思是在
intOutput sorted之后退出你的程序?因为它一旦打印出值,它将继续执行运行过程中的内容if_sorted -
所以我会在
intOutput sorted之后写main ENDP?但是如果数组是排序的,那么它在打印sorted之前根本不会运行if_sorted。这个问题还有第二部分(因为这个问题我还没有写),就是打印出如果数组已排序,则数组的元素,如果数组未排序,则程序将提示用户再次输入数字。但就像现在一样,它会不断地要求新的数字,因为它总是返回 -1 -
不,您需要正确关闭才能退出程序,否则它将继续执行其下方存在的任何代码(这可能会执行其下方过程中的代码。
-
但我想要的是程序执行 if_sorted 并且 then 执行
intOutput sorted。现在倒退了。 -
好吧,我正在尝试修复会导致问题的明显错误。