I'm trying to convert my C code to x86-64. My goal is to reverse a linked list. The two parameters that are passed in are the head ptr and the offset to to get the address of the pointer field (i.e. the pointer to the next node in the list).
From what I understand, the head ptr is passed in through the rdi register, and the offset is passed in through the rsi register. I keep getting a segmentation fault when it reaches the line "mov rcx, [rbx]." The segmentation fault goes away when it's just "mov rcx, rbx" and the line following is changed from "mov [rbx], rdx" to "mov rbx, rdx." However, I end up in an infinite loop because it keeps simply assigning the same values over and over again.
When I'm following along with my C code, all of the logic in the x86-64 makes sense to me, so I'm really at a standstill. Any ideas? This is my first time using x86-64.
.intel_syntax noprefix
.text
.global reverse_asm_64
reverse_asm_64:
push rbx
push r12
mov rax, 0x0
#headptr
mov rbx, rax
#nextptr
mov rcx, rax
#new_headptr
mov rdx, rax
#head
mov rax, [rdi]
#checks if head is null
cmp rax, 0
je null_ret
#move offset into a register
mov r12, rsi
add rax, r12
#add offset to rax to get the next ptr
mov rbx, rax
while_start:
#checks that next ptr isn't null
cmp rbx, 0x0
je while_done
#setting the next ptr
mov rcx, [rbx]
# *headptr = new_headptr
mov [rbx], rdx
#new_headptr = headptr
mov rdx, rbx
#sets headptr to nextptr
mov rbx, rcx
jmp while_start
while_done:
mov rax, rdx
sub rax, rsi
null_ret:
pop r12
pop rbx
ret
rdi, and the second inrsi. What is "offset"? A position into the linked list? Your asm is hard to read; indent your instructions more than the labels. Also, comments on the same line as the instructions make things more compact. - Peter Cordes