0

I have a lab project to add 5 numbers inside an array with a size of WORD. I thought I was doing it correctly, but I am missing something (logic wise as well).

INCLUDE Irvine32.inc

.data
    intarray DWORD 5,6,7,8,9    ; Declares a WORD array with 1,2,3,4,5
    intsum DWORD 0          ; where I want the answer to go (Total should be 35)

.code
main PROC

    MOV eax, intarray   ; EAX = intarray
    MOV ecx, intsum     ; ECX = 0
    L1:
    add eax, ecx
    loop L1

    CALL DumpRegs           
    MOV edi, OFFSET intarray    
    MOV ebx, SIZEOF intarray
    MOV ecx, 1          
    CALL DumpMem        
    CALL WriteINT

    exit

main ENDP
END main

The program runs but its not giving me the correct answer. It should display 35 but I have the feeling I am not including the whole array. Can someone help me out?

Smith John
  • 25
  • 7
  • Use a debugger. Your loop doesn't load from memory; it adds the counter to the pointer register value. And it runs 2^32-1 times https://stackoverflow.com/questions/46881279/how-exactly-does-the-x86-loop-instruction-work. Did you not think it was strange that your program took a whole second to run? (Billions of clock cycles). – Peter Cordes Mar 21 '18 at 22:47
  • This is my first class with assemblyly language and coding, not too familiar with how to use a debugger. I added my intarray to eax, so shouldn't it be adding each value everytime to ecx? I don't understand what you mean when you say its not loading from the memory, because it should be. – Smith John Mar 21 '18 at 23:20
  • `add edx, [eax]` does `edx += dword pointed to by eax`. Your code, `add eax, ecx`, does `eax += ecx`, with no memory reference, just 2 register operands. – Peter Cordes Mar 22 '18 at 01:48
  • The time you spend learning to use a debugger will pay itself back within a day, because it lets you single-step by instructions and watch values in registers change. So you can see how the machine is really running your code, and what changes after every instruction. – Peter Cordes Mar 22 '18 at 01:50

0 Answers0