Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. ;
  2. ; IOCLA, Buffer management
  3. ;
  4. ; Fill buffer with data from standard input.
  5. ; Buffer is stored on the stack.
  6. ;
  7.  
  8. extern printf
  9. extern puts
  10. extern strlen
  11. extern fgets
  12. extern stdin
  13.  
  14. section .data
  15. read_message: db "insert buffer string: ", 0
  16. buffer_intro_message: db "buffer is:", 0
  17. byte_format: db " %02X(%c)", 0
  18. null_string: db 0
  19. var_message_and_format: db "var is 0x%08X", 13, 10, 0
  20.  
  21. section .text
  22.  
  23. global main
  24.  
  25. main:
  26. push ebp
  27. mov ebp, esp
  28.  
  29. ; Make room for local variable (32 bit, 4 bytes).
  30. ; Variable address is at ebp-4.
  31. sub esp, 4
  32.  
  33. ; Make room for buffer (64 bytes).
  34. ; Buffer address is at ebp-68.
  35. sub esp, 64
  36.  
  37. ; Initialize local variable.
  38. mov dword [ebp-4], 0xCAFEBABE
  39.  
  40. ; Read buffer from standard input.
  41. push read_message
  42. call printf
  43. add esp, 4
  44.  
  45. lea ebx, [ebp-68]
  46. push DWORD [stdin]
  47. push DWORD 65
  48. push DWORD ebx
  49. call fgets
  50. add esp, 12
  51.  
  52. ; Push string length on the stack.
  53. ; String length is stored at ebp-72.
  54. push ebx
  55. call strlen
  56. add esp, 4
  57. push eax
  58.  
  59. ; Print data in buffer.
  60. push buffer_intro_message
  61. call printf
  62. add esp, 4
  63.  
  64. xor ecx, ecx
  65. print_byte:
  66. xor eax, eax
  67. lea ebx, [ebp-68]
  68. mov al, byte[ebx+ecx]
  69. push ecx ; save ecx
  70.  
  71. ; Print current byte.
  72. push eax
  73. push eax
  74. push byte_format
  75. call printf
  76. add esp, 12
  77.  
  78. pop ecx ; restore ecx
  79. inc ecx
  80. cmp ecx, [ebp-72]
  81. jl print_byte
  82.  
  83. push null_string
  84. call puts
  85. add esp, 4
  86.  
  87. ; Print local variable.
  88. mov eax, [ebp-4]
  89. push eax
  90. push var_message_and_format
  91. call printf
  92. add esp, 8
  93.  
  94. leave
  95. ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement