Skip to content

Instantly share code, notes, and snippets.

@ammarfaizi2
Last active August 9, 2022 15:25
Show Gist options
  • Save ammarfaizi2/4bd78a85bb3a0917120e8fd620a82bc8 to your computer and use it in GitHub Desktop.
Save ammarfaizi2/4bd78a85bb3a0917120e8fd620a82bc8 to your computer and use it in GitHub Desktop.
.extern pthread_create
.extern pthread_join
.extern printf
.extern sleep
.section .rodata
thread_str:
.string "this is thread %d\n"
.section .text
.global main
main:
pushq %rbp
pushq %rbx
pushq %r12
movq %rsp, %rbp
subq $(8 * 30), %rsp
movl $30, %r12d
xorl %ebx, %ebx
.Lmain_ulang:
movq %rbx, %rdi
leaq (%rsp, %rbx, 8), %rsi
callq create_thread
incq %rbx
cmpq %r12, %rbx
jl .Lmain_ulang
xorl %ebx, %ebx
.Lmain_ulang2:
leaq (%rsp, %rbx, 8), %rdi
callq wait_thread
incq %rbx
cmpq %r12, %rbx
jl .Lmain_ulang2
movq %rbp, %rsp
popq %r12
popq %rbx
popq %rbp
retq
create_thread:
pushq %rbp
movq %rdi, %rcx
movq %rsi, %rdi
xorl %esi, %esi
leaq function(%rip), %rdx
callq pthread_create
popq %rbp
retq
wait_thread:
pushq %rbp
movq (%rdi), %rdi
xorl %esi, %esi
callq pthread_join
popq %rbp
retq
function:
pushq %rbp
movq %rdi, %rbp
movl $2, %edi
callq sleep
movq %rbp, %rsi
leaq thread_str(%rip), %rdi
callq printf
popq %rbp
retq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment