1

我试图弄清楚如何为 SPARC 程序集中的结构分配内存。

这是我正在使用的代码的 C 版本(可以正常工作和编译):

#include <stdio.h>
#include <stdlib.h>
#include "test.h"

int main(int argc, char *argv[])
{
    struct tester test;
    ....other code inbetween

    testfn(&test);
    testfn2(&test);
}

现在在汇编中我想我必须调用这样的函数......

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

我现在坚持的主要部分是如何将初始结构测试器测试传递给程序集。我是在某个地方定义它还是它是如何工作的?

以防万一,我的结构测试器看起来像这样......

#define SIZE 100

struct tester2 {
   char abcd[SIZE];
   char efgh[SIZE];
};

struct tester {
   struct tester2 *somePTR;
   int             an_Int;
};
4

1 回答 1

0

目前尚不清楚您想做什么。在 C 代码中,看起来您正在为堆栈上的结构分配空间。

struct tester test;

在汇编代码中,虽然它包含一些奇怪的语句,但看起来您希望在调用 calloc 时使用结构的大小来分配空间。

mov 1, %o0                ! Prepare for calloc call
mov %l1, %o1              ! Prepare for calloc call

call calloc, 2

所以,决定你想要做什么,或者 1) 通过使用 save 指令减少 %sp 或 2) 只调用 calloc() 为堆栈上的局部变量分配内存。

于 2011-05-17T22:03:54.937 回答