.include \"nios macros.s\" .text .equ TESTNUM, 0x90abcdef /* The number to be te
ID: 2267892 • Letter: #
Question
.include "nios macros.s" .text .equ TESTNUM, 0x90abcdef /* The number to be tested */ .global_start start movia 17, TEST_NUM/* Initialize r7 with the number to be tested */ movr4, r7 /* Copy the number to r4 */ STRING COUNTER movr2, r0 * Initialize the counter to zero */ STRING COUNTER LOOP /* Loop until the number has no more ones beqr4, r0, END STRING COUNTER srli andr4, r4. r5 addi r2, r2, 1 r5, r4, 1 Calculate the number for ones by shifting the */ number by 1 and anding the result with itself. */ /* Increment the counter. */ r' STRING COUNTER LOOP END_STRING.COUNTER movr16, r2 * Store the result into r16/ END END /* Wait here once the program has completed */ .endExplanation / Answer
C Language Code:
#include <stdio.h>
int main (void)
{
//mov r7,TEST_NUM
unsigned int TEST_NUM = 0x90ABCDEF;
//mov r4,r7
unsigned int Register4 = TEST_NUM;
//mov r2,r0
unsigned int Counter = 0;
unsigned int Result;
//beq r4,r0,END_STRING_COUNTER
//br STRING_COUNTER_LOOP
while(Register4)
{
//srli r5,r4,r1 -> r5 = r4 >> 1
//and r4,r4,r5 -> r4 = r4 & r5
Register4 &= Register4 >> 1; //Register4 = Register4 & Register4 >> 1;
//addi r2,r2,1
Counter++;
}
//mov r16,r2
Result = Counter;
// END:
// br END
while(1){}
}
Note: To count number of bit sets in a number , subtract number by 1 & logic anding the result with itself
Example: Register4 &= Resister4 -1;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.