Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6940781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:47:50+00:00 2026-05-27T12:47:50+00:00

I’m trying to experiment with malloc and free in assembly code (NASM, 64 bit).

  • 0

I’m trying to experiment with malloc and free in assembly code (NASM, 64 bit).

I have tried to malloc two arrays, each with space for 2 64 bit numbers. Now I would like to be able to write to their values (not sure if/how accessing them will work exactly) and then at the end of the whole program or in the case of an error at any point, free the memory.

What I have now works fine if there is one array but as soon as I add another, it fails on the first attempt to deallocate any memory 🙁

My code is currently the following:

extern printf, malloc, free


LINUX        equ     80H      ; interupt number for entering Linux kernel
EXIT         equ     60       ; Linux system call 1 i.e. exit ()

segment .text
    global      main

main:
    push dword 16       ; allocate 2 64 bit numbers
    call malloc
    add rsp, 4          ; Undo the push
    test  rax, rax      ; Check for malloc failure
    jz    malloc_fail
    mov r11, rax        ; Save base pointer for array

    ; DO SOME CODE/ACCESSES/OPERATIONS HERE

    push dword 16       ; allocate 2 64 bit numbers
    call malloc
    add rsp, 4          ; Undo the push
    test  rax, rax      ; Check for malloc failure
    jz    malloc_fail
    mov r12, rax        ; Save base pointer for array

    ; DO SOME CODE/ACCESSES/OPERATIONS HERE

malloc_fail:
    jmp dealloc

; Finish Up, deallocate memory and exit
dealloc:
    dealloc_1:
        test  r11, r11    ; Check that the memory was originally allocated
        jz    dealloc_2   ; If not, try the next block of memory
        push r11          ; push the address of the base of the array
        call free         ; Free this memory
        add rsp, 4
    dealloc_2:
        test  r12, r12
        jz    dealloc_end
        push r12
        call free
        add rsp, 4
dealloc_end:
    call os_return        ; Exit

os_return:
    mov  rax, EXIT
    mov  rdi, 0
    syscall
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:47:50+00:00Added an answer on May 27, 2026 at 12:47 pm

    I’m assuming the above code is calling the C functions malloc() and free()…

    If 1st malloc() fails, you arrive at dealloc_1 with whatever garbage is in r11 and r12 after returning from the malloc().

    If 2nd malloc() fails, you arrive at dealloc_1 with whatever garbage is in r12 after returning from the malloc().

    Therefore, you have to zero out r11 and r12 before doing the first allocation.

    Since this is 64-bit mode, all pointers/addresses and sizes are normally 64-bit. When you pass one of those to a function, it has to be 64-bit. So, push dword 16 isn’t quite right. It should be push qword 16 instead. Likewise, when you are removing these parameters from the stack, you have to remove exactly as many bytes as you’ve put there, so add rsp, 4 must change to add rsp, 8.

    Finally, I don’t know which registers malloc() and free() preserve and which they don’t. You may need to save and restore the so-called volatile registers (see your C compiler documentation). The same holds for the code not shown. It must preserve r11 and r12 so they can be used for deallocation. EDIT: And I’d check if it’s the right way of passing parameters through the stack (again, see your compiler documentation).

    EDIT: you’re testing r11 for 0 right before 2nd free(). It should be r12. But free() doesn’t really mind receiving NULL pointers. So, these checks can be removed.

    Pay attention to your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have just tried to save a simple *.rtf file with some websites and
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to loop through a bunch of documents I have to put
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.