Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CORRECT THE ERROR IN THIS CODE.(THE CODE IS ABOUT Given an array of size n, take inputs from the user and add them to the

CORRECT THE ERROR IN THIS CODE.(THE CODE IS ABOUT Given an array of size n, take inputs from the user and add them to the array only if they satisfy the following conditions: it is a prime and it is a non-duplicate number. The program stops taking inputs when the array is full. Print the resultant array.) IN ASSEMBLY LANGUAGE

section .data array db 20 dup(0) ; Declare an array of 20 bytes, initialized to 0 array_size equ $ ; Declare a constant for the size of the array message db "Enter a number: ", 0 number dw 0 ; variable to hold the input number

section .text global _start

_start: ; Initialize counter for array index mov ecx, 0

get_input: ; Print message asking for input mov edx,sizeof message ; length of message mov eax, 4 ; sys_write mov ebx, 1 ; stdout mov ecx, message int 0x80

; Get input from user mov edx, 2 ; Input is 2 bytes (word) mov eax, 3 ; sys_read mov ebx, 0 ; stdin mov ecx, number int 0x80

; Move input into ebx mov ebx, [number]

; Check if input is less than 2 (0 and 1 are not prime) cmp ebx, 2 jl not_prime

; Check if input is greater than or equal to the array size cmp ebx, array_size jge not_prime

; Check if input is already in the array mov ecx, 0 check_duplicate: cmp byte [array + ecx], ebx je not_prime inc ecx cmp ecx, array_size jl check_duplicate

; Check if input is prime mov ecx, 2 mov edx, 0 check_prime: cmp ebx, ecx je prime mov edx, 0 div ecx cmp edx, 0 je not_prime inc ecx cmp ecx, ebx jl check_prime

prime: ; Input is prime and not a duplicate, store it in the array mov byte [array + ecx], ebx

; Increase the array index counter inc ecx

not_prime: ; Check if the array is full cmp ecx, array_size jne get_input

; Print the array mov eax, 4 mov ebx, 1 mov ecx, array mov edx, array_size int 0x80

; Exit mov eax, 1 xor ebx, ebx int 0x80

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

Students also viewed these Databases questions

Question

D How will your group react to this revelation?

Answered: 1 week ago