You are basically missing one level of indirection. malloc
will return a pointer that you store in link
(thus it's size should be 4, not link_size
). Then, when you do mov byte[link], 44
you are overwriting this pointer and not writing into the allocated memory region. You need to load the pointer into a register (but of course malloc already returned it in eax
) and then dereference that, such as:
mov eax, [link]
mov byte [eax], 44
You will also need to adjust the pointer there and you don't need the numbers_stack
at all, lists just have a head
pointer. You might want to use the common established names such as head
, next
and node
so that others can more easily understand what you are talking about.
PS: if you are using libc functions you should use entry point main
and not _start
and link with all the necessary startup objects so that libc
has a chance to initialize properly. You should finally just ret
from main
, or use the exit
function but not the exit
syscall.
Update: here is a possible implementation that allocates 2 lists and prints the first value from each:
section .rodata
LC0:
DB "The number is: %i", 10, 0 ; string
LC1:
DB "Memory allocation failed!!!", 10, 0 ; string
section .bss
struc node
.next resd 1
.value resb 1
endstruc
list_heads:
RESD 5
section .text
global main
extern printf
extern malloc
extern gets
main:
%macro mymalloc 1 ;malloc macro
push %1 ; size to allocate
call malloc
add esp,4
test eax,eax
jz fail_exit
%endmacro
%macro myprintf 1 ;printing macro
push %1
push LC0
call printf
add esp,8
%endmacro
mymalloc node_size
mov [list_heads], eax
mov dword [eax + node.next], 0
mov byte [eax + node.value], 44
mymalloc node_size
mov [list_heads + 4], eax
mov dword [eax + node.next], 0
mov byte [eax + node.value], 11
mov eax, [list_heads] ; get pointer to first list head
movzx edx, byte [eax + node.value]
myprintf edx
mov eax, [list_heads + 4] ; get pointer to second list head
movzx edx, byte [eax + node.value]
myprintf edx
Hope this helps.