# Simple array problem

## Main Question or Discussion Point

Well i'm hoping it's simple, after all i'm not too good at this lol

i am trying to get the user to input values for an array using:

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

int main(void){

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

_asm {

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

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

markthree: ...etc

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

When i execute marktwo it changes the value for marks[0] and i have no idea why or how to fix it???

Any help would be appreciated, thanks =)

Related Programming and Computer Science News on Phys.org
Hmm, I'm not too fresh with x86 so this is a shot in the dark, but do you want "leal eax, marks[0]"?

What compiler are you using?

is that C with assembly language?

I'm using MS Visual Studio 2005 and when i run it i use the Visual C++ environment and tell it to make a new project which is a Win32 console application.

I then go to project properties and under the C/C++ bit i tell it not to use precompiled headers and change the runtime library under code generation to Multi-threaded (/MT).

I hope that answers your questions, if not how do i find out what compiler i'm using? I should probably know this lol

Just did a slow run through of my code watching the locals especially the marks array values...apparently the only one which changes is marks[0].

I entered in 12 and 5 as tests and set all the array values initially to 0, marks[0] changed to 12 first which was good but as soon as i entered 5 supposedly for marks[1], marks[0] changed to 1292? Then i decided to print the value for marks[1] and even though it said it was 0 in the locals bit it printed out 5???

Last edited:
verty
Homework Helper
Why do you add esp,8? I'm not sure but I'm guessing that scanf pops the two values and pushes the result. Is that right?

As far as i know... :S ... according to my lecturer's notes it takes the top two values off the stack and puts the users input into the variable. But i'm not sure how that works exactly.

verty
Homework Helper
On success, the function returns the number of items succesfully read. This count can match the expected number of readings or fewer, even zero, if a matching failure happens.
In the case of an input failure before any data could be successfully read, EOF is returned.
Ok, this is how I believe it goes: scanf pops the first value of the stack (the string address), parses it and pops values for each delimiter it finds in the string, then it pushes an int at the end.

So in your case you can expect that it will pop twice and push once. Therefore one pop is required to revert the stack to the position it was in, so add esp, 4. Try that.

Ok i tried changing both to add esp, 4 but it came up with the same output and the program crashed not a bad crash just like an emergency landing lol i get that a lot, but if i changed both to add esp, 4 and added a pop eax on the end of each it did the same thing but didn't crash if that helps.

I just tried my code but this time using two seperate variables instead of parts of an array and it worked fine so it must be something to do with the array...

I tried this instead and it did the same thing so i don't think it's to do with scanf either it must be the way i'm trying to store values in the array it's just not right for some reason... :S

#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; // After execution, enter 12, mark1 changes to 12

mov eax, mark1;
mov marks[0], eax; // After execution, marks[0] changes to 12

marktwo: lea eax, mark2;
push eax;
lea eax, format;
push eax;
call scanf; // After execution, enter 5, mark2 changes to 5

mov eax, mark2;
mov marks[1], eax; // After execution, marks[0] changes to 1292,
//marks[1] remains unchanged

finish:
}
return 0;
}

Last edited:
I think i've done it!!! lol hurrah!!!

#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, marks;
push eax;
lea eax, format;
push eax;
call scanf;

marktwo: lea eax, [marks+4];
push eax;
lea eax, format;
push eax;
call scanf;

finish:
}
return 0;
}

I read somewhere that each element of an array consumes 4 bytes so i thought since it always seems to point to the base of my array if i tag on +4 it should point to the next element and i put it inside the brackets to say it's an address in case it thought i meant +4 to the value, but i don't know if that matters.

Thanks for all your replies =)

verty
Homework Helper
Now that I've read about cdecl, I see that the parameters are pushed on the stack but must be cleaned up afterwards, and the return value is placed in eax. So the add esp, 8 is to remove the two parameters from the stack and obviously the return value is unused.

Interestingly, 12 is 0x0C and 1292 is 0x050C so for some reason marks[1] only advances it by one byte.

Here. This code functions. It may be sloppy though, I have not slept yet.

Code:
#include <stdio.h>

#define SIZE 4

extern void markit(int i);

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

int
main()
{
int i;
for(i = 0; i < SIZE; i++){
printf("Mark %i: ", i);
mark(i);
}

for(i = 0; i < SIZE; i++)
printf("marks[%i]: %i\n", i, marks[i]);

return 0;
}
Code:
.global mark
mark:
movl 4(%esp), %eax
shl $2, %eax addl$marks, %eax
pushl %eax
movl $format, %eax push %eax call scanf; addl$8, %esp
ret

Last edited: