0

I need help with allocating memory with system calls in homework project. I get a number 'x' from the command line argument and I need to allocating array of 'x' structs which include two fields of dword.

I tried do that using the brk system call but I think that I don't understand it pretty good, this is my try:

    mov eax, SYS_BRK                 ;using brk system call
    mov ebx, 0
    int 0x80
    mov [CORS], eax                  ;CORS-the array of the structs
    mov [initialallocated], eax

    mov ecx, [dronesNumber]          
    shl ecx, 3                      ;8 bytes * numberOfDrones
    mov eax, SYS_BRK
    mov ebx, [CORS]
    add ebx, ecx
    int 0x80
    mov [CORS], eax

    mov ecx, dronesNumber
    here:
    .myLoop:
            mov eax, SYS_BRK                
            mov ebx, 0
            int 0x80
            mov [CORS + 8*ecx], eax        ;segmentation fault here
            mov eax, SYS_BRK
            mov ebx, [CORS + 4*ecx]
            add ebx, ecx
            int 0x80
            mov [CORS + 4*ecx], eax

        loop .myLoop, ecx

UltimateMath
  • 75
  • 2
  • 8
  • 1
    Four issues at least: 1) `CORS` is the **end** of the allocated memory not the start because you update that value after the brk 2) you set `ecx=dronesNumber` which is the address not the value (you are missing `[]`) 3) even if you load the value, you are accessing outside the array (since that is indexed from zero) and 4) what do you need the brk in the loop for? – Jester May 09 '19 at 22:37
  • Allocate once and carve that up into fixed-size chunks! Also, after the first `brk` query, you know the break and don't have to keep making `brk(0)` calls. So even if you were going to use a loop, this is twice as slow as needed. – Peter Cordes May 09 '19 at 23:40
  • @Jester thank you both, I understood the mistakes – UltimateMath May 10 '19 at 18:46

0 Answers0