如何解决在汇编代码中调用pthread_create时应将哪些值设置为寄存器?
出于学习目的,我试图创建一个玩具装配程序,该程序在一个单独的线程中运行一个函数。我的机器具有x86-64架构,并运行Ubuntu OS。为此,我使用了pthread
库:
extern pthread_create
extern pthread_join
section .data
; a separate thread will print the "Hello" message
text: db "Hello",10
text_len: equ $-text
thread_id: dq 0
section .text
global main
main:
mov eax,0
mov ecx,0
mov edx,thread ; the function address to be executed in a thread
; With the "mov esi,0" instruction,the call to pthread_create
; throws a segfault. Without this instruction,the call to
; pthread_create returns,but produces no observable effect.
mov esi,0
mov rdi,thread_id
call pthread_create
mov eax,0
mov rsi,0
mov rdi,[thread_id]
; this call should block until the thread terminates,shouldn't it?
call pthread_join
ret
thread:
; printing "Hello" to the screen
mov eax,4
mov ebx,1
mov ecx,text
mov edx,text_len
int 80h
ret
我使用以下命令编译可执行文件a.out
:
nasm -f elf64 code.s
gcc -no-pie code.o -lpthread
可执行文件无法正常运行,并出现代码中指示的错误。
我怀疑失败的原因是调用pthread_create
之前寄存器参数设置错误。
如何正确设置参数?
更新:
我将pthread_create
自变量更改为
mov eax,0
mov ecx,0
mov edx,thread_0
mov rdi,thread0_id
和{p的pthread_join
参数
mov eax,0
mov rdi,thread0_id
现在,代码将引发bus error
。可能是pthread_t
没有打印消息的权限吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。