Jump to content

memory allocation

- - - - -

  • Please log in to reply
8 replies to this topic

#1
untitled_1

untitled_1

    Learning Programmer

  • Members
  • PipPipPip
  • 89 posts
hi all

does anyone know how i can allocate memory with assembly. I have a small program that processes some data and then stores it in a buffer, i have reserved 10000 bytes but
i am worried this is not enough and the program might need more, so i want the program to request more memory as it needs it.

i took a look at the man pages for the malloc function but i cant seem to find the function code to use with int 0x80. thanks

oh i am using linux.

#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Link with the standard C library.

I know you're using AT&T syntax, but I know NASM best. You'll be able to figure it out pretty easily.

32-bit code:
extern  malloc
extern  free

section .text
; blah ...

main:
    push    ebp             ; save base pointer
    mov     ebp, esp        ; set base pointer for arguments
    sub     esp, 4          ; reserve space for one argument to malloc
    
    mov     DWORD [esp], 10000
    call    malloc
    
    ; pointer to allocated memory is in EAX.
    
    mov     [esp], eax
    call    free
    

64-bit code for GCC is a tad different; arguments are passed in registers first:
mov     rdi, 10000
call    malloc
; pointer to allocated memory is in RAX.


mov     rdi, rax
call    free

You can also use realloc() like this as well.

To compile and link:
nasm -f elf -o myprog.o myprog.asm
gcc -o memprog myprog.o

sudo rm -rf /

#3
untitled_1

untitled_1

    Learning Programmer

  • Members
  • PipPipPip
  • 89 posts
Thanks so much, I forgot to mention that I was using a 64 bit OS.

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
No problem. Is it working?
sudo rm -rf /

#5
untitled_1

untitled_1

    Learning Programmer

  • Members
  • PipPipPip
  • 89 posts
yes it works.

#6
untitled_1

untitled_1

    Learning Programmer

  • Members
  • PipPipPip
  • 89 posts
one more thing, is there anyway i can get a detailed list of functions for int 0x80? I know i can use the man pages and i know which registers the parameters are placed in for 64 bit cpu, my problem is that i dont know the appropriate function code to use for some of the function.

#7
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
System Call Table

By the way, there's a thread called Assembly Languages Resources on the Assembly forum here. I just added that link to it.
sudo rm -rf /

#8
untitled_1

untitled_1

    Learning Programmer

  • Members
  • PipPipPip
  • 89 posts
thanks...it is perfect

#9
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
No problem. :)
sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users