1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Simple array problem

Discussion in 'Assembly Language Programming (ALP) Forum' started by sanitykey, Mar 9, 2007.

  1. sanitykey

    sanitykey New Member

    Joined:
    Mar 9, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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
     
    Last edited by a moderator: Mar 10, 2007
  2. sanitykey

    sanitykey New Member

    Joined:
    Mar 9, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  3. sanitykey

    sanitykey New Member

    Joined:
    Mar 9, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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 =)
     
  4. Asmhead

    Asmhead New Member

    Joined:
    Mar 17, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Uranus
    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.
     

Share This Page