Help with using Array

can anyone help explain or show me how to use an array properly in Microsoft visual as i have no idea where i am going with it at the moment or how to properly get the array into the stack.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  #include <stdio.h>
#include <stdlib.h>
#include "stdafx.h"
int main(void)
{

	char module[] = "Enter the mark for module:";
	char mark[] = "Show Best mark:";
	char format[] = "%d";
	char formatnumber[] = "%d\n";



	int x;
	int myarray[6];  // declaration of an array of integers
	myarray[1] = 0; // initialise the array
	myarray[2] = 0;
	myarray[3] = 0;
	myarray[4] = 0;
	myarray[5] = 0;
	myarray[6] = 0;



   _asm {
lea ebx, myarray  // address of the array (its 0th element) is put in ebx
mov ecx, 5       // size of the array is saved in the counter
mov eax, 0      // eax will be used to hold the sum, initialise to 0
Loop1: add eax,[ebx]   // read an element of the array at the address stored in ebx
add ebx, 4    // int occupies 4 bytes (32 bits)                                               // so to read next element increase address in ebx by 4
		
lea eax, module // this will ask for the number you would like to set in the assembler.   
push eax      // this will push the number you have given into the stack.  
call printf  // call printf will then call the parameter for the stack.
add esp, 4

lea eax, 
push eax
lea eax, format
push eax
call printf
add esp, 8 
			
loop Loop1 // the end of the loop


	return 0;
}
Last edited on
As any tutorial or textbook will tell you, array indices in C++ start at 0, not 1.

So myarray[6] is past the end of your array.
Topic archived. No new replies allowed.