In MIPS assembly language Write a function named bitcount th
In MIPS assembly language
Write a function named bitcount that returns the number of 1-bit in the input argument that is an unsigned 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 unsigned integer, calls bitcount and then displays the result. For example, if the user gives 17 as the input, the program displays \"The number of 1-bit is 2\".
Thanks in advance!
Solution
Solution:
.data
msg1: .asciiz \" Please enter the value:\"
msg2: .asciiz \" the number of 1-bit is \"
.text
main:
li $v0,4
la $a0, msg1
jal count_ bit
syscall
li $v0,4 #instruction to print the string
la $a0, msg2
add $t0, $0, $v0
li $v0,1 #instruction to print the integer
li $t0,5
syscall
count_bit: addi $sp, $sp, -8 # instruction to create stack space
sw $ra, 4($sp) # instruction to save return address
sw $s0, 0($sp) # instruction to save $s0
add $v0, $0, $0 # instruction to initialize $v0 to 0
beq $a0, $0, close # instruction to check whether user input suppose n==0, if true,return
andi $s0, $a0, 1 # instruction for bit = n & 0n1
srl $a0, $a0, 1 # instruction to shift right logical, n >> 1
jal count_bit # instruction for recursive call
add $v0, $v0, $s0
close: lw $ra, 4($sp) # instruction to restore $ra
lw $s0, 0($sp) # instruction to restore $s0
addi $sp, $sp, 8 # instruction to restore stack
jr $ra

