将C转换为MIPS组件

时间:2014-01-06 20:57:00

标签: c assembly mips

我有这个代码,我用C编写,我想转换为MIPS程序集,我尝试使用编译器,它给了.s文件,但这对我来说似乎是胡言乱语。有人可以提供一些帮助,因为我对MIPS组装毫无头绪。

我的代码是Collat​​z猜想。我的C编程不是很好,因为我到目前为止只学过Java。提前致谢

#include<stdio.h>

int main()
{
    int n;

    printf("Enter an integer\n");
    scanf("%d", &n);


    while (n != 1)
    {
        if(n == 1)
        {   
            printf("N == 1");
        }
        else if((n%2)==0)
        {
            printf("Integer is even : %d\n", n);
            n = n/2;
        }
        else
        {
            n = 3*n + 1;
            printf("Integer has been multipled by 3 and added by 1 : %d\n", n);
        }
   }
}

2 个答案:

答案 0 :(得分:3)

这是重新排列代码,将输入和输出都放在算法例程collatz()之外。

#include <stdio.h>

int steps;

void print(int n)
{
    printf("%d ", n);
    steps++;
}

void collatz(int n)
{
    print(n);
    while (n != 1)
    {
        if ((n % 2) == 0)
        {
            n = n / 2;
            print(n);
        }
        else
        {
            n = 3 * n + 1;
            print(n);
        }
    }
}

int main()
{
    int n;

    printf("Enter an integer\n");
    scanf("%d", &n);

    steps = 0;
    collatz(n);
    printf("in %d steps.\n", steps);
}

对于n = 6,将打印:

6 3 10 5 16 8 4 2 1 in 9 steps.

酷算法,谢谢你介绍我!

答案 1 :(得分:1)

当使用编译器编译C代码时,他们必须记住这是机器使用的代码,因此通常不是人类认为是良好可读代码的好例子。

那就是说,这是我对MIPS汇编中源代码的解释。这是为了与SPIM模拟器一起使用而编写的,并将其I / O系统调用用作已记录的here

.data  

prompt:        .asciiz "Enter an integer\n"
neq1Message:   .asciiz "N == 1"
nevenMessage:  .asciiz "Integer is even : "
noddMessage:   .asciiz "Integer has been multiplied by 3 and added by 1 : "

.text  
main:

    #print prompt
    la $a0 prompt
    addi $v0 $zero 4
    syscall

    #read integer into $t0
    addi $v0 $zero 5
    syscall
    move $t0 $v0 

    loop:

        # quit loop if n == 1
        addi $t1 $zero 1
        beq  $t0 $t1 loopEnd

        #skip to even if n != 1
        addi $t1 $zero 1
        bne  $t0 $t1 neven

        neq1:

            # print n is 1
            la $a0 neq1Message
            addi $v0 $zero 4
            syscall

            j loop

        neven:

            # skip to odd if n not even
            andi $t1 $t0 1
            bne  $t1 $zero nodd

            # print n is even
            la $a0 nevenMessage
            addi $v0 $zero 4
            syscall

            # print n
            move $a0 $t0
            addi $v0 $zero 1
            syscall

            # print newline
            addi $a0 $zero 10
            addi $v0 $zero 11
            syscall

            # n = n / 2
            srl $t0 $t0 1

            j loop

        nodd:

            # n = 3 * n + 1
            addi $t1 $zero 3
            mul  $t0 $t0 $t1
            addi $t0 $t0 1

            # print n is odd
            la $a0 noddMessage
            addi $v0 $zero 4
            syscall

            # print n
            move $a0 $t0
            addi $v0 $zero 1
            syscall

            # print newline
            addi $a0 $zero 10
            addi $v0 $zero 11
            syscall

            j loop

    loopEnd:

jr $ra