Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. .intel_syntax noprefix
  2.  
  3. .global _start
  4. .text
  5.  
  6. _start:
  7. mov eax, 45 # brk
  8. mov ebx, 0
  9. int 0x80
  10. mov [buffer], eax # find start pos
  11.  
  12. mov eax, 45
  13. mov ebx, capacity # allocate memory in heap
  14. add ebx, [buffer]
  15. int 0x80
  16. mov edi, 0 # cur_pos
  17.  
  18. read: #read in chunks, look for newlines after
  19. mov eax, 3
  20. mov ecx, [buffer]
  21. add ecx, edi # start + cur_pos
  22. mov ebx, 0
  23. mov edx, 8192 # read 4096 bytes
  24. int 0x80
  25. cmp eax, 0
  26. je start_write # end of read
  27. add edi, eax
  28. cmp edi, capacity
  29. jne read
  30. mov eax, 45
  31. mov ebx, capacity
  32. add ebx, capacity
  33. mov capacity, ebx # double the capacity
  34. add ebx, [buffer]
  35. int 0x80
  36. jmp read
  37.  
  38. start_write:
  39. cmp edi, 0
  40. jz end
  41. sub edi, 1
  42. mov eax, edi
  43. add eax, [buffer]
  44. mov eax, [eax]
  45. cmpb al, endline_character # \n
  46. je write
  47. add edi, 1
  48.  
  49. write:
  50. cmp edi, 0
  51. jle end
  52. mov esi, edi
  53.  
  54. inner_loop:
  55. dec esi
  56. cmp esi, 0
  57. je print_str
  58. mov eax, [buffer]
  59. add eax, esi # check start[esi]
  60. mov eax, [eax]
  61. cmpb al, endline_character
  62. jne inner_loop
  63. add esi, 1
  64. jmp print_str # go until \n found
  65.  
  66. print_str:
  67. mov al, endline_character
  68. mov ebx, 1
  69. mov ecx, [buffer]
  70. add ecx, esi
  71. mov edx, edi
  72. sub edx, esi
  73. movb [ecx + edx], al # add \n at the end
  74. mov eax, 4
  75. inc edx
  76. mov edi, esi
  77. dec edi # edi points to \n every time in write_loop
  78. int 0x80
  79. jmp write
  80.  
  81. end:
  82. mov eax, 1
  83. mov ebx, 0
  84. int 0x80
  85.  
  86.  
  87. .data
  88.  
  89. buffer:
  90. .long 0
  91.  
  92. capacity:
  93. .long 8192
  94.  
  95. endline_character:
  96. .byte 10
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement