如何将此代码转换为MIPS?

时间:2015-10-05 00:18:45

标签: mips

C ++代码:

#include <iostream>
using namespace std;

int main()
{
    int num;
    int sum = 0;

    cout << " Enter a number : ";
    cin >> num;

    while ( num > 0 ) {
    sum += num % 10;
    num /= 10;
}

如何将其转换为MIPS?

我知道,我可以使用$HI$LO寄存器,但我无法实现此功能。

.data 
error: .asciiz "The Number you entered is not valid"
input: .asciiz "Please enter a number"

.text 

main:
#GET NUMBER FROM USER
li $v0, 4 #load syscall print string
la $a0, input  #load adress of str1 into $a0
syscall   #make the syscall

#SAVE NUMBER
li $v0, 5 #load syscall read_int into $v0
syscall   #make the syscall
move $s0, $v0 #move the number read into $t0 now $t0=n

#make sure number is between 100 and 400
addi $t1, $zero, 400
addi $t2, $zero, 100 
ble $s0, $t2, EXIT 
bge $s0, $t1, EXIT 



loop: 
div $s0, $s1 
add $s2, $s2, $LO 
add $s0, $HI, $zero 
bgt $s0, $zero, loop 




end: 
li $v0, 10 
syscall


EXIT: 
li $v0, 4 #load syscall print string
la $a0, error  #load adress of error into $a0
syscall   #make the syscall

1 个答案:

答案 0 :(得分:1)

当您在MIPS中执行div时,商进入$LO,余数进入Sum: s2 Num: s0 10: s1 While: # while num >0 div $s0, $s1. # $s0/$s1 num/10 add $s2, $s2, $LO # sum += num%10 add $s0, $HI, $zero # num /= 10 bgt $s0, $zero, While # if num >0 loop again 。如果while循环是您遇到的问题,那应该会有所帮助。

rails g model notification body: text, user_id: integer, is_seen: boolean 

其余的只是一个分支大于。

的循环