linux - 在汇编代码中调用 pthread_create 时应该为寄存器设置什么值?
问题描述
出于学习目的,我正在尝试创建一个玩具汇编程序,该程序在单独的线程中运行一个函数。我的机器采用 x86-64 架构并运行 Ubuntu 操作系统。我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
和pthread_join
论据
mov eax, 0
mov ecx, 0
mov rdi, thread0_id
现在代码抛出一个bus error
. 难道是pthread_t
没有权限打印消息?
解决方案
推荐阅读
- r - 如何限制solve() 的CPU 使用率?
- docker - 如何使用 ECS 和 docker-compose 使用本地内容填充卷
- google-maps - Google Maps Matrix API 返回状态:ZERO_RESULTS 英国邮政编码
- python - 为什么我的hovertool,从散景显示不正确的结果?
- django - ProjectDetailsComponent.html:18 错误类型错误:无法读取未定义的属性“id”
- json - Elixir - JasonHelpers - 如何将关键字列表发送到 json_map?
- java - 如何在java中使用bufferReader获取json对象中的特定元素
- javascript - 在javascript中将多条数据添加到单行
- graph-databases - Gremlin - 在单次遍历中移动多个边
- cron - 如何创建每 3 个月执行一次的 cron 作业?