0
program test_idea;
#include( "stdlib.hhf" );
static
begin test_idea;
   mov(6, ecx);
   malloc(ecx);
   mov(30, ecx);
   mov(ecx, [eax + 5]);
   mem.free(eax);
end test_idea;

不知道为什么这不起作用。我正在使用 hla 和汇编。

4

1 回答 1

0

环境

  • HLA (High Level Assembler - HLABE back end, POLINK linker) 版本 2.16 build 4413 (prototype)
  • 视窗 10

笔记

  • 查看文档。
  • 分配内存时,您需要分配n * 4字节,其中n是数组中元素的数量。
  • 所有对数组的访问和索引都是相对于数组基地址的。
  • 因此,在索引到数组时,您需要使用b + i * 4whereb = ArrayBaseAddr; i = DesiredIndex;

例子


program array_access;
#include( "stdlib.hhf" );

static
    ArrySize: int32 := 10;
    BaseAddr: pointer to int32;

begin array_access;
    // Store the array size in EAX
    mov(ArrySize, EAX);

    // Multiple array size by 4 
    shl(2, EAX);

    // Allocate storage
    malloc(EAX);       

    // save the BaseAddr of the new array
    mov(EAX, BaseAddr); 

    // Store BaseAddr in EBX
    mov(BaseAddr, EBX);

    // Loop through the Array
    for(mov(0, ESI); ESI < ArrySize; inc(ESI)) do

        // Store the index as the value for the element at the index
        mov(ESI, [EBX + ESI*4 ]);

    endfor;

    // Print the sixth element
    mov(6, ESI);
    mov([EBX + ESI*4], EAX);
    stdout.puti32(EAX);
    stdout.put(nl);

    // Change the sixth element to 73
    mov(73, EDX);
    mov(EDX, [EBX + ESI*4]);

    // Print the sixth element
    mov([EBX + ESI*4], EAX);
    stdout.puti32(EAX);
    stdout.put(nl);

end array_access;

于 2020-06-03T16:54:21.323 回答