c - Accessing calloced array in GAS assembly -
i have c function allocated memory array going filled natural numbers n.
lets say,
n = 10; array = calloc(n, sizeof(int));
i call upon assembly function have written, don't seem able access array fields. manage find value of n located @ 8(%ebp), , have checked gdb equals n set in c code.
however, when try access first element in array, , move to, example %esi, value not 0 should be.
that using following code
movl 12(%ebp), %esi
edit; of course fill array natural numbers before calling assembly function. did not want type in loop here.
as understands it, parentheses de-refrences first element of array, , copies esi, however, esi contains huge negative number when use info registers on breakpoint set after code in gdb.
so, how access arrays calloced beforehand, , passed assembly function? not possible derefrence, , copy single element?
here c function calls upon assembly function
int main(int argc, char *argv[]){ int n = 10; int *array = calloc(n, sizeof(int)); int i, j; // populate array n for(i = 0; < n; i++){ array[i] = 2 + i; } printf("array: %d \n", sizeof(array)); // run sievs sievs_assembly(n, array); // print prime print_prime(array, n); // free mem free(array); return exit_success; }
i dont want post assembly file whole, since school project, , i'm not asking solving assessment, specific problem. how derefrence array item.
the function prototype
extern void sievs_assembly(int n, int *a);
i thought since pointer *a int array, , first argument n located @ 8(%ebp), first array element 12(%ebp).
how value if not enough movl 12(%ebp), %esi
when do:
movl 12(%ebp), %esi
you have moved memory adress of array %esi. value of first element adress pointing to. can use:
movl (%esi), %eax
this moves first element %eax. brackets basicly mean "what %esi pointing to". size of int 4 bytes (you check 'sizeof(int)'). acces next element use:
movl 4(%esi), %eax
which moves next element %eax.
i've made example program prints 2 values array. note: made windows.
.macro print str #macro name 'print'. 1 argument: 'str' pushl \str #argument names escaped call _printf addl $4, %esp #pop arguments .endm .macro printf str fs #can't overload macro names, let's call 1 'printf' pushl \str pushl \fs #printing numbers requires format srting call _printf addl $8, %esp .endm .text .global _main _main: #actual program, '_main' becomes 'winmain@16' pushl %ebp #push frame movl %esp, %ebp movl $array, %esi #move array pointer $esi. #print %esi pointing printf (%esi), $fs #print newline print $nl #print %esi+4 pointing to. since long 4 bytes #the next element of array 4 bytes further first printf 4(%esi), $fs movl $0, %eax #move 0 return register leave #pop frame ret #return .data fs: .string "%i" #format string nl: .string "\n" #new line array: #some array .long 1,2
this program prints output:
1 2
edit: since got attention, thought i'd update answer macros.
and explain _
prefixes on c library calls , main
; i'm compiling on windows mingw, requires prefixes avoid getting undefined reference errors. on linux they're not needed.
Comments
Post a Comment