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 4
found at postion 7
But instead. its just this:
o
hello world.
section .data
LC0 db 10,0 ; "
"
LC1 db "Search string exceeds maximum allowed length.", 0
LC2 db "Error: Search string is not followed by any text.", 0
LC3 db "Found at position %d",10,0
section .bss
inputBuffer resb 1024
searchBuffer resb 1024
section .text
global main
extern fgets
extern stdin
extern strlen
extern puts
extern printf
extern strcspn
main:
push rbp
mov rbp, rsp
sub rsp,2032
; Read input from stdin
mov rdx, QWORD [stdin]
lea rax, [rbp -1024]
mov esi, 1000
mov rdi, rax
call fgets
; Find the length of input string
lea rax, [rbp -1024]
mov rdi, rax
call strcspn
mov BYTE [rbp -1024+ rax],0
; Check if input exceeds maximum allowed length
lea rax, [rbp -1024]
mov rdi, rax
call strlen
cmp eax, 999
jg checkLengthError
; Continue reading until newline is encountered
readLoop:
mov rdx, QWORD [stdin]
lea rax, [rbp -2032]
mov esi, 1000
mov rdi, rax
call fgets
test rax, rax
je endOfFile
movzx eax, BYTE [rbp -2032]
cmp al,10
je readLoop
endOfFile:
jmp endProgram
processInput:
; Start searching for substrings
call searchSubstring
jmp readLoop
checkLengthError:
mov edi, LC1
call puts
mov eax, 1
jmp endProgram
noTextError:
mov edi, LC2
call puts
mov eax, 1
jmp endProgram
searchSubstring:
lea rax, [rbp -1024] ; 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 -12], eax ; Store the length of the input string
mov DWORD [rbp -4],0 ; Initialize the loop counter
outerLoop:
; Compare current substring with the input string
mov DWORD [rbp -8],0 ; Initialize the inner loop counter
innerLoop:
; Compare characters of substring and input string
mov edx, DWORD [rbp -4] ; Offset for input string
mov eax, DWORD [rbp -8] ; Offset for substring
add eax, edx
cdqe
movzx edx, BYTE [rbp -2032+ rax] ; Character from input string
mov eax, DWORD [rbp -8] ; Offset for substring
cdqe
movzx eax, BYTE [rbp -1024+ rax] ; Character from substring
cmp dl, al ; Compare characters
jne notMatched ; If not matched, continue to the next position
add DWORD [rbp -8],1 ; Move to the next character in substring
notMatched:
; Check if the end of substring is reached
mov eax, DWORD [rbp -8]
cmp eax, DWORD [rbp -16] ; 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 -8] ; Length of substring
cmp eax, DWORD [rbp -12] ; Length of input string
jne nextPosition ; If lengths are not equal, move to the next position
mov eax, DWORD [rbp -4] ; Print position if substring matches
inc eax ; Positions are 1-based, so increment by 1
mov esi, eax
mov edi, LC3
call printf
nextPosition:
; Move to the next position in the input string
add DWORD [rbp -4],1 ; Move to the next position
mov eax, DWORD [rbp -16] ; Length of the input string
sub eax, DWORD [rbp -12] ; Subtract the length of the substring
cmp DWORD [rbp -4], 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!