linked list - Assembly set address to point to an address -
i'm trying build linked list in assembly (dont ask why), linked list has in stack built. i'm using nasm intel 80x86 on ubuntu linked list:
size_i: struc node data: resb 1 next: resb 4 endstruc size_of_struct: equ $-size_i
this stack:
create_stack: push stksz*4 call malloc add esp,4 ;correct push mov dword[my_stack],eax ret
now, read number user , each byte addressed different node in list. how try create new list:
create_new_list: push size_of_struct call malloc ;now eax has pointer location add esp,4 ;correct push mov edx,0 mov dl,byte[ebx] ;dl data of current number (current digit) mov byte[eax+data],dl ;set data mov dword[eax+next],0 ;set next null ;build first node in eax push edx push ecx push eax mov eax,0 mov ecx,dword[my_stack] mov dword[curr_stack_pointer],ecx mov ecx,0 mov cl,byte[counter] mov al,4 mul cl add dword[curr_stack_pointer],eax ;dword[curr_stack_pointer] has pointer head of list pop eax mov ecx,dword[curr_stack_pointer] mov [ecx],eax ;this problematic line. here trying ;mov address inside eax location of ecx ;this way have in current array block address ;of head of list. not working, ;segmentation error pop ecx pop edx ret
how can this? thanks!
i suggest make sure malloc
succeeds (i.e. check whether returned non-null pointer).
also, these 2 lines:
push stksz*4 push size_of_struct
would better write as:
push strict dword stksz*4 push strict dword size_of_struct
to ensure pushes dword , not byte (which mean pushing word).
your multiplication 4 bit over-complicated. written as:
mov eax,dword [my_stack] movzx ecx,byte [counter] lea ecx,[eax+ecx*4]
and make sure counter
within valid range (0 <= counter < stksz
) don't try read/write outside boundaries of "stack".
Comments
Post a Comment