Answered step by step
Verified Expert Solution
Link Copied!

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 non-printable ASCII characters. That is, any character in the input from decimal 32(space) up to 126(tilde) should be copied to the output. Also copy to the output the characters at decimal values 9 and 10(tab and line-feed). Any other character should not be copied to the output. it should take in a file
section .data
newline db 10
section .bss
input_buffer resb 256
output_buffer resb 256
section .text
global _start
_start:
; Check if there are enough arguments
cmp dword [esp],2
jne .exit
; Open input file
mov eax, 5 ; sys_open syscall number
mov ebx, [esp +4] ; input filename
mov ecx, 0 ; flags (O_RDONLY)
int 0x80 ; syscall
; Check if file opened successfully
cmp eax, -1
je .exit ; If file open failed, exit
; Read input from file
mov eax, 3 ; sys_read syscall number
mov ebx, eax ; use file descriptor returned by open
mov ecx, input_buffer
mov edx, 255
int 0x80
; Close input file
mov eax, 6 ; sys_close syscall number
mov ebx, ebx ; use file descriptor returned by open
int 0x80
; Filter characters
mov esi, input_buffer ; Source pointer
mov edi, output_buffer ; Destination pointer
.filter_loop:
mov al, byte [esi] ; Load character into AL
cmp al,0 ; Check for null terminator
je .end_filter_loop
cmp al,32 ; Check if character is printable
jl .next_char
cmp al,126
jg .next_char
cmp al,9 ; Check if character is tab or newline
je .copy_char
cmp al,10
je .copy_char
jmp .next_char
.copy_char:
mov byte [edi], al ; Copy character to output buffer
inc edi ; Move to next position in output buffer
.next_char:
inc esi ; Move to next character in input buffer
jmp .filter_loop
.end_filter_loop:
mov byte [edi],0 ; Null terminate the output buffer
; Open output file
mov eax, 5 ; sys_open syscall number
mov ebx, [esp +8] ; output filename
mov ecx, 641 ; flags (O_WRONLY | O_CREAT | O_TRUNC)
mov edx, 0644 ; mode (rw-r--r--)
int 0x80 ; syscall
; Check if file opened successfully
cmp eax, -1
je .exit ; If file open failed, exit
; Write filtered string to file
mov eax, 4 ; sys_write syscall number
mov ebx, eax ; use file descriptor returned by open
mov ecx, output_buffer
call strlen ; Calculate length of output_buffer
add eax, 1 ; Add 1 to include null terminator
int 0x80
; Close output file
mov eax, 6 ; sys_close syscall number
mov ebx, eax ; use file descriptor returned by open
int 0x80
.exit:
; Exit
mov eax, 1
xor ebx, ebx
int 0x80
strlen: ; Function to calculate string length
xor eax, eax ; Counter
.loop:
cmp byte [ecx + eax],0 ; 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Probabilistic Databases

Authors: Dan Suciu, Dan Olteanu, Christopher Re, Christoph Koch

1st Edition

3031007514, 978-3031007514

More Books

Students also viewed these Databases questions

Question

What is the Definition for Third Normal Form?

Answered: 1 week ago

Question

Provide two examples of a One-To-Many relationship.

Answered: 1 week ago