Skip to content

Instantly share code, notes, and snippets.

@derekthecool
Created November 29, 2024 04:50
Show Gist options
  • Save derekthecool/a24dcd30a70b413d8657c488c8aefb22 to your computer and use it in GitHub Desktop.
Save derekthecool/a24dcd30a70b413d8657c488c8aefb22 to your computer and use it in GitHub Desktop.
Clash of code assembly
#!/bin/bash
# Use a here document to write assembly code into clash.s
cat <<EOF > clash.s
.global _start
.section .data
msg:
.asciz "Hello, World!\n"
.section .text
_start:
mov \$1, %rax # sys_write (1)
mov \$1, %rdi # File descriptor 1 (stdout)
lea msg(%rip), %rsi # Load address of msg into %rsi
mov \$14, %rdx # Length of the message (14 bytes including newline)
syscall # Invoke syscall
mov \$60, %rax # sys_exit (60)
xor %rdi, %rdi # Return code 0
syscall # Invoke syscall to exit
EOF
# Assemble the code into object file
as --64 clash.s -o clash.o
# Link the object file to create the executable
ld clash.o -o program
# Run the program
./program
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment