views:

44

answers:

0
 Program: Ones counter
       The user will type in an integer.
       The program will pass the integer to a function that will count the
       number of binary bits that are "1".

   Now here is the C+ pseudocode I'm using to translate to MIPS language but when I 
   tried to use it in my program it won't run. I'm I using the right C+ pseudocode for
   translation? Below is the routine program I created.
#################################################################
Function CountBits(int)

    counts the one bits in an integer.
    for example, it the input is "15" the output is "4"

    Arguments:
       a0 input integer

    Returns
       v0 the bit count of 1 bits
    Other Register usage:
       t0-t7 as required
       S0-S7 as required
-------------------------------------
 int Countbits(unsigned int a0)
 {
    int v0 = 0;
    do {
        v0 += a0 & 1;
        a0 >>= 1;
    }while (a0);
    return v0;
 }
-------------------------------------

CountBits:

    #{translated code goes here!}




###############################################################################
do not modify below this line
        .data

prompt: .asciiz "\nType in an integer:  "
ans:    .asciiz "The number of one bits is: "
        .text
        .globl  main
main:   la      $a0,prompt              # do{
        syscall $print_string           # cout<< prompt
        syscall $read_int               # cin >> N;
        move    $a0,$v0
        jal     CountBits               #
        move    $s0,$v0                 #  s0= CountBits(N)
        la      $a0,ans
        syscall $print_string           #  cout << ans << s0;
        move    $a0,$s0
        syscall $print_int   
        b       main                    # }while (true)