Skip to content

Instantly share code, notes, and snippets.

@mouseroot
Last active March 9, 2020 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mouseroot/21491779d723ad946bf791d824e9876a to your computer and use it in GitHub Desktop.
Save mouseroot/21491779d723ad946bf791d824e9876a to your computer and use it in GitHub Desktop.
Build and Assemble a program to print a message, using python (self contained script)
import subprocess
asm_code = """
.global _start
.text
_start:
mov $1,%rax
mov $1,%rdi
mov $msg, %rsi
mov $?size?, %rdx
syscall
mov $60, %rax
xor %rdi,%rdi
syscall
msg:
.ascii "?message?"
"""
with open("_main.s","w") as _code:
_code.write(asm_code)
line = raw_input("Message:")
sz = len(line)
with open("_main.s","r") as f:
data = f.read()
result = data.replace("?size?",str(sz))
result = result.replace("?message?",line.replace("\n",''))
with open("patch_main.s","w") as _out:
_out.write(result)
subprocess.call(["gcc","-c","patch_main.s","-o","patch.o"])
subprocess.call(["ld","patch.o","-o","app"])
print("Complete")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment