Advertisement
xFazz

proj3 version3

Jul 1st, 2023 (edited)
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 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. promptStringValue db "Enter a string greater than 8 characters ", 10
  8. ;confirmString1 db "Here's the value you entered: ", 10
  9. buffer db 16 ; Buffer to store the input (2 digits + newline + null terminator)
  10. ;result dw 0 ; Variable to store the converted integer result
  11. lower dw -25
  12. ;upper dw 25
  13.  
  14. section .bss
  15. result resw 16 ; Reserve 2 bytes for result variable
  16.  
  17. section .text
  18. global main
  19.  
  20. main:
  21. call _printObtainShiftValueText
  22. call _obtainShiftValue
  23. call _checkIfValid
  24. call _printObtainStringText
  25.  
  26. mov rax, 60
  27. mov rdi, 0
  28. syscall
  29.  
  30. _printObtainShiftValueText:
  31. mov rax, 1
  32. mov rdi, 1
  33. mov rsi, promptShiftValue
  34. mov rdx, 50
  35. syscall
  36. ret
  37.  
  38. _obtainShiftValue:
  39. xor ax, bx
  40. ; Get user input
  41. mov rax, 0
  42. mov rdi, 0
  43. mov rsi, buffer
  44. mov rdx, 16
  45. syscall
  46.  
  47. ; Print user input
  48. ;mov rax, 1
  49. ;mov rdi, 1
  50. ;mov rsi, buffer
  51. ;mov rdx, 3
  52. ;syscall
  53.  
  54. ; Convert input to ASCII
  55. xor ax, ax ; Clear AX (accumulator)
  56. xor ecx, ecx ; Clear ECX (loop counter)
  57. movzx esi, byte [buffer + ecx] ; Get the first character
  58.  
  59. ; Check for minus sign character '-'
  60. cmp esi, 45
  61. je _handleNegative
  62.  
  63. ; Move result to ax and lower to bx
  64. ; mov ax, [result]
  65. ; mov bx, [lower]
  66.  
  67. ; cmp ax, bx
  68. ; jl _obtainShiftValue ; (repeat _obtainShiftValue again if shiftValue < lower)
  69.  
  70. ;cmp ax, upper ; (upper defined as 25)
  71. ;jg _obtainShiftValue ; (repeatt _obtainshiftValue again if shiftValue < upper)
  72.  
  73. ;jmp _obtainString ; (neither condition)
  74.  
  75. _convertLoop:
  76. ; Check if the character is a newline
  77. cmp byte [buffer + ecx], 10
  78. je _endConversion
  79.  
  80. ; Convert the character to a digit value
  81. sub esi, 0x30 ; Subtrac '0' to conver tot digit value
  82.  
  83. ; Multiply the accumulator by 10 and add the digit value
  84. imul ax, 10
  85. add ax, si
  86.  
  87. ; Move to the next character
  88. inc ecx
  89. movzx esi, byte [buffer + ecx]
  90. jmp _convertLoop
  91.  
  92. _handleNegative:
  93. ; Move to the next character
  94. inc ecx
  95. movzx esi, byte [buffer + ecx]
  96.  
  97. ; Convert the remaining characters to an integer
  98. jmp _convertLoop
  99.  
  100. _endConversion:
  101. ; Check if the number is negative
  102. cmp byte [buffer], 45
  103. je _makeNegative
  104.  
  105. ; Store the result
  106. mov [result], ax
  107. ret
  108.  
  109. _makeNegative:
  110. ; Negate the result
  111. neg ax
  112. mov [result], ax
  113.  
  114. _checkIfValid:
  115. xor bx, bx ; clear out bx
  116. mov [result], ax
  117. cmp ax, -25
  118.  
  119. jl main
  120. ret
  121.  
  122. _printObtainStringText:
  123. mov rax, 1
  124. mov rdi, 1
  125. mov rsi, promptStringValue
  126. mov rdx, 43
  127. syscall
  128. ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement