[英]I'm having problems adding numbers in an array — x86 MASM assembly
I have to create a random range of 100 counts of numbers from 27 to 58 and then add up all the numbers in the 100 positions for a total amount. 我必须创建一个从27到58的100个数字的随机范围,然后将100个位置中的所有数字加起来总计。 However, when I do that I get a random number and ninety-nine 32's as a result. 但是,当我这样做时,我得到一个随机数和99个32。 I've searched everywhere and tried possible solutions but I'm either getting the same result or random garbage. 我到处搜索并尝试了可能的解决方案,但结果还是相同,还是随机垃圾。 Can someone offer some help? 有人可以提供帮助吗?
INCLUDE irvine32.inc
.data
a DWORD 27
b DWORD 58
delta DWORD ?
source DWORD 100 DUP(?)
prompt BYTE "The sum of the 100 counts in array is ",0
.code
main PROC
Call Randomize
mov edi, 0
mov edi, OFFSET delta
mov esi, OFFSET source
mov eax, b
sub eax, a
inc eax
mov delta, eax
mov ecx, LENGTHOF source
mov eax, 0
L1:
mov eax, delta
call randomrange
add eax, a
mov source, eax
call writedec
mov al, " "
call writechar
loop L1
call crlf
call crlf
mov ecx, SIZEOF source
mov edx, OFFSET prompt
call writestring
l2:
add eax,[esi]
add esi, TYPE delta
call writedec
mov al, " "
call writechar
loop l2
exit
main ENDP
END main
I assume randomrange leaves its random number in EAX. 我假设randomrange在EAX中保留其随机数。
In the L1 loop, you add A to EAX to get your random value and then copy it to the first element of SOURCE every time through the L1 loop. 在L1循环中,将A添加到EAX以获取随机值,然后每次通过L1循环将其复制到SOURCE的第一个元素。 That's why the first element is random, but the rest of the array isn't touched. 这就是为什么第一个元素是随机的,但数组的其余部分没有被触及的原因。 (Note that you have the same problem in L2 -- you always get the value to print from the first element of SOURCE.) (请注意,您在L2中存在相同的问题-您始终会从SOURCE的第一个元素中获取要打印的值。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.