Answered step by step
Verified Expert Solution
Question
1 Approved Answer
i have a segmentation fault i used gdp and this should be a a program that filters out non - printable ASCII characters. That is
i have a segmentation fault i used gdp and this should be a a program that filters out nonprintable ASCII characters. That is any character in the input from decimal space up to tilde should be copied to the output. Also copy to the output the characters at decimal values and tab and linefeed Any other character should not be copied to the output. it should take in a file
section data
newline db
section bss
inputbuffer resb
outputbuffer resb
section text
global start
start:
; Check if there are enough arguments
cmp dword esp
jne exit
; Open input file
mov eax, ; sysopen syscall number
mov ebx, esp ; input filename
mov ecx, ; flags ORDONLY
int x ; syscall
; Check if file opened successfully
cmp eax,
je exit ; If file open failed, exit
; Read input from file
mov eax, ; sysread syscall number
mov ebx, eax ; use file descriptor returned by open
mov ecx, inputbuffer
mov edx,
int x
; Close input file
mov eax, ; sysclose syscall number
mov ebx, ebx ; use file descriptor returned by open
int x
; Filter characters
mov esi, inputbuffer ; Source pointer
mov edi, outputbuffer ; Destination pointer
filterloop:
mov al byte esi ; Load character into AL
cmp al ; Check for null terminator
je endfilterloop
cmp al ; Check if character is printable
jl nextchar
cmp al
jg nextchar
cmp al ; Check if character is tab or newline
je copychar
cmp al
je copychar
jmp nextchar
copychar:
mov byte edi al ; Copy character to output buffer
inc edi ; Move to next position in output buffer
nextchar:
inc esi ; Move to next character in input buffer
jmp filterloop
endfilterloop:
mov byte edi ; Null terminate the output buffer
; Open output file
mov eax, ; sysopen syscall number
mov ebx, esp ; output filename
mov ecx, ; flags OWRONLY OCREAT OTRUNC
mov edx, ; mode rwrr
int x ; syscall
; Check if file opened successfully
cmp eax,
je exit ; If file open failed, exit
; Write filtered string to file
mov eax, ; syswrite syscall number
mov ebx, eax ; use file descriptor returned by open
mov ecx, outputbuffer
call strlen ; Calculate length of outputbuffer
add eax, ; Add to include null terminator
int x
; Close output file
mov eax, ; sysclose syscall number
mov ebx, eax ; use file descriptor returned by open
int x
exit:
; Exit
mov eax,
xor ebx, ebx
int x
strlen: ; Function to calculate string length
xor eax, eax ; Counter
loop:
cmp byte ecx eax ; Check for null terminator
je done
inc eax ; Increment counter
jmp loop
done:
ret
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started