Hi,

I am working on a homework assignment, which is to add the values of an array. The array is populated by user input. I am able to store the values, but unable to get the sum. My programs crashes (see comments in code). Any help or guidance is appreciated.


TITLE Arrays (lab5.asm)
INCLUDE Irvine32.inc
.data
startStr  BYTE "Hello, you will be asked to enter a number and hit enter (10 times)", 0
enterStr  BYTE "Please enter a number and hit enter:", 0
myArray  DWORD 10 DUP( 0)
count DWORD 2 ;set counter
.code
main PROC
mov ecx, count ; set counter to 2
mov esi, OFFSET myArray ; set size of array
mov edx, OFFSET startStr
call WriteString
call CRLF
call getInput ; get input from users
call arraySum ; add array sum
call WriteInt
call DumpRegs           
     
    exit
main ENDP

; Gets input from user
getInput PROC

mov edx, OFFSET enterStr
inputLoop: ; start loop
call WriteString
call ReadInt ; get value from user
call WriteInt
mov  eax,   ; store in array
add esi, 4 ; next integer (32 bit integer use 4)
call Crlf
loop inputLoop ; end loop
ret
getInput ENDP
; example from page 139
arraySum PROC
push esi; ; save esi
push ecx ; save ecx
mov eax, 0 ; set eax to 0
L1:
add eax, ; PROGRAM CRASHES HERE
add esi, 4
loop L1;
pop esi; ; restore esi
pop ecx ; restore ecx
ret
arraySum ENDP

END main

Posted on 2009-03-16 17:31:35 by bonekrusher
In getInput, are you sure you want to do mov eax,, and not the other way around (mov ,eax).

By the time you get to arraySum, esi already points to the end of the array. You should set esi to the beginning of myArray before you call arraySum. As well, you want to pop the esi/ecx in reverse order that they are pushed (so restore ecx first, and then restore esi - this is how a stack works).
Posted on 2009-03-16 20:39:43 by comrade
Thank you for the help! As suggested I changed how my array is being stored and the pop order:

getInput PROC

mov edx, OFFSET enterStr
inputLoop: ; start loop
call WriteString
call ReadInt ; get value from user
call WriteInt

mov  , eax ; store in array

add esi, 4 ; next integer (32 bit integer use 4)
call Crlf
loop inputLoop ; end loop
ret
getInput ENDP


As for "You should set esi to the beginning of myArray before you call arraySum", how do I do this?

Thanks,
Posted on 2009-03-17 04:37:45 by bonekrusher
like this...


mov esi, OFFSET myArray ; set size of array << how does this set size anyway? :)
call arraySum ; add array sum

Posted on 2009-03-17 05:33:51 by evlncrn8
Thank you for the help. This is very helpful.

I am still having trouble iterating through the array:


arraySum PROC
mov esi, OFFSET myArray
mov eax, 0                      
L1:

mov eax,
add esi, 4 ; PROGRAM CRASHES HERE
loop L1;


ret
arraySum ENDP


The program crashes when I try to increment the esi register. What am I doing wrong?

Thanks for the help.
Posted on 2009-03-17 11:36:14 by bonekrusher

The program crashes when I try to increment the esi register. What am I doing wrong?


Check to see if ECX is being used by other functions, between setting the initial value of ECX to be used by LOOP in arraySum and actually calling arraySum. If it is being trashed, you are setting yourself up for a basic buffer overflow.
Posted on 2009-03-17 12:56:25 by SpooK
Thanks Spook,

I am totally new to Assembly. Can you elaborate on how to check if  ECX is being used by other functions?
Posted on 2009-03-17 14:51:04 by bonekrusher
Never mind, I figured it out. I never set my loop count for my loop!
mov ecx, count
Posted on 2009-03-17 16:41:02 by bonekrusher