malloc and pointers in assembly -
I am trying to implement a linked list in the assembly, so to save me an 'array' (actually In 20 bytes) is the link for 5 possible lists Each 'link' is 5 bytes long (data for pointer 1 - x86 machine). Now I called malloc and created 1 link and first byte (decimal number) Pushed a number into and moved the link indicator in the beginning of the array. . After that I try to call Molok again to create a new link, but the new link created by the link has pushed me completely into the array and I do not know why !!! Here are codes and some input / output examples:
section. Rodata LC0: DB "Number is:% i", 10, 0; String LC1: DB "Memory allocation failed !!!", 10, 0; String section. Data section Bss number_stock: rsb 20 link: rsb link_size section. Text Exchange 16 Global Main External Printf Extant MLL Extension Link_Size EQU 5 _start: JMP Main Main:% MacroMemolok 2; Molok macro mf edx,% 1; Edx js fail_exit mov [% 2], eX% endmacha% macro myprint1; Print Macro Push% 1 Push LC0 Call Print Add Ed ESP, 8% Endmacha MyMoll LinkLimize, Link Mov Byte [link], 44 mov dword [numbers_stack], link mov eax, 0 mymalloc link_size, link; Allocate new links - * At this point the content is completely overwritten mov eax, [numbers_stack]; Get the indicator to list the head from the numbers [0] EX mode in ADX, 0 mL DL, byte [EX] MyPrint FX
Output example: It will always use the '40' number It will print its '44', if it removes the second mimolok, then it will print 44 as expected, please help! Why is it still pointing to the old memory place when I explicitly allocated a new one ?!
You are basically missing a level indirection. malloc
will return an indicator that you will store in link
(Thus, it should be 4 size, not link_size
). Then, when you do mov byte [link], 44
you are overwriting this indicator and not writing in the allocated memory area you must load the pointer in a register (but of course From malloc it has already been returned to eax
) and then dereference that, like:
mov eax, [link] Mov bite [eax], 44
You will need to adjust the pointer there and you do not need numbers_stack
at all, only the A in the list If you have a head
indicator you want to use common denominated names such as head
, next
and node
Easily understand what you are talking about.
PS: If you are using libc function, you should use the entry point main
and not _start
and all the required startup objects You should link so that Libc
is a chance to get started properly. You should end up with ret
to main
, or exit
function, but exit
syscall
section Rodata LC0: DB "Number is:% i", 10, 0; String LC1: DB "Memory allocation failed !!!", 10, 0; String section .bssrrcode node .nccdd1 Maan Ribeb 1 Andstrak List Hands: RDD5 section .text global main external printf external Molok extender becomes main:% Macro Mimlock 1; Molok macro push% 1; Call Malok Esp, 4 Exam eX, Ax JS Fell_Xit% andMacroco% Macro MyPrint1; Printing Macro Push% 1 Push LC0 Call Print Aid ASP, 8% Endmcro Mymolock Node_Size MOG [List_head], Axx MEGDWORD [EX + Node.next], 0moov byte [xx + node.value], 44mimolok node_sm mov [list_heads + 4], AXX meopied [Ex + node.ext], 0 mm byte [ed + node. Special], 11 mop x, [list_head]; Find the indicator in the header of the first list, movzx edx, byte [eax + node.value] myprintf edx mov eax, [list_heads + 4]; Get the pointer to the second list head movzx edx, byte [eax + node.value] myprintf edx
Hope this helps.
Comments
Post a Comment