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
- lenPromptShiftValue equ $-promptShiftValue
- promptStringValue db "Enter a string greater than 8 characters ", 10
- lenPromptStringValue equ $-promptStringValue
- currentMessageText db "Current message: ", 10
- lenCurrentMessageText equ $-currentMessageText
- testStringText db "character: ", 10
- lenTestStringText equ $-testStringText
- shiftValueBuffer db 16 ; Buffer to store the input (2 digits + newline + null terminator)
- stringBuffer db 30
- ;result dw 0 ; Variable to store the converted integer result
- lower dw -25
- ;upper dw 25
- my_char db 'A'
- section .bss
- result resw 16 ; Reserve 2 bytes for result variable
- charBuffer resw 2
- section .text
- global main
- main:
- call _handleObtainingShiftValue
- call _handleObtainingString
- call _handleCipher
- mov rax, 60
- mov rdi, 0
- syscall
- _handleObtainingShiftValue:
- call _printObtainShiftValueText
- call _obtainShiftValue
- call _checkIfShiftValueValid
- ret
- _handleObtainingString:
- call _printObtainStringText
- call _obtainString
- mov eax, 0
- call _countLoop
- ret
- _handleCipher:
- call _printCurrentMessageText
- ;call _printUnchangedString
- ;mov rdi, stringBuffer
- call _loopThroughCharacters
- ret
- _printObtainShiftValueText:
- mov rax, 1
- mov rdi, 1
- mov rsi, promptShiftValue
- mov rdx, lenPromptShiftValue
- syscall
- ret
- _obtainShiftValue:
- ; Get user input
- mov rax, 0
- mov rdi, 0
- mov rsi, shiftValueBuffer
- mov rdx, 16
- syscall
- ; Convert input to ASCII
- xor ax, ax ; Clear AX (accumulator)
- xor ecx, ecx ; Clear ECX (loop counter)
- movzx esi, byte [shiftValueBuffer + ecx] ; Get the first character
- ; Check for minus sign character '-'
- cmp esi, 45
- je _handleNegative
- _convertLoop:
- ; Check if the character is a newline
- cmp byte [shiftValueBuffer + 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 [shiftValueBuffer + ecx]
- jmp _convertLoop
- _handleNegative:
- ; Move to the next character
- inc ecx
- movzx esi, byte [shiftValueBuffer + ecx]
- ; Convert the remaining characters to an integer
- jmp _convertLoop
- _endConversion:
- ; Check if the number is negative
- cmp byte [shiftValueBuffer], 45
- je _makeNegative
- ; Store the result
- mov [result], ax
- ret
- _makeNegative:
- ; Negate the result
- neg ax
- mov [result], ax
- _checkIfShiftValueValid:
- xor bx, bx ; clear out bx
- mov [result], ax
- cmp ax, -25
- jl _handleObtainingShiftValue
- cmp ax, 25
- jg _handleObtainingShiftValue
- ret
- _printObtainStringText:
- xor rsi, rsi
- mov rax, 1
- mov rdi, 1
- mov rsi, promptStringValue
- mov rdx, lenPromptStringValue
- syscall
- ret
- _obtainString:
- mov rax, 0
- mov rdi, 0
- mov rsi, stringBuffer
- mov rdx, 30
- syscall
- ret
- _countLoop:
- cmp byte [stringBuffer + rax], 0 ; Check for null terminator
- je _checkCount
- inc eax
- cmp eax, 8
- jle _countLoop
- ret
- _checkCount:
- cmp eax, 8 ; Compare the character count with 8
- jle _handleObtainingString
- mov ax, [stringBuffer]
- ret
- _printCurrentMessageText:
- mov rax, 1
- mov rdi, 1
- mov rsi, currentMessageText
- mov rdx, lenCurrentMessageText
- syscall
- ret
- _printUnchangedString:
- mov rax, 1
- mov rdi, 1
- mov rsi, stringBuffer
- mov rdx, 16
- syscall
- ret
- _loopThroughCharacters:
- mov al, byte [stringBuffer]
- mov [charBuffer], al
- mov rax, 1
- mov rdi, 1
- mov rsi, testStringText
- mov rdx, lenTestStringText
- syscall
- ; Check if current character is null
- ;cmp al, 0
- ;ret
- ; Process the current character here
- ; Example - print current char
- mov eax, 1
- mov edi, 1
- mov rsi, charBuffer
- mov edx, 1
- syscall
- ; Move to the next character
- inc al
- jmp _loopThroughCharacters
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement