Skip to content

Instantly share code, notes, and snippets.

@Archenoth
Last active January 30, 2019 21:44
Show Gist options
  • Select an option

  • Save Archenoth/5380671 to your computer and use it in GitHub Desktop.

Select an option

Save Archenoth/5380671 to your computer and use it in GitHub Desktop.
This will test to see if a file exists and then if it does, exit, or if not, to create it and write some text to it...The Linux Syscall reference used was this: http://web.archive.org/web/20120312200215/http://www.acsu.buffalo.edu/~charngda/linux_syscalls_64bit.html (The first number after each call is the system call number, the next is the add…
;; Write to and create file
global _start
section .text
_start:
;; sys_open(file, permissions)
mov rax, 2 ; sys_open
mov rdi, file ; file
mov rsi, 2 ; Read/Write permissions
syscall
;; File exists?
mov rdx,0
cmp rdx,rax
jle WR
;; Create file
mov rax,85 ; sys_creat
syscall
;; File created sucessfully?
mov rdx,0
cmp rax,rdx
jle EX
WR:
;; File descriptor copy
mov rbx,rax
;; sys_write(stream, message, length)
mov rax, 1 ; sys_write
mov rdi, rbx ; ./nope
mov rsi, message ; message address
mov rdx, length ; message string length
syscall
mov rax, 3
syscall
EX:
;; sys_exit(return_code)
mov rax, 60 ; sys_exit
mov rdi, 0 ; return 0 (success)
syscall
section .data
message: db 'Hello, world!',0x0A ; message and newline
file: db './herpderp' ; File to write
length: equ $-message ; NASM definition pseudo-instruction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment