Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

in MARS MIPS simulator assembly language Write a function named bitcount that re

ID: 3691221 • Letter: I

Question

in MARS MIPS simulator assembly language

Write a function named bitcount that returns the number of 0-bit in the input argument that is an integer (contained in register $a0), and a test driver (main function) to test the function bitcount. The test driver prompts the user for a decimal integer, calls bitcount and then displays the result. For example, if the user gives -4 as the input, the program displays "The number of 0-bit is 2". Submit the source code as well as the screen shot showing the program runs correctly with several test runs (refer to the syllabus for requirements of programming assignments.)

Explanation / Answer

bitCount:

#include <iostream>
#include <stdio.h>

using namespace std;
int bitCount(unsigned long c)
{
   int bitcount = 0;

   __asm__( "movl %1, %%eax; " //code into eax
           "popcnt %%eax, %%eax;"// call popcnt instruction
           "movl %%eax, %0;"// ebx into bitcount
           :"=r"(bitcount)// output
           :"r"(c)// input
           :"%eax","%ebx","%ecx","%edx"// clobbered register
   );
   return 4-bitcount;
}


int main() {
   // 0xA = 1010 -- > 2
   // 0x3F2 = 1111110010 -- > 7

   unsigned long code ;
  
   code=0x3;
   printf("entered number is = %d ",code);
   printf("number of 0's %d", bitCount(code));
   return 0;

}

output: