Loading

Paste #p6i3wccev

  1. ;    ___                     __   __       _____              __               __  _             __ ___ __
  2. ;   / _ | ___ ___ ___ __ _  / /  / /_ __  / ___/__  ___  ___ / /_______ ______/ /_(_)__  ___    / //_(_) /_
  3. ;  / __ |(_-<(_-</ -_)  ' \/ _ \/ / // / / /__/ _ \/ _ \(_-</ __/ __/ // / __/ __/ / _ \/ _ \  / ,< / / __/
  4. ; /_/ |_/___/___/\__/_/_/_/_.__/_/\_, /  \___/\___/_//_/___/\__/_/  \_,_/\__/\__/_/\___/_//_/ /_/|_/_/\__/
  5. ;                                /___/
  6. ; ----------------------------------------------------------------------------------------------------------
  7. ; NullForth/64 Subproject: x64 Assembly Construction Kit
  8. ; ----------------------------------------------------------------------------------------------------------
  9. ; file:     unix-filter.asm
  10. ; version:  1.0
  11. ; license:  GPL3
  12. ; purpose:  kanonic skeleton of a unix filter
  13. ;       this example filter performs output <- id (input) , like posix cat
  14. ; ----------------------------------------------------------------------------------------------------------
  15.  
  16.     bits 64
  17.  
  18. %include "linux-syscall.macro"
  19. %include "structflow.inc"
  20. %include "stringdata.macro"
  21. %include "terminal-control.macro"
  22.  
  23. %macro  PRINT   1-*
  24. %rep %0
  25.     LINUX   write,Linux.stdout,%1,%1.length
  26. %rotate 1
  27. %endrep
  28. %endmacro
  29.  
  30. %macro  FSYNC   0
  31.     LINUX   fsync,Linux.stdout
  32. %endmacro
  33.  
  34. %macro  PUTCHAR 1
  35.     LINUX   write,Linux.stdin,%1,1
  36. %endmacro
  37.  
  38.     section .text
  39.  
  40.     global  _start
  41.  
  42. _start:
  43.     LINUX.ABI64.ProcessEnter
  44.  
  45. ; handle some arguments
  46.     xor rax,rax
  47.     mov [argc_counter],rax
  48.     FORSTEP [argc_counter],[Process.argc],1 ; in FORSTEP, rax register is loop index
  49.       mov   rbx,[Process.argv]
  50.       lea   rbx,[rbx+rax*8]
  51.       mov   rbx,[rbx]
  52.       ; compare here
  53.     ENDFOR
  54.  
  55. ; cycle through env and pick what you want
  56.     mov rax,[Process.envp]
  57.     mov [envpptr],rax   ; initially, we get pointer to environment pointers array
  58.     FORALL  [envpptr]   ; in FORALL, rax register is pointing to array item
  59.     mov rbx,rax     ; array items are pointers to C strings
  60.       ; compare here   
  61.     ENDFOR
  62.  
  63.     ; ...Skeleton: insert your initialization code just here
  64.  
  65.     ; we currently use buffered input on stdin. that's quite usable for input pipe redirection
  66.  
  67. FilterLoop:
  68. .Input:
  69.     LINUX   read,Linux.stdin,Input.buffer,4096
  70.     ; currently, we have no signal handlers.
  71.     LINUX.ONINTERRUPT Interrupted
  72.     LINUX.ONERROR Exit
  73.     mov [Input.charcount],rax   ; save the length of received input
  74.     or  rax,rax         ; well, we didn't received anything. so let's finish. it's eof or broken pipe
  75.     jz  Finish
  76.  
  77. .Output:
  78.     LINUX   write,Linux.stdout,Input.buffer,[Input.charcount]
  79.     FSYNC
  80.  
  81.     jmp FilterLoop
  82.  
  83. Interrupted:
  84.     ; PRINT MsgInterrupted
  85.  
  86. Finish:
  87.     ; ... Skeleton: insert your termination code just here
  88.  
  89. Exit:
  90.     LINUX.ABI64.ProcessExit 0
  91.  
  92.     section .data
  93.  
  94. ; arguments index and environment pointer
  95. argc_counter:   dq  0
  96. envpptr:    dq  0
  97.  
  98. ; messages 
  99.     ; SString MsgInterrupted," ...Interrupted.",ASCII_LF
  100.  
  101. section .bss
  102.     align   8
  103.  
  104. ; input buffers
  105. Input:
  106. .charcount  resq    1
  107. .buffer     resq    4096/8
  108.  
  109. ; end unix-filter.asm