Guest User

Untitled

a guest
Aug 15th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. x86 NASM Assembly - Problems with Input
  2. %include "system.inc"
  3.  
  4. section .data
  5. greet: db 'Hello!', 0Ah, 'Please enter a word or character:', 0Ah
  6. greetL: equ $-greet ;length of string
  7. inform: db 'I will now repeat this until you type it back to me.', 0Ah
  8. informL: equ $-inform
  9. finish: db 'Good bye!', 0Ah
  10. finishL: equ $-finish
  11. newline: db 0Ah
  12. newlineL: equ $-newline
  13.  
  14.  
  15. section .bss
  16.  
  17. input: resb 40 ;first input buffer
  18. check: resb 40 ;second input buffer
  19.  
  20. section .text
  21.  
  22. global _start
  23. _start:
  24.  
  25.  
  26. greeting:
  27. mov eax, 4
  28. mov ebx, 1
  29. mov ecx, greet
  30. mov edx, greetL %include "system.inc"
  31.  
  32. section .data
  33. greet: db 'Hello!', 0Ah, 'Please enter a word or character:', 0Ah
  34. greetL: equ $-greet ;length of string
  35. inform: db 'I will now repeat this until you type it back to me.', 0Ah
  36. informL: equ $-inform
  37. finish: db 'Good bye!', 0Ah
  38. finishL: equ $-finish
  39. newline: db 0Ah
  40. newlineL: db $-newline
  41.  
  42.  
  43. section .bss
  44.  
  45. input: resb 40 ;first input buffer
  46. check: resb 40 ;second input buffer
  47.  
  48. section .text
  49.  
  50. global _start
  51. _start:
  52.  
  53.  
  54. greeting:
  55. mov eax, 4
  56. mov ebx, 1
  57. mov ecx, greet
  58. mov edx, greetL
  59. sys.write
  60.  
  61. getword:
  62. mov eax, 3
  63. mov ebx, 0
  64. mov ecx, input
  65. mov edx, 40
  66. sys.read
  67.  
  68. sub eax, 1 ;remove the newline
  69. push eax ;store length for later
  70.  
  71. instruct:
  72. mov eax, 4
  73. mov ebx, 1
  74. mov ecx, inform
  75. mov edx, informL
  76. sys.write
  77.  
  78. pop edx ;pop length into edx
  79. mov ecx, edx ;copy into ecx
  80. push ecx ;store ecx again (needed multiple times)
  81.  
  82. mov eax, 4
  83. mov ebx, 1
  84. mov ecx, input
  85. sys.write
  86.  
  87. mov eax, 4 ;print newline
  88. mov ebx, 1
  89. mov ecx, newline
  90. mov edx, newlineL
  91. sys.write
  92.  
  93. mov eax, 3 ;get the user's word
  94. mov ebx, 0
  95. mov ecx, check
  96. mov edx, 40
  97. sys.read
  98.  
  99. xor eax, eax
  100.  
  101. checker:
  102. mov ebx, check
  103. mov ecx, input
  104. cmp ebx, ecx ;see if input was the same as before
  105.  
  106. jne loop ;if not the same go to input again
  107. je done ;else go to the end
  108. pop edx
  109. mov ecx, edx
  110. push ecx
  111. mov eax, 4
  112. mov ebx, 1
  113. mov ecx, check
  114. sys.write ;repeat the word
  115.  
  116. mov eax, 4
  117. mov ebx, 1
  118. mov ecx, newline
  119. mov edx, newlineL
  120. sys.write
  121.  
  122.  
  123.  
  124. loop:
  125. mov eax, 3 ;replace new input with old
  126. mov ebx, 0
  127. mov ecx, check
  128. mov edx, 40
  129. sys.read
  130.  
  131. jmp checker
  132.  
  133. done:
  134.  
  135. mov eax, 1
  136. mov ebx, 0
  137. sys.exit
  138. sys.write
  139.  
  140. getword:
  141. mov eax, 3
  142. mov ebx, 0
  143. mov ecx, input
  144. mov edx, 40
  145. sys.read
  146.  
  147. Hello!
  148. Please enter a word or character:
  149. Nick
  150. I will now repeat this until you type it back to me.
  151. Nick
  152. (I input) Magerko
  153. (I get) M
  154. (I input)Nick
  155. (I get)
  156. (I input)Nick
  157. (I get)
  158.  
  159. checker:
  160. mov ebx, check ; Moves the *address* of check into ebx
  161. mov ecx, input ; Similarly for input
  162. cmp ebx, ecx ; Checks if the addresses are the same (they never are)
  163.  
  164. mov eax, [input_len]
  165. cmp eax, [check_len]
  166. jne loop ; Strings of different length, do loop again
  167. mov ebx, check
  168. mov ecx, input
  169. .checkloop:
  170. mov dl, [ebx] ; Read a character from check
  171. cmp dl, [ecx] ; Equal to the character from input?
  172. jne loop ; Nope, jump to `loop`
  173. inc ebx ; Move ebx to point at next character in check
  174. inc ecx ; and ecx to next character in input
  175. dec eax ; one less character to check
  176. jnz .checkloop ; done?
  177.  
  178. ; the strings are equal if we reach this point in the code
  179. jmp done
  180.  
  181. jne loop ;if not the same go to input again
  182. je done ;else go to the end
  183. pop edx ; Will never be reached!
Add Comment
Please, Sign In to add comment