If you have just started Mips Assembly, after learning syntax first basic program you learn is either printer a “Hello world” or reading and print integer or character. In this section, we will learn how to print an integer from a register and how to read an integer from the keyboard. So let’s get started!
MIPS Print Integer from Register
First, consider the following MIPS program to print an integer from a register:
[code].data
.code
li $to,4
li $v0,1
move $a0,$t0
syscall
li $v0,10
syscall
Explanation
In the above program, we have taken a register $t0 to store an integer. Then we used a syscall service to print that integer. To print that integer we have to move that integer to a register called $a0. The $a0 register is used to store a string, character or integer output.
MIPS Read Integer from Keyboard/User
To read an integer in MIPS we have to use a syscall service for reading an integer. Consider the example to read an integer.
[code].data
msg1: .asciiz “Enter a number: ”
.code
li $v0,4
la $a0,msg1
syscall
li $v0,5
syscall
li $v0,10
syscall
Explanation
In the above program, we have used a syscall service “5” for reading an integer. We have used a label “msg1” to ask a user to enter a number. In case you don’t want to print the message and just want to read the number just remove the msg1 lin in .data section and in .code section remove the corresponding code for printing the msg.
Let me know it helped you or not. If you have any problem feel free to ask in the comment box. Comment to show me that you are alive!