简体   繁体   中英

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. However, when I do that I get a random number and ninety-nine 32's as a result. 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.

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. 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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM