Advertisement
xFazz

proj3-1, v2, infinite loop when iterating through chars

Jul 8th, 2023
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. ; Asks user for number between -25 and 25
  2. ; Asks the user for a string, must be longer than 8 characters long
  3. ; Shifts string
  4.  
  5. section .data
  6. promptShiftValue db "Enter a shift value between -25 and 25 (included) ", 10
  7. lenPromptShiftValue equ $-promptShiftValue
  8.  
  9. promptStringValue db "Enter a string greater than 8 characters ", 10
  10. lenPromptStringValue equ $-promptStringValue
  11.  
  12. currentMessageText db "Current message: ", 10
  13. lenCurrentMessageText equ $-currentMessageText
  14.  
  15. testStringText db "character: ", 10
  16. lenTestStringText equ $-testStringText
  17.  
  18. shiftValueBuffer db 16 ; Buffer to store the input (2 digits + newline + null terminator)
  19. stringBuffer db 30
  20. ;result dw 0 ; Variable to store the converted integer result
  21. lower dw -25
  22. ;upper dw 25
  23. my_char db 'A'
  24.  
  25. section .bss
  26. result resw 16 ; Reserve 2 bytes for result variable
  27. charBuffer resw 2
  28.  
  29. section .text
  30. global main
  31.  
  32. main:
  33. call _handleObtainingShiftValue
  34. call _handleObtainingString
  35. call _handleCipher
  36.  
  37. mov rax, 60
  38. mov rdi, 0
  39. syscall
  40.  
  41. _handleObtainingShiftValue:
  42. call _printObtainShiftValueText
  43. call _obtainShiftValue
  44. call _checkIfShiftValueValid
  45. ret
  46.  
  47. _handleObtainingString:
  48. call _printObtainStringText
  49. call _obtainString
  50. mov eax, 0
  51. call _countLoop
  52. ret
  53.  
  54. _handleCipher:
  55. call _printCurrentMessageText
  56. ;call _printUnchangedString
  57. ;mov rdi, stringBuffer
  58. call _loopThroughCharacters
  59. ret
  60.  
  61. _printObtainShiftValueText:
  62. mov rax, 1
  63. mov rdi, 1
  64. mov rsi, promptShiftValue
  65. mov rdx, lenPromptShiftValue
  66. syscall
  67. ret
  68.  
  69. _obtainShiftValue:
  70. ; Get user input
  71. mov rax, 0
  72. mov rdi, 0
  73. mov rsi, shiftValueBuffer
  74. mov rdx, 16
  75. syscall
  76.  
  77. ; Convert input to ASCII
  78. xor ax, ax ; Clear AX (accumulator)
  79. xor ecx, ecx ; Clear ECX (loop counter)
  80. movzx esi, byte [shiftValueBuffer + ecx] ; Get the first character
  81.  
  82. ; Check for minus sign character '-'
  83. cmp esi, 45
  84. je _handleNegative
  85.  
  86. _convertLoop:
  87. ; Check if the character is a newline
  88. cmp byte [shiftValueBuffer + ecx], 10
  89. je _endConversion
  90.  
  91. ; Convert the character to a digit value
  92. sub esi, 0x30 ; Subtrac '0' to conver tot digit value
  93.  
  94. ; Multiply the accumulator by 10 and add the digit value
  95. imul ax, 10
  96. add ax, si
  97.  
  98. ; Move to the next character
  99. inc ecx
  100. movzx esi, byte [shiftValueBuffer + ecx]
  101. jmp _convertLoop
  102.  
  103. _handleNegative:
  104. ; Move to the next character
  105. inc ecx
  106. movzx esi, byte [shiftValueBuffer + ecx]
  107.  
  108. ; Convert the remaining characters to an integer
  109. jmp _convertLoop
  110.  
  111. _endConversion:
  112. ; Check if the number is negative
  113. cmp byte [shiftValueBuffer], 45
  114. je _makeNegative
  115.  
  116. ; Store the result
  117. mov [result], ax
  118. ret
  119.  
  120. _makeNegative:
  121. ; Negate the result
  122. neg ax
  123. mov [result], ax
  124.  
  125. _checkIfShiftValueValid:
  126. xor bx, bx ; clear out bx
  127. mov [result], ax
  128. cmp ax, -25
  129. jl _handleObtainingShiftValue
  130. cmp ax, 25
  131. jg _handleObtainingShiftValue
  132.  
  133. ret
  134.  
  135. _printObtainStringText:
  136. xor rsi, rsi
  137. mov rax, 1
  138. mov rdi, 1
  139. mov rsi, promptStringValue
  140. mov rdx, lenPromptStringValue
  141. syscall
  142.  
  143. ret
  144.  
  145. _obtainString:
  146. mov rax, 0
  147. mov rdi, 0
  148. mov rsi, stringBuffer
  149. mov rdx, 30
  150. syscall
  151.  
  152. ret
  153.  
  154. _countLoop:
  155. cmp byte [stringBuffer + rax], 0 ; Check for null terminator
  156. je _checkCount
  157.  
  158. inc eax
  159. cmp eax, 8
  160. jle _countLoop
  161.  
  162. ret
  163.  
  164. _checkCount:
  165. cmp eax, 8 ; Compare the character count with 8
  166. jle _handleObtainingString
  167.  
  168. mov ax, [stringBuffer]
  169. ret
  170.  
  171. _printCurrentMessageText:
  172. mov rax, 1
  173. mov rdi, 1
  174. mov rsi, currentMessageText
  175. mov rdx, lenCurrentMessageText
  176. syscall
  177.  
  178. ret
  179.  
  180. _printUnchangedString:
  181. mov rax, 1
  182. mov rdi, 1
  183. mov rsi, stringBuffer
  184. mov rdx, 16
  185. syscall
  186.  
  187. ret
  188.  
  189. _loopThroughCharacters:
  190. mov al, byte [stringBuffer]
  191. mov [charBuffer], al
  192.  
  193. mov rax, 1
  194. mov rdi, 1
  195. mov rsi, testStringText
  196. mov rdx, lenTestStringText
  197. syscall
  198.  
  199. ; Check if current character is null
  200. ;cmp al, 0
  201. ;ret
  202.  
  203. ; Process the current character here
  204. ; Example - print current char
  205. mov eax, 1
  206. mov edi, 1
  207. mov rsi, charBuffer
  208. mov edx, 1
  209. syscall
  210.  
  211. ; Move to the next character
  212. inc al
  213.  
  214. jmp _loopThroughCharacters
  215.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement