Write an assembly language function to count the number of odd integer values in
ID: 3620636 • Letter: W
Question
Write an assembly language function to count the number of odd integer values in an array of integers. The assembly language routine has the definition:int countodd( int array[], int size )
where int array[] is the array of integers to search through and int size is the number of elements in the array. The function returns an integer which is the number of odd values in the array. The first parameter, int array[], is passed by reference and is a pointer in register a1. The second, int size, is passed by value in register a2.
C Code given:
#include
#include
extern int oddcount( int array[], int size ) ;
int main( int argc, char * argv[] )
{
int intarray[] = {0, 1, 4, 7, 10, 13 } ; /* initialize array at declaration */
int size = sizeof(intarray)/sizeof(int) ; /* automagically determine number of elements in array */
int result ;
result = oddcount( intarray, size ) ;
printf( "Result for oddcount1: %d ", result ) ;
exit(0) ;
}
Explanation / Answer
START:
XOR DX,DX ;Clear the contents of DX register
MOV AX,@DATA ;Initialize data segment
MOV DS,AX
MOV CL,COUNT ;Move count to CL register
MOV SI,OFFSET LIST ;Offset of array is moved to SI
LOOP1: MOV AX,[SI] ;The contents of array pointed by SI are moved to AX
ROR AX,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 DX ; Increment DX register
NEXT: ADD SI,02 ;Increment SI by 2
DEC CL ; Decrement CL by 1
JNZ LOOP1 ; Jump to loop1 if ZF! = 0
INT 03H
END START
Output DX register gives number of odd numbers in array
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.