Assembly Language: For this program, you declare three double words containing z
ID: 3792923 • Letter: A
Question
Assembly Language:
For this program, you declare three double words containing zeros and ones: Array1 SDWORD 0, 1, 1, 0, 1, 1, 1, 0 ; success Array2 SDWORD 0, 0, 0, 0, 0, 1, 1, 1 ; success Array3 SDWORD 0, 0, 0, 0, 1, 1, 0, 0 ; failure Array4 SDWORD 0, 0, 0, 0, 0, 0, 1, 1 ; failure You will detect a sequence of exactly 3 ones in an array of double words. The sequence of ones can be in the middle of the array, i.e. 3 one's in succession followed by a zero, or at the end of the array, i.e. 3 one's in succession followed by the end of the array. You will report on success or failure of the search, and if successful, the array index where the beginning of the sequence of three 1's occurs. Your output (given the arrays above) will look exactly as follows: Array 1: Found 111 Pattern at Array Index 4 Array 2: Found 111 Pattern at Array Index 5 Array 3: Did Not Find 111 Pattern -- Search Discontinued Array 4: Did Not Find 111 Pattern -- Search Discontinued Guidelines: You should include Irvine32.inc. Your program must contain (in addition to main) two procedures, one called Find111, and the other called DisplayResult. Each is called three times for each of the three arrays. The equivalent call to Find111 in C is: Find111(int &arr, int size, int&posn) Find111 uses registers esi to contain the address of the beginning of the array, ecx will contain the size of the array, ebx will contain position, i.e. first occurrence of 1 in 111, in array where match is found, edx (optional) is locally used in the procedure to keep track of the current position in the array as it is being checked. in Find111, register eax is used in both main and Find111 to indicate whether the search for 111 was successful. eax = 1 is success, eax = 0 is failure. Find111 is called 4 times in main. DisplayResult is called without arguments and uses eax and edx.
Explanation / Answer
import java.io.*;
class PushZero
{
// Function which pushes all zeros to end of an array.
static void pushZerosToEnd(int arr[], int n)
{
int count = 0; // Count of non-zero elements
// Traverse the array. If element encountered is
// non-zero, then replace the element at index 'count'
// with this element
for (int i = 0; i < n; i++)
if (arr[i] != 0)
arr[count++] = arr[i]; // here count is
// incremented
// Now all non-zero elements have been shifted to
// front and 'count' is set as index of first 0.
// Make all elements 0 from count to end.
while (count < n)
arr[count++] = 0;
}
/*Driver function to check for above functions*/
public static void main (String[] args)
{
int arr[] = {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9};
int n = arr.length;
pushZerosToEnd(arr, n);
System.out.println("Array after pushing zeros to the back: ");
for (int i=0; i<n; i++)
System.out.print(arr[i]+" ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.