简体   繁体   中英

translating C to mips Assembly

I have this C program that is used to identify odd numbers:

#include <math.h>
#include <stdio.h>

int impar (int x)
{
    if (x%2 != 0)
        return 1;
    else 
        return 0;
}

void test ()
{
    int x;
    while(scanf("%d", &x) != EOF)
    {
        int y = impar(x);
        printf("%d\n", y);
    }
}

int main (void)
{
    test();
    return 0;
}               

and I need to convert it to MIPS assembly. So far I got this:

.data
.globl main
linha1: .asciiz "Numero a inserir\n"
.text
body:
    la $a0, linha1      # carrega o endereco da linha1 para syscall
    li $v0,4            # servico de print da string
    syscall

    main:
    li $v0,7            # servico para ler um inteiro
    syscall
    div $t0, $v0, 2 # divisao do x por 2
    mfhi $t9 #mover o resto para t9
    li $t3, 0
    bne $t3, $t9, if
    beq $t3, $t9, else


    if:
    add $t6, $v0, 1
    li $v0,3            # servico para imprimir um inteiro
    syscall
    li $v0,10           # servico para terminar o programa

    else: 
    add $t6, $v0, 0     
    li $v0,3            # servico para imprimir um inteiro
    syscall
    li $v0,10           # servico para terminar o programa

I am having problems using the number that is on input, since the output is always 0.00.0

Any ideas what is wrong here?

Syscalls 3 and 7 are print_double and read_double . That doesn't match your C code, which is using the %d format specifier (ie decimal integer). If you intended to read and write integers you should be using syscalls 1 and 5 instead.

And if you use syscall 1 to print an integer you should place the value to print in register $a0 .

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