Advertisement
xFazz

proj3-1, v1

Jul 7th, 2023
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 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. shiftValueBuffer db 16 ; Buffer to store the input (2 digits + newline + null terminator)
  16. stringBuffer db 16
  17. ;result dw 0 ; Variable to store the converted integer result
  18. lower dw -25
  19. ;upper dw 25
  20. my_char db 'A'
  21.  
  22. section .bss
  23. result resw 16 ; Reserve 2 bytes for result variable
  24.  
  25. section .text
  26. global main
  27.  
  28. main:
  29. call _handleObtainingShiftValue
  30. call _handleObtainingString
  31.  
  32. mov rax, 60
  33. mov rdi, 0
  34. syscall
  35.  
  36. _handleObtainingShiftValue:
  37. call _printObtainShiftValueText
  38. call _obtainShiftValue
  39. call _checkIfShiftValueValid
  40. ret
  41.  
  42. _handleObtainingString:
  43. call _printObtainStringText
  44. call _obtainString
  45. mov eax, 0
  46. call _countLoop
  47. ret
  48.  
  49. _handleCipher:
  50. call _printCurrentMessageText
  51. ret
  52.  
  53. _printObtainShiftValueText:
  54. mov rax, 1
  55. mov rdi, 1
  56. mov rsi, promptShiftValue
  57. mov rdx, 50
  58. syscall
  59. ret
  60.  
  61. _obtainShiftValue:
  62. ; Get user input
  63. mov rax, 0
  64. mov rdi, 0
  65. mov rsi, shiftValueBuffer
  66. mov rdx, 16
  67. syscall
  68.  
  69. ; Convert input to ASCII
  70. xor ax, ax ; Clear AX (accumulator)
  71. xor ecx, ecx ; Clear ECX (loop counter)
  72. movzx esi, byte [shiftValueBuffer + ecx] ; Get the first character
  73.  
  74. ; Check for minus sign character '-'
  75. cmp esi, 45
  76. je _handleNegative
  77.  
  78. _convertLoop:
  79. ; Check if the character is a newline
  80. cmp byte [shiftValueBuffer + ecx], 10
  81. je _endConversion
  82.  
  83. ; Convert the character to a digit value
  84. sub esi, 0x30 ; Subtrac '0' to conver tot digit value
  85.  
  86. ; Multiply the accumulator by 10 and add the digit value
  87. imul ax, 10
  88. add ax, si
  89.  
  90. ; Move to the next character
  91. inc ecx
  92. movzx esi, byte [shiftValueBuffer + ecx]
  93. jmp _convertLoop
  94.  
  95. _handleNegative:
  96. ; Move to the next character
  97. inc ecx
  98. movzx esi, byte [shiftValueBuffer + ecx]
  99.  
  100. ; Convert the remaining characters to an integer
  101. jmp _convertLoop
  102.  
  103. _endConversion:
  104. ; Check if the number is negative
  105. cmp byte [shiftValueBuffer], 45
  106. je _makeNegative
  107.  
  108. ; Store the result
  109. mov [result], ax
  110. ret
  111.  
  112. _makeNegative:
  113. ; Negate the result
  114. neg ax
  115. mov [result], ax
  116.  
  117. _checkIfShiftValueValid:
  118. xor bx, bx ; clear out bx
  119. mov [result], ax
  120. cmp ax, -25
  121. jl _handleObtainingShiftValue
  122. cmp ax, 25
  123. jg _handleObtainingShiftValue
  124.  
  125. ret
  126.  
  127. _printObtainStringText:
  128. xor rsi, rsi
  129. mov rax, 1
  130. mov rdi, 1
  131. mov rsi, promptStringValue
  132. mov rdx, 42
  133. syscall
  134.  
  135. ret
  136.  
  137. _obtainString:
  138. mov rax, 0
  139. mov rdi, 0
  140. mov rsi, stringBuffer
  141. mov rdx, 8
  142. syscall
  143.  
  144. ret
  145.  
  146. _countLoop:
  147. cmp byte [stringBuffer + rax], 0 ; Check for null terminator
  148. je _checkCount
  149.  
  150. inc eax
  151. cmp eax, 8
  152. jle _countLoop
  153.  
  154. _checkCount:
  155. cmp eax, 8 ; Compare the character count with 8
  156. jle _handleObtainingString
  157.  
  158. call _handleCipher
  159.  
  160. _printCurrentMessageText:
  161. mov rax, 1
  162. mov rdi, 1
  163. mov rsi, currentMessageText
  164. mov rdx, lenCurrentMessageText
  165. syscall
  166.  
  167. ret
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement