Allocating memory for a struct in SPARC Assembly
I'm trying to figure out how to allocate memory for a struct in SPARC assembly.
Here's the C version of my code that I'm using (which works and compiles fine):
#include <stdio.h>
#include <stdlib.h>
#include "test.h"
int main(int argc, char *argv[])
{
struct tester test;
...开发者_Python百科.other code inbetween
testfn(&test);
testfn2(&test);
}
Now in assembly I figure I have to call a function like this...
mov struct tester, %o0 ! Move struct tester into %o0
call sizeof ! Get size of struct tester
nop
set %o0, %l1 ! Store size
nop
mov 1, %o0 ! Prepare for calloc call
mov %l1, %o1 ! Prepare for calloc call
call calloc, 2
nop
mov %o0, %l2 ! The pointer returned to the allocated space
mov %l2, %o0
call testfn
nop
mov %l2, %o0
call testfn2
nop
The main part I'm stuck on right now is how to pass that initial struct tester test into assembly. Do I define it somewhere or how does it work?
And just in case, my struct tester looks something like this...
#define SIZE 100
struct tester2 {
char abcd[SIZE];
char efgh[SIZE];
};
struct tester {
struct tester2 *somePTR;
int an_Int;
};
It is not clear what you want to do. In the C code, it looks like you are allocating space for the structure on the stack.
struct tester test;
In the assembly code, though it contains some odd statements, it looks like you are want to use the size of a structure in a call to calloc to allocate space.
mov 1, %o0 ! Prepare for calloc call
mov %l1, %o1 ! Prepare for calloc call
call calloc, 2
So, decide what you want to do and either 1) allocate memory for a local variable on the stack by using the save instruction to decrement %sp or 2) just call calloc().
精彩评论