Skip to content

Instantly share code, notes, and snippets.

@edma2
Created January 24, 2012 11:06
Show Gist options
  • Save edma2/1669652 to your computer and use it in GitHub Desktop.
Save edma2/1669652 to your computer and use it in GitHub Desktop.
; tee
; Returns non-zero exit code if an error occured
section .bss
buf: resb 1024
buflen equ $-buf
stdin equ 0
stdout equ 1
O_WRONLY equ 1q
O_CREAT equ 100q
section .text
global _start
_start:
; Throw away argc and argv[0]
pop eax
pop eax
; sys_open
mov eax, 5
pop ebx
mov ecx, (O_WRONLY|O_CREAT)
mov edx, 666q
int 0x80
cmp eax, 0
jz exit
mov edi, eax
read_loop:
; sys_read
mov eax, 3
mov ebx, stdin
mov ecx, buf
mov edx, buflen
int 0x80
; EOF if bytes read == 0
cmp eax, 0
; Bail out if <= 0
jle cleanup
; sys_write
; edx = bytes to write == bytes read
; ecx already contains buf
mov edx, eax
mov eax, 4
mov ebx, edi
int 0x80
; Check number of bytes written
cmp eax, edx
jne cleanup
; Do the same for stdout
mov eax, 4
mov ebx, stdout
int 0x80
cmp eax, edx
je read_loop
cleanup:
; Save exit code
push eax
; sys_close
mov eax, 6
mov ebx, edi
int 0x80
; Restore exit code
pop eax
exit:
mov ebx, eax
mov eax, 1
int 0x80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment