1

Somehow related to this calling assembly functions from c

I'm passing to that function an array of int and a length:

do_stuff_in_asm (array, len);

The assembly part looks like this

...................
movl 8(%ebp), %eax
movl 12(%ebp), %ecx
movl $0, %edi
...................
movl (%eax,%edi,4), %edx
pushl %edx
pushl $printtext
call printf
addl $8, $esp
..................

I can print the len argument 12(%ebp), but every time I try the same thing on %edx which should be the first element of the array I get a segmentation fault.

int *array, n, i;

printf ("Give array size: ");
scanf("%d",&n);

array = malloc (n * sizeof(int));

Then it's filled up with data.

Community
  • 1
  • 1
  • Are you sure that `array` contains valid '\0'-terminated ASCII string? – Simone Jan 13 '11 at 08:54
  • array is defined as int *array; array = malloc ....; for array<=input .. –  Jan 13 '11 at 08:56
  • Stupid question that needs to be asked: You're checking that `malloc` isn't failing, right? – Chris Lutz Jan 13 '11 at 09:05
  • How are you printing the value? *(int *)($ebp + 8) is a pointer to the first element so (in GDB) x/10x *(int *)($ebp + 8) should print the first 10 values from your array. – diciu Jan 13 '11 at 09:13
  • @diciu I'm trying to put %edx on the stack and use a call to printf. %edx is (%eax,%edi,4) and %eax is supposed to be the address of the array. –  Jan 13 '11 at 09:16
  • oh, so it's printf that segfaults. What address is the segfault on? Are you sure you're using the proper convention when calling printf on your platform (alignment, etc). You can compare by writing a simple printf call and compiling with "gcc -S" to see how the ABI is done compared to what you're doing. – diciu Jan 13 '11 at 09:27
  • @diciu it works if I print len, that is movl 12(%ebp), %ecx the thing in ecx. Fails only when I try to print what should be in edx that is the first element in the array. –  Jan 13 '11 at 10:35
  • In the code sample you've shown, are there any other functions called in between the lines that load `eax` / `ecx` from the arguments on the stack, and the place where `(%eax,%edi,4)` is pushed ? Neither `eax` nor `ecx` are guaranteed to be preserved and would have to be reinitialized after function calls, but it might just so happen `eax` gets clobbered while `ecx` retains what it should have. If you replicate the `movl 8(%ebp), %eax` does it work then ? – FrankH. Jan 13 '11 at 11:02
  • it works with the following lines commented, that is printing the first value in the array but fails if i enable the loop ideone.com/XyrPx asm file and C file ideone.com/mzFD1 –  Jan 13 '11 at 16:44

1 Answers1

0

You should have posted complete code. As an illustration, I have written this sample program. Note, it is only inline asm so that ideone can compile it from a single file. As you can see the code you posted, when used properly, does work. So the problem must be in some other part that you have omitted.

Jester
  • 56,577
  • 4
  • 81
  • 125
  • it works with the following lines commented, that is printing the first value in the array but fails if i enable the loop http://ideone.com/XyrPx asm file and C file http://ideone.com/mzFD1 –  Jan 13 '11 at 16:24
  • @void: As I commented on your other question, called functions are allowed to change eax,ecx and edx. So, the printf call will destroy eax (in fact that contains the return value), you will have to reload it. – Jester Jan 13 '11 at 17:19
  • I was taking so much care of edi and ecx that I lost track of eax, I'm a moron. Thanks –  Jan 13 '11 at 18:34