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;

not sure why this does not work. I am using hla and assembly.

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
  • What do you mean by "does not work"? What are you expecting? What happened? What have you tried? – David Harris Dec 05 '14 at 05:23
  • HLA Exception (54) at line 76 in ex_InstallSignals.hla, edx=$BFF6D56C Memory Access Violation I get this error when I try and compile i am trying to move 30 to the 6th byte that was allocated. – Emanuel Tapia Dec 05 '14 at 08:07

1 Answers1

0

ENVIRONMENT

  • HLA (High Level Assembler - HLABE back end, POLINK linker) Version 2.16 build 4413 (prototype)
  • Windows 10

NOTE

  • Checkout this documentation.
  • When allocating the memory you need to allocate n * 4 bytes where n is the number of element in the array.
  • All access and indexing into the array are relative to the array base address.
  • Therefore, when indexing into the array you need to use b + i * 4 where b = ArrayBaseAddr; i = DesiredIndex;

EXAMPLE


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;

Kuma
  • 427
  • 5
  • 17