Assembly x86 brk() call use

后端 未结 4 1821
孤城傲影
孤城傲影 2020-12-10 16:36

i am trying to dynamically allocate memory into the heap and then assign values in those memory addresses. I understand how to allocate the memory but how would i assign for

4条回答
  •  天命终不由人
    2020-12-10 17:17

    As you have done, call once to retrieve the current bottom of heap, then move the top of heap (which is the brk value). However your code is not correct in using the 64-bit register set r.x. If your Linux is 32-bit (as implied by the use of 45 for the syscall number), then you want the 32-bit register set:

    mov eax, 45                            ; brk                                                                                                                                                                                  
    mov ebx, 0                             ; arg 1: 0 = fail returning brk value in rax                                                                                                                               
    int 0x80                               ; syscall
    mov dword ptr [brk_firstLocation], rax ; save result
    mov eax, 45                            ; brk                                                                                                                                                                                  
    mov ebx, 4                             ; arg 1: allocate 4 bytes for a 32-bit int                                                                                                                                
    int 0x80                               ; syscall
    
    mov eax, dword ptr [brk_firstLocation] ; reload rax with allocated memory address.
    mov dword ptr [eax], 42 ; use result: store 42 in newly allocated storage
    

    Of course you can re-load the saved value for re-use as many times as needed.

提交回复
热议问题