1

I understand the basic functions of MIPS, but I do not understand how to declare variables and especially do not understand how to write the equivalent of scanf()/printf() in MIPS (can't find anything about them online when I search). Here is the code I am trying to translate for now:

#include <stdio>
int main(int argc, char* argv[]) 
{ 
 unsigned long int n; 
 scanf("%d", &n); 
 printf("%lu",fact(n)); 
}

Any guidance?

user3251142
  • 175
  • 7
  • 16

2 Answers2

1

You are probably looking for the MIPS simulator system calls (syscalls). Here are the SPIM syscalls and the MARS syscalls.

syscall 7 is read double, which is roughly equivalent to scanf("%d", &n)

syscall 1 is print integer, which is equivalent to printf("%u",n)

li   $v0, 1    # service 1 is print integer
move $a0, $t0  # move register to be printed into argument register $a0
syscall
markgz
  • 6,054
  • 1
  • 19
  • 41
0

You probably do not want to re-implement scanf in assembly. It is much more productive to implement in C or other high-level language. Even better would be to grab an open source implementation, like in glibc.

Here is the source code for scanf() in glibc: http://fossies.org/dox/glibc-2.19/scanf_8c_source.html

Here is a GPL-like implementation: http://mirror.fsf.org/pmon2000/3.x/src/lib/libc/scanf.c

wallyk
  • 56,922
  • 16
  • 83
  • 148