How do I create an int variable to store user input? I tried the code below, but when I put in a number it comes out with some random 6 digit number. My assignment is:
Write a complete, commented, stand-alone MIPS program
to include the same function as this C code procedure:
int assn9(int K){
if(K<0){return(-K);}
if(K<7){return(K-2);}
return((K-1)*2));
}
Name the SPIM source file assn9.s (submit this file)
[] Prompt the user for input K,
be sure to echo the input and tell the user how the program ends.
[] Call the (assembler version of) the function assn9()
above to find the value of assn9(K)
[] Print the result and go back to the initial prompt.
[] When K=0, exit the program.
.data #let processor know we will be submitting data to program now
insert_into:
.word 4 #make a 4 byte (32 bit) space in memory for an int with address insert_into
Ask_Input:
.asciiz "Please enter a number: \n" #asks user for an integer
Tell_Output:
.asciiz "You typed in: " #message to display the input integer
.text #beginning of code
main: #main function (user interface)
la $a0, Ask_Input #load address Ask_Input from memory and store it into arguement register 0
li $v0, 4 #loads the value 4 into register $v0 which is the op code for print string
syscall #reads register $v0 for op code, sees 4 and prints the string located in $a0
la $a0, insert_into #sets $a0 to point to the space allocated for writing an int
la $a1, insert_into #gets the length of the space in $a1 so we can't go over the memory limit
li $v0, 5 #load op code for getting an int from the user into register $v0
syscall #reads register $v0 for op code, sees 8 and asks user to input a string, places string in reference to $a0
la $a0, Tell_Output #load address Tell_Output from memory and store it into arguement register 0
li $v0, 4 #loads the value 4 into register $v0 which is the op code for print string
syscall #reads register $v0 for op code, sees 4 and prints the string located in $a0
la $a0, insert_into #load address insert_into from memory and store it into arguement register 0
li $v0, 1 #loads the value 1 into register $v0 which is the op code for print int
syscall #reads register $v0 for op code, sees 1 and prints the integer located in $a0
li $v0, 10 #loads op code into $v0 to exit program
syscall #reads $v0 and exits program