Skip to content

Instantly share code, notes, and snippets.

@larzconwell
Last active December 31, 2015 13:59
Show Gist options
  • Save larzconwell/7997198 to your computer and use it in GitHub Desktop.
Save larzconwell/7997198 to your computer and use it in GitHub Desktop.
; Write contents from stdin to a given file
section .rodata
errnofile: db "No filename given", 10
errnofilelen: equ $ - errnofile
errnocreate: db "Couldn't create file", 10
errnocreatelen: equ $ - errnocreate
errnowrite: db "Couldn't write file", 10
errnowritelen: equ $ - errnowrite
section .bss
input: resb 1024
inputlen: equ $ - input
section .text
global _start
; simple exit, ebx contains exit code.
exit:
mov eax, 1
int 80h
; no file name given, print error exit with 1.
nofile:
mov eax, 4
mov ebx, 2
mov ecx, errnofile
mov edx, errnofilelen
int 80h
mov ebx, 1
jmp exit
; couldn't create, print error exit with 1.
nocreate:
mov eax, 4
mov ebx, 2
mov ecx, errnocreate
mov edx, errnocreatelen
int 80h
mov ebx, 1
jmp exit
; couldn't write, print error exit with 1.
nowrite:
mov eax, 4
mov ebx, 2
mov ecx, errnowrite
mov edx, errnowritelen
int 80h
mov ebx, 1
jmp exit
_start:
pop ebx
cmp ebx, 1
je nofile
pop ebx
pop ebx
; create file
mov eax, 8
mov ecx, 0q600
int 80h
test eax, eax
js nocreate
push eax
; read stdin
mov eax, 3
mov ebx, 0
mov ecx, input
mov edx, inputlen
int 80h
; write stdin to file
mov eax, 4
pop ebx
mov ecx, input
mov edx, inputlen
int 80h
test eax, eax
js nowrite
; close file
mov eax, 6
int 80h
mov ebx, 0
jmp exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment