Last active
August 29, 2015 14:15
-
-
Save DogParty/8fb4094367baa1aebad5 to your computer and use it in GitHub Desktop.
A self-replicating 64bit assembly program for OS X. Save as g and run with "as g -o g.o; ld g.o -e _m; ./a.out"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.section __DATA,__data # OSX specific form of ".section data" | |
SOURCE: # .incbin copies files into applications byte-for-byte | |
.incbin "g" # so this is loading the source's bytes into a var called SOURCE | |
SIZE = . - SOURCE # . here points to the byte after SOURCE in memory | |
# so . - SOURCE returns the length of SOURCE in mem | |
.section __TEXT,__text | |
.globl _main # entry point. also OSX needs _ before the function | |
_main: | |
movl $0x2000004, %eax # x64 needs to pad syscalls w 0x2000000 so 4 is sys_write | |
movl $1, %edi # with sys_write in eax, moving 1 into edi says write to console | |
movq SOURCE@GOTPCREL(%rip),%rsi # str's in the Global Offset Table (GOT) and we access thru %rip | |
movq $SIZE, %rdx # move the length to read into %rdx (the next sequential register) | |
syscall # execute eax with edi as return code. (sys_write to console) | |
movl $0, %ebx # set return code to 0 | |
movl $0x2000001, %eax # sys_exit | |
syscall # execute exit with a success return code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment