Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; Asks user for number between -25 and 25
- ; Asks the user for a string, must be longer than 8 characters long
- ; Shifts string
- section .data
- promptShiftValue db "Enter a shift value between -25 and 25 (included) ", 10
- promptStringValue db "Enter a string greater than 8 characters ", 10
- ;confirmString1 db "Here's the value you entered: ", 10
- buffer db 16 ; Buffer to store the input (2 digits + newline + null terminator)
- ;result dw 0 ; Variable to store the converted integer result
- lower dw -25
- ;upper dw 25
- section .bss
- result resw 16 ; Reserve 2 bytes for result variable
- section .text
- global main
- main:
- call _printObtainShiftValueText
- call _obtainShiftValue
- call _checkIfValid
- call _printObtainStringText
- mov rax, 60
- mov rdi, 0
- syscall
- _printObtainShiftValueText:
- mov rax, 1
- mov rdi, 1
- mov rsi, promptShiftValue
- mov rdx, 50
- syscall
- ret
- _obtainShiftValue:
- xor ax, bx
- ; Get user input
- mov rax, 0
- mov rdi, 0
- mov rsi, buffer
- mov rdx, 16
- syscall
- ; Print user input
- ;mov rax, 1
- ;mov rdi, 1
- ;mov rsi, buffer
- ;mov rdx, 3
- ;syscall
- ; Convert input to ASCII
- xor ax, ax ; Clear AX (accumulator)
- xor ecx, ecx ; Clear ECX (loop counter)
- movzx esi, byte [buffer + ecx] ; Get the first character
- ; Check for minus sign character '-'
- cmp esi, 45
- je _handleNegative
- ; Move result to ax and lower to bx
- ; mov ax, [result]
- ; mov bx, [lower]
- ; cmp ax, bx
- ; jl _obtainShiftValue ; (repeat _obtainShiftValue again if shiftValue < lower)
- ;cmp ax, upper ; (upper defined as 25)
- ;jg _obtainShiftValue ; (repeatt _obtainshiftValue again if shiftValue < upper)
- ;jmp _obtainString ; (neither condition)
- _convertLoop:
- ; Check if the character is a newline
- cmp byte [buffer + ecx], 10
- je _endConversion
- ; Convert the character to a digit value
- sub esi, 0x30 ; Subtrac '0' to conver tot digit value
- ; Multiply the accumulator by 10 and add the digit value
- imul ax, 10
- add ax, si
- ; Move to the next character
- inc ecx
- movzx esi, byte [buffer + ecx]
- jmp _convertLoop
- _handleNegative:
- ; Move to the next character
- inc ecx
- movzx esi, byte [buffer + ecx]
- ; Convert the remaining characters to an integer
- jmp _convertLoop
- _endConversion:
- ; Check if the number is negative
- cmp byte [buffer], 45
- je _makeNegative
- ; Store the result
- mov [result], ax
- ret
- _makeNegative:
- ; Negate the result
- neg ax
- mov [result], ax
- _checkIfValid:
- xor bx, bx ; clear out bx
- mov [result], ax
- cmp ax, -25
- jl main
- ret
- _printObtainStringText:
- mov rax, 1
- mov rdi, 1
- mov rsi, promptStringValue
- mov rdx, 43
- syscall
- ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement