Binary 如何计算二进制1';在MIPS代码中是什么?

Binary 如何计算二进制1';在MIPS代码中是什么?,binary,mips,Binary,Mips,首先,我通过键入一个C代码开始这项工作,该代码以正数计算二进制1的数量。我发现先键入C代码,然后尝试转换它更容易。我的C代码如下: #include <stdio.h> int main(void) { int num; int remainder; int one_count=0; int input_num; printf("Please enter a positive integer: "); scanf("%d",&num);

首先,我通过键入一个C代码开始这项工作,该代码以正数计算二进制1的数量。我发现先键入C代码,然后尝试转换它更容易。我的C代码如下:

#include <stdio.h>

int main(void)
{
   int num;
   int remainder;
   int one_count=0;
   int input_num;

   printf("Please enter a positive integer: ");
   scanf("%d",&num);
   input_num = num;

   while (num > 0)
   {
    remainder = num % 2;
    if(remainder == 1)
      {
       one_count++;
      }
     printf("%d\n",num);
     num = num/2;

   }
   printf("The number of 1's in %d",input_num);
   printf(" is %d\n.",one_count);

 return 0;
}
    .data
   Prompt1:.asciiz "Please enter a positive integer: "
   Prompt2:.asciiz "in "
   Prompt3:.asciiz " is "
   Result: .asciiz "The number of 1's "
    .text
  main:
    li $v0,4
    la $a0, Prompt1
    syscall

 #get user entered number
 li $v0,5
 syscall
 move $s0,$v0
 move $s1,$v0

 li $v0,4
 la $a0, Result
 syscall

 li $v0,4
 la $a0, Prompt2
 syscall


 move $a0,$s1
 li $v0,1
 syscall

 li $v0,4
 la $a0, Prompt3
 syscall

 move $a0,$t4
 li $v0,1
 syscall




onecount:

  li $t5,1            #to set remainder equal to 1
  li $t2,2            # to divide num by two


  ble $s1,$0,exit     #if $s1(num <=0), jump to exit
  rem $s2, $s0,$t2    # hold modulus in $s2

  bnez $s2, increment #if ($s2(remainder) != 0), jump to increment and do        .                                                    # one_count++
  div $s1,$t2         # num = num /2


increment:
  addi $t4,$t4,1      # one_count++ to count number of 1's in $t4 

  j onecount          #jump back to one count until num equal to zero

exit:

 li $v0,10
 syscall
#包括
内部主(空)
{
int-num;
整数余数;
int one_计数=0;
int输入_num;
printf(“请输入一个正整数:”);
scanf(“%d”和&num);
输入_num=num;
while(num>0)
{
余数=num%2;
如果(余数==1)
{
一个计数++;
}
printf(“%d\n”,num);
num=num/2;
}
printf(“在%d中的1的数量”,输入数量);
printf(“是%d\n.”,一个计数);
返回0;
}
现在,我试图在不使用指针的情况下将其转换为MIPS,但我一直得到零作为答案,尽管我认为我的逻辑是正确的。我基本上只是在C代码中完成了这一行,并试图将其转换为MIPS。我假设使用$sp,但现在我只是尝试让它计算正确的数字,然后我将尝试实现堆栈。我的MIPS代码尝试如下:

#include <stdio.h>

int main(void)
{
   int num;
   int remainder;
   int one_count=0;
   int input_num;

   printf("Please enter a positive integer: ");
   scanf("%d",&num);
   input_num = num;

   while (num > 0)
   {
    remainder = num % 2;
    if(remainder == 1)
      {
       one_count++;
      }
     printf("%d\n",num);
     num = num/2;

   }
   printf("The number of 1's in %d",input_num);
   printf(" is %d\n.",one_count);

 return 0;
}
    .data
   Prompt1:.asciiz "Please enter a positive integer: "
   Prompt2:.asciiz "in "
   Prompt3:.asciiz " is "
   Result: .asciiz "The number of 1's "
    .text
  main:
    li $v0,4
    la $a0, Prompt1
    syscall

 #get user entered number
 li $v0,5
 syscall
 move $s0,$v0
 move $s1,$v0

 li $v0,4
 la $a0, Result
 syscall

 li $v0,4
 la $a0, Prompt2
 syscall


 move $a0,$s1
 li $v0,1
 syscall

 li $v0,4
 la $a0, Prompt3
 syscall

 move $a0,$t4
 li $v0,1
 syscall




onecount:

  li $t5,1            #to set remainder equal to 1
  li $t2,2            # to divide num by two


  ble $s1,$0,exit     #if $s1(num <=0), jump to exit
  rem $s2, $s0,$t2    # hold modulus in $s2

  bnez $s2, increment #if ($s2(remainder) != 0), jump to increment and do        .                                                    # one_count++
  div $s1,$t2         # num = num /2


increment:
  addi $t4,$t4,1      # one_count++ to count number of 1's in $t4 

  j onecount          #jump back to one count until num equal to zero

exit:

 li $v0,10
 syscall
.data
Prompt1:.asciiz“请输入一个正整数:”
提示2:.asciiz“在”
提示3:.asciiz“是”
结果:.asciiz“1的数量”
.文本
主要内容:
李$v0,4
洛杉矶$a0,提示1
系统调用
#获取用户输入的号码
李$v0,5
系统调用
移动$s0,$v0
移动$s1,$v0
李$v0,4
la$a0,结果
系统调用
李$v0,4
洛杉矶$a0,提示2
系统调用
移动$a0,$s1
李$v0,1
系统调用
李$v0,4
洛杉矶$a0,提示3
系统调用
移动$a0,$t4
李$v0,1
系统调用
一个计数:
li$t5,1#将余数设置为1
li$t2,2#将num除以2

ble$s1,$0,退出#如果在
onecount
循环中从未修改$s1(num 1)
$s1
。2)
lt
表示小于,但您可能想要的是
le
@Michael,那么您是说(blt$s1,$s0,exit)应该是“ble”吗?如果是的话,那不就是检查输入的数字是否等于零且小于零吗?这是一个很好的捕获,因为我确实需要检查它,但最终还是得到了零作为输出。只要
num>0
,C
while
循环就会继续。相应的“如果`will'be
num@Michael退出,那么我添加了我认为每行代码正在做的事情的注释。你能告诉我有没有线路错了吗?感谢您帮助实现除法和错误行的冗余。1)
$s1
onecount
循环中从未被修改。2)
lt
表示小于,但您可能想要的是
le
@Michael,那么您是说(blt$s1,$s0,exit)应该是“ble”吗?如果是的话,那不就是检查输入的数字是否等于零且小于零吗?这是一个很好的捕获,因为我确实需要检查它,但最终还是得到了零作为输出。只要
num>0
,C
while
循环就会继续。相应的“exit if`将是
num@Michael,因此我添加了我认为每行代码正在执行的操作的注释。您能告诉我是否有任何一行是错误的吗?感谢您帮助解决除法和错误行的冗余问题。