Reputation: 233
for my school project i have to make a simple program in MARS 4.5 in assembly language. So far what is confusing me is storing numbers. In this program i have to store 2 numbers of what the user inputs. so far this is my code...
.data
.text
main:
jal GetUserInput
li $v0, 10
syscall
GetUserInput:
#get the input
li $v0, 5
syscall
#move the input
move $t0, $v0
#display the input
li $v0, 1
move $a0, $t0
syscall
so in the main function it will run the getuserinput function, it will then get the input and move it to $t0. does this mean in c# terms its basicily a variable "int $t0 = 10" providing 10 is the number i inputted and i can change that number later on in the program? now if i wanted 2 stored numbers, what would i store the other one in, $t1? im new to assembly language
Upvotes: 0
Views: 2066
Reputation: 31
What your function is doing is taking an input, temporarily saving it, and printing that input on the console. To store multiple numbers, you may need to form a function that returns a specific integer, which is fairly simple.
It is common on MIPS processors to use $v0
for function return values. Of course, you're free to come up with any calling convention you want for your own functions, but unless you have a very good reason not to I'd say stick with $v0
for return values.
In order to call a function, use jal
, because that's what sets up the $ra
register so that you later can return with jr $ra
.
So in order to store two integers, your code will be as follows:
.data
.text
main:
jal GetUserInput
add $s0,$s0,$v0
li $v0, 1
move $a0,$s0
syscall
jal GetUserInput
add $s1,$s1,$v0
li $v0, 1
move $a0,$s0
syscall
li $v0, 10 #End program
syscall
GetUserInput:
li $v0, 5 #Get the input
syscall
jr $ra
Upvotes: 0
Reputation: 1
To store any value you don't necessarily need to use registers, you can store as many value in as many variables you make inside the .data section. This not only helps you write code efficiently, but also makes registers free, which can be used without having fear of losing values.
.data
# Declare as many variables as you want, so that you have more registers in hand
num1: .word 0 # Number data type initially setting equals to zero
num2: .word 0
line_feed: .asciiz "\n" # Line feed in order to change line.
print_command: .asciiz "Showing you number: "
ask_user: .asciiz "Enter number: "
.text
.globl mian
main:
# Taking input form user
jal take_input
sw $v0, num1 # Storing user input to variable.
jal take_input
sw $v0, num2 # Storing user input to variable.
# Printing first number
li $v0, 4
la $a0, print_command
syscall
lw $t0, num1
li $v0, 1
move $a0, $t0
syscall
li $v0, 4
la $a0, line_feed
syscall
# Printing second number
li $v0, 4
la $a0, print_command
syscall
lw $t0, num2
li $v0, 1
move $a0, $t0
syscall
li $v0, 4
la $a0, line_feed
syscall
# Exit Block In order to prevent infinite loop writing it befoere other procedures.
exit:
li $v0, 10
syscall
# A porcedure to take input form user
take_input:
li $v0, 4
la $a0, ask_user
syscall
li $v0, 5
syscall
jr $ra
Upvotes: 0
Reputation: 18503
You may store the numbers in various registers (such as t0, t1, ...) but you may also store the numbers in memory.
If you use a C compiler for MIPS and switch off optimization the resulting code will probably store the values in memory.
In fact the MIPS CPU is one of the CPUs that provides the largest "freedom" for the programmer (or compiler).
Upvotes: 1