Question: I need the code output to be this way: o hello world found at postion 4 found at postion 7 But instead. its just this:
I need the code output to be this way:
o
hello world
found at postion
found at postion
But instead. its just this:
o
hello world.
section data
LC db ;
LC db "Search string exceeds maximum allowed length.",
LC db "Error: Search string is not followed by any text.",
LC db "Found at position d
section bss
inputBuffer resb
searchBuffer resb
section text
global main
extern fgets
extern stdin
extern strlen
extern puts
extern printf
extern strcspn
main:
push rbp
mov rbp rsp
sub rsp
; Read input from stdin
mov rdx QWORD stdin
lea rax, rbp
mov esi,
mov rdi, rax
call fgets
; Find the length of input string
lea rax, rbp
mov rdi, rax
call strcspn
mov BYTE rbp rax
; Check if input exceeds maximum allowed length
lea rax, rbp
mov rdi, rax
call strlen
cmp eax,
jg checkLengthError
; Continue reading until newline is encountered
readLoop:
mov rdx QWORD stdin
lea rax, rbp
mov esi,
mov rdi, rax
call fgets
test rax, rax
je endOfFile
movzx eax, BYTE rbp
cmp al
je readLoop
endOfFile:
jmp endProgram
processInput:
; Start searching for substrings
call searchSubstring
jmp readLoop
checkLengthError:
mov edi, LC
call puts
mov eax,
jmp endProgram
noTextError:
mov edi, LC
call puts
mov eax,
jmp endProgram
searchSubstring:
lea rax, rbp ; Load the address of the inputBuffer
mov rdi, rax ; Pass inputBuffer address to strlen
call strlen ; Get the length of input string
mov DWORD rbp eax ; Store the length of the input string
mov DWORD rbp ; Initialize the loop counter
outerLoop:
; Compare current substring with the input string
mov DWORD rbp ; Initialize the inner loop counter
innerLoop:
; Compare characters of substring and input string
mov edx, DWORD rbp ; Offset for input string
mov eax, DWORD rbp ; Offset for substring
add eax, edx
cdqe
movzx edx, BYTE rbp rax ; Character from input string
mov eax, DWORD rbp ; Offset for substring
cdqe
movzx eax, BYTE rbp rax ; Character from substring
cmp dl al ; Compare characters
jne notMatched ; If not matched, continue to the next position
add DWORD rbp ; Move to the next character in substring
notMatched:
; Check if the end of substring is reached
mov eax, DWORD rbp
cmp eax, DWORD rbp ; Length of substring
jl innerLoop ; If not reached end, continue inner loop
jmp checkMatch ; If reached end, check for match
checkMatch:
; Check if substring matches input string
mov eax, DWORD rbp ; Length of substring
cmp eax, DWORD rbp ; Length of input string
jne nextPosition ; If lengths are not equal, move to the next position
mov eax, DWORD rbp ; Print position if substring matches
inc eax ; Positions are based, so increment by
mov esi, eax
mov edi, LC
call printf
nextPosition:
; Move to the next position in the input string
add DWORD rbp ; Move to the next position
mov eax, DWORD rbp ; Length of the input string
sub eax, DWORD rbp ; Subtract the length of the substring
cmp DWORD rbp eax ; Compare with the remaining length of the input string
jle outerLoop ; If there is remaining length, continue outer loop
endProgram:
leave
ret
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
