Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Assembly Language Programming (ALP) Forum (http://www.go4expert.com/forums/assembly-language-programming-forum/)
-   -   Simple array problem (http://www.go4expert.com/forums/simple-array-problem-t3330/)

sanitykey 10Mar2007 02:49

Simple array problem
 
Hi, i am trying to get the user to input values for an array using:
Code:

int main(void){

int marks[4];
char format[]  = "%i";

_asm {

markone:  lea eax, marks[0];
                push eax;
                lea eax, format;
                push eax;
                call scanf;
                add esp,8;

marktwo:  lea eax, marks[1];
                push eax;
                lea eax, format;
                push eax;
                call scanf;
                add esp,8;

markthree: ...etc

I've cut my code down a bit to what i think is relevant.

When i execute that second chunk of code it changes the value for marks[0] and i have no idea why or how to fix it???

Any help would be appreciated and sorry this isn't pure assembly language code is it a c++ hybrid type thing? i don't know lol

sanitykey 10Mar2007 17:30

Re: Simple array problem
 
Sorry to double post but i thought i could clarify a bit...

I've been messing about with my code trying to get it to work here's what it looks like at the moment:

Code:

#include <stdio.h>
#include <stdlib.h>

int main (void)
        {
int marks[4];
int mark1 = 0;
int mark2 = 0;
marks[0] = 0;
marks[1] = 0;
marks[2] = 0;
marks[3] = 0;
char format[] = "\n%i";

_asm {

markone:  lea eax, mark1;
          push eax;
          lea eax, format;
          push eax;
          call scanf;
          add esp,8;

                  mov eax, mark1;
                  mov marks[0], eax;

marktwo:  lea eax, mark2;
          push eax;
          lea eax, format;
          push eax;
          call scanf;
          add esp, 8;

                  mov eax, mark2;
                  mov marks[1], eax;

finish:
}
return 0;
}

I think what i've figured out is that the problem isn't to do with scanf it's just related to how i'm trying to store the values in the array.

I used the values 12 and 5 to test and when i watched the variables when debugging i noticed the only value that changed was marks[0], first it changed to 12 as i wanted but then it changed to 1292 as soon as i executed this line:

Code:

mov marks[1], eax;
I'm using MS Visual Studio 2005 in the C++ environment.

sanitykey 12Mar2007 15:13

Re: Simple array problem
 
I managed to solve my problem the trick was that instead of saying marks[1] i said [marks+4] instead and it pointed to the right place, apparently for some reason by saying marks[1] it was only adding on 1 byte instead of 4 i think.

Thanks for all the views anyway =)

Asmhead 17Mar2007 10:26

Re: Simple array problem
 
Sorry for the late reply but I just found the post and maybe this a moot point if you have already solved your problem. You are right That you have to add 4 to access the next element of the array as it is int type.
Maybe just a couple of other suggestions. It is probably better to use esi register to load the effective address and use a loop to access elements of array.


All times are GMT +5.5. The time now is 21:59.