fork(1) download
  1. global _start
  2.  
  3. section .data
  4. sys_read equ 3
  5. sys_write equ 4
  6. stdin equ 0
  7. stdout equ 1
  8. bytesRead dd 0
  9. termios: times 36 db 0
  10. ICANON: equ 1<<1
  11. ECHO: equ 1<<3
  12.  
  13. section .bss
  14. line resb 11
  15. index resb 4
  16.  
  17. section .text
  18.  
  19. _start:
  20. push ebp
  21. mov ebp, esp
  22.  
  23. call canonical_off
  24. call echo_off
  25.  
  26. call _readLine
  27. call _printLine
  28.  
  29. call canonical_on
  30. call echo_on
  31.  
  32. mov esp, ebp
  33. pop ebp
  34. jmp exit
  35.  
  36. _readLine:
  37. ; Reads into line until new line (0xA)
  38. ; Number of bytes read will be stored in bytesRead when _readLine returns
  39. mov eax, sys_read ; syscall to read
  40. mov ebx, stdin ; stdin
  41. mov edx, [index] ; put index into edx
  42. mov ecx, dword line ; put line addr in ecx
  43. add ecx, edx ; add index to addr in ecx
  44. mov edx, 1 ; read one char
  45. int 0x80 ; call kernel to read char
  46. mov ecx, [index] ; put index into ecx
  47. cmp dword [line + ecx], 0xA ; compare value at line + ecx to new line char
  48. inc byte [index] ; increment index
  49. jne _readLine ; if last char is not new line, loop
  50. ret
  51.  
  52. _printLine:
  53. mov eax, sys_write
  54. mov ebx, stdout
  55. mov ecx, line
  56. mov edx, [index]
  57. int 0x80
  58. ret
  59.  
  60. _printIndex:
  61. mov eax, sys_write
  62. mov ebx, stdout
  63. add byte [index], '0'
  64. mov ecx, index
  65. mov edx, 4
  66. int 0x80
  67. ret
  68.  
  69. canonical_off:
  70. call read_stdin_termios
  71.  
  72. ; clear canonical bit in local mode flags
  73. ;push rax
  74. mov eax, ICANON
  75. not eax
  76. and [termios+12], eax
  77. ;pop rax
  78.  
  79. call write_stdin_termios
  80. ret
  81.  
  82. echo_off:
  83. call read_stdin_termios
  84.  
  85. ; clear echo bit in local mode flags
  86. ;push rax
  87. mov eax, ECHO
  88. not eax
  89. and [termios+12], eax
  90. ;pop rax
  91.  
  92. call write_stdin_termios
  93. ret
  94.  
  95. canonical_on:
  96. call read_stdin_termios
  97.  
  98. ; set canonical bit in local mode flags
  99. or dword [termios+12], ICANON
  100.  
  101. call write_stdin_termios
  102. ret
  103.  
  104. echo_on:
  105. call read_stdin_termios
  106.  
  107. ; set echo bit in local mode flags
  108. or dword [termios+12], ECHO
  109.  
  110. call write_stdin_termios
  111. ret
  112.  
  113. read_stdin_termios:
  114. ; push rax
  115. ; push rbx
  116. ; push rcx
  117. ;push rdx
  118.  
  119. mov eax, 36h
  120. mov ebx, stdin
  121. mov ecx, 5401h
  122. mov edx, termios
  123. int 80h
  124.  
  125. ;pop rdx
  126. ; pop rcx
  127. ;pop rbx
  128. ;pop rax
  129. ret
  130.  
  131. write_stdin_termios:
  132. ; push rax
  133. ;push rbx
  134. ;push rcx
  135. ; push rdx
  136.  
  137. mov eax, 36h
  138. mov ebx, stdin
  139. mov ecx, 5402h
  140. mov edx, termios
  141. int 80h
  142.  
  143. ;pop rdx
  144. ;pop rcx
  145. ; pop rbx
  146. ;pop rax
  147. ret
  148.  
  149. exit:
  150. mov eax, 01h ; exit()
  151. xor ebx, ebx ; errno
  152. int 80h
Success #stdin #stdout 0s 144KB
stdin
abcd
stdout
Standard output is empty