Hello, I have a program here that 1. Sums the integers the user has given 2. Fin
ID: 3625317 • Letter: H
Question
Hello, I have a program here that1. Sums the integers the user has given
2. Finds the largest number given
3. displays number of odd integers
I am having trouble with a function that finds the odd numbers.
I'm assuming it has something do to with the Parity Flag, but I cannot
figure out how to code it. Any suggestions?
FYI: The calls such as writestring and writeint are from the author
INTEGER_COUNT = 3
.data
str1 BYTE "Enter a signed integer: ",0
str2 BYTE "The sum of the integers is: ",0
str3 BYTE "The highest number in the array is: ",0
str4 BYTE "The number of Odd Integers is: ",0
str5 BYTE "No Odd numbers Found!",0
array DWORD INTEGER_COUNT DUP(?)
.code
main PROC
call Clrscr
mov esi,OFFSET array
mov ecx,INTEGER_COUNT
call PromptForIntegers
call ArraySum
call DisplaySum
call Highest
call OddCount
exit
main ENDP
PromptForIntegers PROC USES ecx edx esi
mov edx,OFFSET str1 ; "Enter a signed integer"
L1: call WriteString ; display string
call ReadInt ; read integer into EAX
call Crlf ; go to next output line
mov [esi],eax ; store in array
add esi,TYPE DWORD ; next integer
loop L1
ret
PromptForIntegers ENDP
ArraySum PROC USES esi ecx
mov eax,0 ; set the sum to zero
L1: add eax,[esi] ; add each integer to sum
add esi,TYPE DWORD ; point to next integer
loop L1 ; repeat for array size
ret ; sum is in EAX
ArraySum ENDP
;-----------------------------------------------------
DisplaySum PROC USES edx
mov edx,OFFSET str2 ; "The sum of the..."
call WriteString
call WriteInt ; display EAX
call Crlf
ret
DisplaySum ENDP
;-----------------------------------------------------
Highest PROC USES esi ecx
dec ecx ; Because we'll start processing
; from the 2nd element
mov eax, [esi] ; Get the first element
loopTop:
add esi, type array ; start comparing
; from the 2nd element
; looking for the highest
cmp [esi], eax ; if (array[count]> eax)
jl lower
mov eax, [esi] ;eax = array[count]
; this is really the
; "greater" situation
lower:
loop loopTop
mov edx, offset str3
call WriteString
call WriteInt
call Crlf
ret
Highest ENDP
;-----------------------------------------------------
OddCount PROC USES eax esi edx ebx
mov eax, 0
LoopTop:
JNP Found
add esi, TYPE DWORD
Found:
inc eax
mov edx, OFFSET str4
call WriteString
call WriteInt
ret
OddCount ENDP
END main
Explanation / Answer
Dear, #ODD count is stored in EDXMOV CL,COUNT ;Move count to CL register
MOV SI,OFFSET LIST ;Offset of array is moved to SI
LOOP1: MOV EAX,[SI] ;The contents of array pointed by SI are moved to AX
ROR EAX,01 ;After ROR, the LSB specifies if the no is even/odd
JC ODD ;Jump on no carry to ODD
JMP NEXT ; Jump to next
ODD: INC EDX ; Increment EDX register
NEXT: ADD SI,02 ;Increment SI by 2
DEC CL ; Decrement CL by 1
JNZ LOOP1 ; Jump to loop1 if ZF! = 0
mov EDX, OFFSET str4
call WriteString
call WriteInt
ret
OddCount ENDP
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.