this is assembly language for x-86 processors using microsoft visual studio Crea
ID: 3838272 • Letter: T
Question
this is assembly language for x-86 processors using microsoft visual studio
Create a procedure named FindThrees that returns 1 if an array has three consecutive values of 3 somewhere in the array. Otherwise, return 0. The procedure’s input parameter list contains a pointer to the array and the array’s size. Use the PROC directive with a parameter list when declaring the procedure. Preserve all registers (except EAX) that are modified by the procedure. Write a test program that calls FindThrees several times with different arrays.
Explanation / Answer
length: BYTE.data
strCons BYTE “The array contains three consecutive 3.”,0
strNonCon BYTE “The array does not contain three consecutive 3.”,0
arrOne BYTE 1, 2, 3, 2
arrTwo BYTE 3, 3, 5, 7, 3
arrThree BYTE 4, 3, 3, 3, 1, 8.code
main PROC
; calls the procedures
call Clrscr ; clears the screen
; find if the array arrOne contains three consecutive 3
INVOKE FindThrees, ADDR arrOne, LENGTHOF arrOne
.IF eax == 1
mov edx,OFFSET strCons
call WriteString ; writes strCons
call Crlf
.ELSE
mov edx,OFFSET strNonCon
call WriteString ; writes strNonCon
call Crlf
.ENDIF; find if the array arrTwo contains three consecutive 3
INVOKE FindThrees, ADDR arrTwo, LENGTHOF arrTwo
.IF eax == 1
mov edx,OFFSET strCons
call WriteString ; writes strCons
call Crlf
.ELSE
mov edx,OFFSET strNonCon
call WriteString ; writes strNonCon
call Crlf
.ENDIF; find if the array arrThree contains three consecutive 3
INVOKE FindThrees, ADDR arrThree, LENGTHOF arrThree
.IF eax == 1
mov edx,OFFSET strCons
call WriteString ; writes strCons
call Crlf
.ELSE
mov edx,OFFSET strNonCon
call WriteString ; writes strNonCon
call Crlf
.ENDIF
exit
main ENDPFindThrees PROC USES esi ecx,
pArr: PTR BYTE, ; points the array
length: BYTE ; the length of array
; finds if the array contains three consecutive 3 as its values
; Receives: pointer to array and the length of array
; Returns: EAX = 1 (true) or 0 (false)
mov eax,0 ; initialize EAX = 0
mov esi,pArr ; ESI is the pointer to array
mov ecx,length
sub ecx,2 ; as next 2 elements were observed
L1:
.IF [esi] == 3
.IF [esi+1] == 3
.IF [esi+2] == 3
mov eax,1 ; set EAX = 1
jmp L2
.ENDIF
.ENDIF
.ENDIF
inc esi ;Increment ESI
loop L1
L2:
ret ; returns EAX
FindThrees ENDP
END main
The same program is written is C language also
#include<stdio.h>
#include<stdlib.h>
/* Helper functions to get minimum and maximum in an array */
int getMin(int arr[], int n);
int getMax(int arr[], int n);
/* The function checks if the array elements are consecutive
If elements are consecutive, then returns true, else returns
false */
bool areConsecutive(int arr[], int n)
{
if ( n < 1 )
return false;
/* 1) Get the minimum element in array */
int min = getMin(arr, n);
/* 2) Get the maximum element in array */
int max = getMax(arr, n);
/* 3) max - min + 1 is equal to n, then only check all elements */
if (max - min + 1 == n)
{
/* Create a temp array to hold visited flag of all elements.
Note that, calloc is used here so that all values are initialized
as false */
bool *visited = (bool *) calloc (n, sizeof(bool));
int i;
for (i = 0; i < n; i++)
{
/* If we see an element again, then return false */
if ( visited[arr[i] - min] != false )
return false;
/* If visited first time, then mark the element as visited */
visited[arr[i] - min] = true;
}
/* If all elements occur once, then return true */
return true;
}
return false; // if (max - min + 1 != n)
}
/* UTILITY FUNCTIONS */
int getMin(int arr[], int n)
{
int min = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] < min)
min = arr[i];
return min;
}
int getMax(int arr[], int n)
{
int max = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
/* Driver program to test above functions */
int main()
{
int arr[]= {5, 4, 2, 3, 1, 6};
int n = sizeof(arr)/sizeof(arr[0]);
if(areConsecutive(arr, n) == true)
printf(" Array elements are consecutive ");
else
printf(" Array elements are not consecutive ");
getchar();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.