Question

Complete count_bits function. You are given an 32-bits integer stored in $t0. Count the number of...

Complete count_bits function. You are given an 32-bits integer stored in $t0. Count the number of 1's in the given number. For example: 1111 0000 should return 4

j main
###############################################################
# Data Section
.data
#


new_line: .asciiz "\n"
space: .asciiz " "
double_range_lbl: .asciiz "\nDouble range (Decimal Values) \nExpected output:\n1200 -690 104\nObtained output:\n"
swap_bits_lbl: .asciiz "\nSwap bits (Hexadecimal Values)\nExpected output:\n75757575 FD5775DF 064B9A83\nObtained output:\n"
count_bits_lbl: .asciiz "\nCount bits \nExpected output:\n20 24 13\nObtained output:\n"

swap_bits_test_data: .word 0xBABABABA, 0xFEABBAEF, 0x09876543
swap_bits_expected_data: .word 0x75757575, 0xFD5775DF, 0x064B9A83

double_range_test_data: .word 945, -345, 0, -3, 55
double_range_expected_data: .word 1200, -6, 104

hex_digits: .byte '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'

###############################################################
# Text Section
.text
# Utility function to print hexadecimal numbers
print_hex:
move $t0, $a0
li $t1, 8 # digits
lui $t2, 0xf000 # mask
mask_and_print:
# print last hex digit
and $t4, $t0, $t2
srl $t4, $t4, 28
la $t3, hex_digits
add $t3, $t3, $t4
lb $a0, 0($t3)
li $v0, 11
syscall
# shift 4 times
sll $t0, $t0, 4
addi $t1, $t1, -1
bgtz $t1, mask_and_print
exit:
jr $ra
###############################################################
###############################################################
###############################################################
# PART 1 (Count Bits)
#
# You are given an 32-bits integer stored in $t0. Count the number of 1's
#in the given number. For example: 1111 0000 should return 4
count_bits:
move $t0, $a0
############################## Part 1: your code begins here ###


############################## Part 1: your code ends here ###
move $v0, $t0
jr $ra

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM:

.data
   prompt: .asciiz "Enter a number: "
   output: .asciiz "Number of 1's = "
  
.text
main:
   # Prompt to the user to enter a number
   li $v0,4
   la $a0,prompt
   syscall
  
   # Reading input from user
   li $v0,5
   syscall
   move $a0,$v0
  
   # calling count_bits function  
   jal count_bits
  
   # Storing the returned value in $t0
   move $t0,$v0
  
   # Displaying the output to the user
   li $v0,4
   la $a0,output
   syscall
   li $v0,1
   move $a0,$t0
   syscall
  
   # Exiting the program
   li $v0,10
   syscall

# count_bits function
count_bits:
   move $t0,$a0       # Moving function argument to $t0
   li $t1,0       # Count variable
   li $t3,32       # Loop termination condition variable
again:  
   and $t2,$t0,1       # Comparing LSB with 1
   bne $t2,1,count       # If not equal branch to count label
   addi $t1,$t1,1       # If equal increment count variable value
count:   srl $t0,$t0,1       # Shift the input number by 1
   addi $t3,$t3,-1       # Decreasing the loop termination variable value by 1
   bnez $t3,again       # if loop termination not zero continuing the counting process
   move $v0,$t1       # Storing the count value in $v0
   jr $ra           # Returning back to the main function

Please refer to the screenshot of the program for indentation of code:

.data prompt: .asciiz Enter a number: output: .asciiz Number of ls = .text main: ooco Jou WN R # Prompt to the user to e

OUTPUT:

Printing 10 numbers: 12 13 45 46 78 79 31 64 97 21 -- program is finished running -- Enter a number: 5 Number of 13 = 2 -- p

Add a comment
Know the answer?
Add Answer to:
Complete count_bits function. You are given an 32-bits integer stored in $t0. Count the number of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT