Count 1 in integers using the mips assembly language (without any flow of control instructions)

I'm currently working on a program that counts the number 1 in the binary representation of an integer, where the user enters an integer. I need to make it so that the program runs from top to bottom, so this means no loops or threads of any command. However, I am very new to Mips and assembler, and currently I'm struggling with how to do this.

I think you can use the instructions srlvand / or sllvfor this with some multiplication, but I do not know where to start.

+3
source share
1 answer

The function that you describe is called the Hamming weight.

C . ( 32 ):

//This uses fewer arithmetic operations than any other known  
//implementation on machines with fast multiplication.
//It uses 12 arithmetic operations, one of which is a multiply.
int popcount_3(uint32_t x) {
    const uint32_t m1  = 0x55555555; //binary: 0101...
    const uint32_t m2  = 0x33333333; //binary: 00110011..
    const uint32_t m4  = 0x0f0f0f0f; //binary:  4 zeros,  4 ones ...
    const uint32_t h01 = 0x01010101; //the sum of 256 to the power of 0,1,2,3...

    x -= (x >> 1) & m1;             //put count of each 2 bits into those 2 bits
    x = (x & m2) + ((x >> 2) & m2); //put count of each 4 bits into those 4 bits 
    x = (x + (x >> 4)) & m4;        //put count of each 8 bits into those 8 bits 
    return (x * h01)>>24;  //returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24) + ... 
}

MIPS :

main:

    #read in int x for Hamming Weight
    addi $v0 $zero 5
    syscall

    lui $t5 0x0101 #$t5 is 0x01010101
    ori $t5 0x0101
    lui $t6 0x5555 #$t6 is 0x55555555
    ori $t6 0x5555
    lui $t7 0x3333 #$t7 is 0x33333333
    ori $t7 0x3333
    lui $t8 0x0f0f #$t8 is 0x0f0f0f0f
    ori $t8 0x0f0f

    # x -= (x>>1) & 0x55555555
    srl $t0 $v0 1
    and $t0 $t0 $t6
    sub $v0 $v0 $t0

    # x = (x & 0x33333333) + ((x >> 2) & 0x33333333)
    and $t0 $v0 $t7
    srl $t1 $v0 2
    and $t1 $t1 $t7
    add $v0 $t0 $t1

    # x = (x + (x >> 4)) & 0x33333333
    srl $t0 $v0 4
    add $t0 $v0 $t0
    and $v0 $t0 $t8

    # output (x * 0x01010101) >> 24
    mul $v0 $v0 $t5
    srl $a0 $v0 24
    li $v0 1
    syscall

    jr $ra
+2

All Articles