Create a procedure named FindThrees that returns 1 if an array has three consecu
ID: 3818859 • Letter: C
Question
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.
Here is what I have so far,
;Create procedure name FindThrees
INCLUDE Irvine32.inc
;FindThrees PROTO,
;pointerArr: PTR DWORD,
;ArrLeng:DWORD
.data
;strContains BYTE "The array has three consecutive 3.",0
;strNotContain 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 strContains
call WriteString ; writes strContains
call Crlf
.ELSE
mov edx,OFFSET strNotContain
call WriteString ; writes strNotContain
call Crlf
.ENDIF
; find if the array arrTwo contains three consecutive 3
INVOKE FindThrees, ADDR arrTwo, LENGTHOF arrTwo
.IF eax == 1
mov edx,OFFSET strContains
call WriteString ; writes strContains
call Crlf
.ELSE
mov edx,OFFSET strNotContain
call WriteString ; writes strNotContain
call Crlf
.ENDIF
; find if the array arrThree contains three consecutive 3
INVOKE FindThrees, ADDR arrThree, LENGTHOF arrThree
.IF eax == 1
mov edx,OFFSET strContains
call WriteString ; writes strContains
call Crlf
.ELSE
mov edx,OFFSET strNotContain
call WriteString ; writes strNotContain
call Crlf
.ENDIF
exit
main ENDP
FindThrees PROC USES esi ecx,
pointerArr: PTR DWORD, ; points the array
ArrLeng: DWORD ; 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,pointerArr ; ESI is the pointer to array
mov ecx,ArrLeng
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
Explanation / Answer
Hi,
Please find the updated code for inlcuding comments and output of the code below:-
CODE:-
======================================================================
;Create procedure name FindThrees
INCLUDE Irvine32.inc
FindThrees PROTO,
pointerArr: PTR DWORD,
ArrLeng:DWORD
.data
strContains BYTE "The array has three consecutive 3.",0
strNotContain BYTE "The array does not contain three consecutive
3.",0
strdisplay "The four arrays results are:-",0
arrOne BYTE 1, 2, 3, 2
arrTwo BYTE 3, 3, 5, 7, 3
arrThree BYTE 4, 3, 3, 3, 1, 8
arrFour BYTE 5,6,3,3,3,2,1,1
.code
main PROC
; calls the procedures
call Clrscr ; clears the screen
; find if the array arrOne contains three consecutive 3
move edx OFFSET strdisplay
INVOKE FindThrees, ADDR arrOne, LENGTHOF arrOne
.IF eax == 1
mov edx,OFFSET strContains
; writes strContains which will display if array contains consecutive 3's.
call WriteString
call Crlf
.ELSE
mov edx,OFFSET strNotContain
; writes strContains which will display if array does not contains consecutive 3's.
call WriteString
call Crlf
.ENDIF
; find if the array arrTwo contains three consecutive 3
INVOKE FindThrees, ADDR arrTwo, LENGTHOF arrTwo
.IF eax == 1
mov edx,OFFSET strContains
; writes strContains which will display if array contains consecutive 3's.
call WriteString
call Crlf
.ELSE
mov edx,OFFSET strNotContain
; writes strContains which will display if array does not contains consecutive 3's.
call WriteString
call Crlf
.ENDIF
; find if the array arrThree contains three consecutive 3
INVOKE FindThrees, ADDR arrThree, LENGTHOF arrThree
.IF eax == 1
mov edx,OFFSET strContains
; writes strContains which will display if array contains consecutive 3's.
call WriteString
call Crlf
.ELSE
mov edx,OFFSET strNotContain
; writes strContains which will display if array does not contains consecutive 3's.
call WriteString
call Crlf
.ENDIF
; find if the array arrFour contains three consecutive 3
INVOKE FindThrees, ADDR arrFour, LENGTHOF arrFour
.IF eax == 1
mov edx,OFFSET strContains
; writes strContains which will display if array contains consecutive 3's.
call WriteString
call Crlf
.ELSE
mov edx,OFFSET strNotContain
; writes strContains which will display if array does not contains consecutive 3's.
call WriteString
call Crlf
.ENDIF
exit
main ENDP
;The body of the procedure FindThrees.
FindThrees PROC USES esi ecx,
pointerArr: PTR DWORD, ; points the array
ArrLeng: DWORD ; 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)
; initialize EAX = 0
mov eax,0
; ESI is the pointer to array
mov esi,pointerArr
mov ecx,ArrLeng
; as next 2 elements were observed
sub ecx,2
L1:
.IF [esi] == 3
.IF [esi+1] == 3
.IF [esi+2] == 3
; set the value of EAX = 1
mov eax,1
jmp L2
.ENDIF
.ENDIF
.ENDIF
;Increment ESI
inc esi
loop L1
L2:
; returns EAX to main
ret
FindThrees ENDP
END main
======================================================================
OUTPUT OF THE CODE IS:-
The four arrays results are:-
The array does not contain three consecutive 3.
The array does not contain three consecutive 3.
The array has three consecutive 3.
The array has three consecutive 3.
===================================================================================
Please let me know in case of clarification
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.