Arrays MASM阵列不循环
更新(2017-11-13): 我添加了另一个变量“index”,并将其设置为零。然后,在每个.IF循环之后,我向索引添加4(DWORD),然后将其传递到esi寄存器并指向正确的数组变量。我还将includedCounter变量移到了.IF循环之外。答案现在是正确的!!Arrays MASM阵列不循环,arrays,loops,masm,Arrays,Loops,Masm,更新(2017-11-13): 我添加了另一个变量“index”,并将其设置为零。然后,在每个.IF循环之后,我向索引添加4(DWORD),然后将其传递到esi寄存器并指向正确的数组变量。我还将includedCounter变量移到了.IF循环之外。答案现在是正确的!! 我试图循环遍历一个数组,只对>=3&&=“lower”value(3)和=ebx)&&(array[esi]的值求和。您的注释(代码内和代码外)表明您认为递增loopCounter也会以某种方式自动递增esi。事实上,这些是不同
我试图循环遍历一个数组,只对>=3&&=“lower”value(3)和=ebx)&&(array[esi]的值求和。您的注释(代码内和代码外)表明您认为递增
loopCounter
也会以某种方式自动递增esi
。事实上,这些是不同的实体(一个是寄存器,另一个是内存中的一个位置),因此这种“自动增量”不会发生。(事实上,在汇编代码中通常不会“自动”发生任何事情。)您认为您的代码的哪一部分应该递增ESI
?如果将ESI添加到数组中以指向数组中的一个值,那么我应该递增ESI以指向数组中的下一个值,对吗?好的,我添加了一个索引变量并将其设置为零。然后,我在每个。if循环之后向索引中添加了4,然后答案现在是正确的。。如果
不是循环。
; Calculates the sum of all array elements
; >= "lower" value (3) and <= "higher" value (8).
INCLUDE Irvine32.inc
.data
sum DWORD ? ; EAX - holds sum of included integers
lower DWORD 3 ; holds lower value
upper DWORD 8 ; holds higher value
;Update (2017-11-13)
index DWORD 0 ; holds index for array
;==============
loopCounter DWORD ? ; ESI - holds loop array pointer
includedCounter DWORD ? ; EDX - holds 'included' counter
array DWORD 3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,8,4 ; values checked
arraySize = ($ - array) / TYPE array ; [current location lounter ($) - array / DWORD] = 20
.code
main PROC
mov eax, sum
mov ebx, lower
mov ecx, upper
mov edx, includedCounter
.WHILE loopCounter < arraySize ; While loopCounter is less than 20
;Update (2017-11-13)
mov esi, index
;===============
.IF (array[esi] >= ebx) && (array[esi] <= ecx)
add eax, array[esi]
inc includedCounter
.ENDIF
;Update (2017-11-13)
add index, 4
inc loopCounter
;================
.ENDW
; Display values
mov sum, eax
mov eax, sum
call WriteInt
call CrLF
mov eax, includedCounter
call WriteInt
Call CrLF
; Exit program
call WaitMsg
exit
main ENDP
END main