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.
8 replies to this topic
#1
Posted 13 January 2012 - 10:35 PM
|
|
|
#2
Posted 14 January 2012 - 01:13 AM
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:
64-bit code for GCC is a tad different; arguments are passed in registers first:
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
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
Posted 14 January 2012 - 02:10 AM
Thanks so much, I forgot to mention that I was using a 64 bit OS.
#4
Posted 14 January 2012 - 01:26 PM
#5
Posted 14 January 2012 - 10:40 PM
yes it works.
#6
Posted 16 January 2012 - 01:29 AM
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
Posted 16 January 2012 - 01:30 AM
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.
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
Posted 16 January 2012 - 01:34 AM
thanks...it is perfect
#9
Posted 16 January 2012 - 01:39 AM
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









