macos - 为什么 x86 中的这个与链表相关的程序会出现段错误?
问题描述
我想为链表分配一些节点。我有一个alloc_pair
似乎可以工作的功能。我包括注释来解释每一行关于链接列表的意图。我的代码在某处给了我一个分段错误,但我不知道在哪里。GDB 没有帮助,如下所示:
Thread 2 hit Breakpoint 1, 0x0000000100003f63 in main ()
(gdb) c
Continuing.
Thread 2 hit Breakpoint 2, 0x0000000100003f4e in alloc_pair ()
(gdb) ni
0x0000000100003f55 in alloc_pair ()
(gdb) ni
0x0000000100003f59 in alloc_pair ()
(gdb) disassemble
Dump of assembler code for function alloc_pair:
0x0000000100003f4e <+0>: mov rdi,0x10
0x0000000100003f55 <+7>: sub rsp,0x8
=> 0x0000000100003f59 <+11>: call 0x100003f96
0x0000000100003f5e <+16>: add rsp,0x8
0x0000000100003f62 <+20>: ret
End of assembler dump.
(gdb) c
Continuing.
Thread 2 received signal SIGSEGV, Segmentation fault.
0x00007fff731d970a in ?? ()
(gdb) bt
#0 0x00007fff731d970a in ?? ()
#1 0x00007ffeefbff828 in ?? ()
#2 0x0000000100008008 in ?? ()
#3 0x0000000000000000 in ?? ()
(gdb)
如果你知道我犯的错误,请告诉我。
.global _main
.text
alloc_pair:
push rbp
mov rbp, rsp
mov rdi, 16
sub rsp, 8
call _malloc
add rsp, 8
mov rsp, rbp
pop rbp
ret
_main:
call alloc_pair
mov r13, rax # r13 stores the initial pair allocated
mov qword ptr [rax], 29 # the node 1 head contains 29
mov r12, [rax + 8] # r12 stores the memory location of the node 1 tail
call alloc_pair
mov qword ptr [rax], 7 # the node 2 head contains 7
mov qword ptr [r12], rax # the node 1 tail points to the node 2 head
mov rdi, 0
mov rax, 0x2000001
syscall
解决方案
这一行:
mov r12, [rax + 8] # r12 stores the memory location of the node 1 tail
没有按照您的评论所说的那样做。该指令将内存的 64 位内容移动[rax+8]
到R12。它不会将地址移动[rax+8]
到R12。您想要的是加载有效地址(LEA)[rax+8]
以获取R12的地址。该指令如下所示:
lea r12, [rax + 8] # r12 stores the memory location of the node 1 tail
推荐阅读
- android - 在本机反应中发布后崩溃的应用程序?
- flutter - 如何限制 TextSpan 小部件的文本长度
- javascript - 单击“Enter”按钮时提交表单,并进行验证
- flutter - 如何在不改变屏幕/页面的情况下传递数据?
- kubernetes - 是否可以在容器内的 env 中将 application.properties 的内容作为键/值对公开?
- javascript - 使用 node.js 处理来自 aws s3 getobject 的数百万条记录并通过分页将记录返回到前端的最佳方法
- python - Django 连接错误(Oracle 11g 数据库)
- vba - 使用手动干预将单词表转换为文本
- angular - 由于我们在 d3 v3 中具有标注功能,有没有办法在 d3 v5 中自动放置标签而不重叠?
- angular - 拉入模型 ID 以保存其他模型的形式