简体   繁体   中英

Converting C to MIPS assembly

I have this code that i've written in C which i'd like to convert to MIPS assembly, I tried using the compiler which gave a .s file but that seemed like gibberish to me. Could someone provide some help please as I'm clueless with MIPS assembly.

My code is the Collatz conjecture. My C programming isn't very good as I've only been taught Java so far. Thanks in advance

#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);
        }
   }
}

Here's a rearranging of your code, placing both input and output outside of the algorithmic routine 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);
}

For n = 6, this will be printed:

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

Cool algorithm, thanks for introducing me!

When one uses a compiler to compile there C code they must keep in mind that this is code for the machine's use and is therefore not often a good example of what a human would consider to be good readable code.

That said, here is my interpretation of your source code in MIPS assembly. This is written for use with the SPIM simulator and employs its system calls for I/O as documentated 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM