Inputting Array Values in Assembly: Solving the Problem

  • Thread starter sanitykey
  • Start date
  • Tags
    Array
In summary: The function main() calls the function scanf(), which takes two arguments. The first argument is an array of ints, and the second is a string. The function scanf() reads data from the string and stores it in the int array marks[] according to the format string. The function main() also sets mark1 to 0 and mark2 to 0.
  • #1
sanitykey
93
0
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;
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 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 =)
 
Technology news on Phys.org
  • #2
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?
 
  • #3
is that C with assembly language?
 
  • #4
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:
  • #5
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?
 
  • #6
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.
 
  • #7
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.
 
  • #8
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 separate 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
add esp,8;

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
add esp, 8;

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

finish:
}
return 0;
}
 
Last edited:
  • #9
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;
add esp,8;

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

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 =)
 
  • #10
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.
 
  • #11
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:

1. What is an array in Assembly language?

An array in Assembly language is a collection of data elements that are stored under a single variable name. The elements in an array are typically of the same data type and can be accessed using their index or position within the array.

2. How do you input values into an array in Assembly language?

To input values into an array in Assembly language, you will need to use the mov or move instruction to store the values into the memory addresses of the array. You will also need to use a loop to iterate through the array and input values for each element.

3. How do you solve problems related to inputting array values in Assembly?

To solve problems related to inputting array values in Assembly, you will need to have a clear understanding of how arrays work and how to use the appropriate instructions and loops to input values. You may also need to use debugging techniques and testing to identify and fix any errors in your code.

4. Can you provide an example of inputting an array in Assembly?

Here is an example of inputting an array of 5 integers in Assembly language:

array db 5 dup(?) ; Declare an array of 5 elements
mov bx, offset array ; Move the address of the array into BX register
mov cx, 5 ; Set the counter to 5 for the loop
input_loop: ; Start of the loop
mov ah, 01 ; Input a single character from the keyboard
int 21h ; Store the character in AL register
mov [bx], al ; Store the character in the current position of the array
inc bx ; Increment the index of the array
loop input_loop ; Repeat the loop until the counter reaches 0

5. How can I test my code for inputting array values in Assembly?

You can test your code for inputting array values in Assembly by using a debugger to step through your code and check the values of your array at each step. You can also use test cases with different input values to see if your code produces the expected output. Additionally, you can use tools such as assembly simulators or emulators to run your code and observe the results.

Similar threads

  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
4
Views
733
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
4
Views
8K
  • Programming and Computer Science
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
753
Back
Top