问题描述
出于学习目的,我试图创建一个玩具装配程序,该程序在一个单独的线程中运行一个函数。我的机器具有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 (将#修改为@)