I'm doing a part C, part Assembly code but I'm getting a segmentation fault. And I haven't figured out why? Can anyone help?
#include <stdio.h>
#define SZ 7
int* a[SZ];
int x, y, z;
void populate() {
x = 1;
y = 2;
z = 3;
a[0] = &x;
a[1] = &y;
a[2] = &z;
a[3] = a[0];
a[4] = a[1];
a[5] = a[2];
a[6] = a[3];
}
void printall()
{
for(int i = 0; i < SZ; i++)
{
printf("a[%1d]=%1d, ", i, *(a[i]));
}
printf("x=%1d, y=%1d, z=%1d\n", x, y, z);
}
void add1each()
{
__asm__("\
pushl %ebp\n\
movl %esp, %ebp\n\
subl $23, %esp\n\
movl $0, -4(%ebp)\n\
jmp compare\n\
increment:\n\
movl -4(%ebp), %eax\n\
movl a(,%eax,4), %eax\n\
movl -4(%ebp), %edx\n\
movl a(,%edx,4), %edx\n\
movl (%edx), %edx\n\
addl $1, %edx\n\
movl %edx, (%eax)\n\
addl $1, -4(%ebp)\n\
compare:\n\
cmpl $6, -4(%ebp)\n\
jle increment\n\
leave\n\
ret\n\
");
}
int main(int argc, char* argv[]) {
populate();
printall();
add1each();
printall();
return 0;
}
Thanks in Advance