This is probably going to be the easiest tutorial that I ever write, but I didn't see any tutorials on writing a "hello world" application in mips assembly, or anything for that matter on mips. I am going to change it a bit and take in an input like "Hello World" and print it out. You can easily delete parts of my code to achieve the simple hello world if you wish.
To program on a mips processor under windows, we normally simulate the platform. To do this, I am going to use a program called mars which is available here
With mars running, goto file> new. A new edit window should now open.
The mips processor takes 32 bit instructions containing 3 types of instructions. The 3 varities are Immediate, Register, and Jumps. All of the registers take a 6 bit operation code, then the remaining 26 bits are used for the instruction. For this tutorial, I will be using only register and immediate type instructions.
So I am going to enter the following code into the edit interface of my coding interface:
I saved the file as text.asm, then went to assemble, clicked the run code button and it works as expected. So there is an easy way to read a user input and output it back to the terminal using the mips instruction set.Code:#A Program that asks for your input then outputs what you said. #Author: Philip Matuskiewicz #Mips Code .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 a word with address insert_into Ask_Input: .asciiz "\Please Enter a String to Print\n" #in unused memory store this string with address Ask_Input Tell_Output: .asciiz "\You typed in: " #in unused memory store this string with address Tell_Output .text #enables text input / output, kind of like String.h in C++ main: #main function is always called in any mips program, so the program will start here with actual assembly code 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 a word la $a1, insert_into #gets the length of the space in $a1 so we can't go over the memory limit li $v0, 8 #load op code for getting a string 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, 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 li $v0, 10 #loads op code into $v0 to exit program syscall #reads $v0 and exits program
Attached is text.asm (rename to .mips for some programs like spim in linux), and Mars.jar (in a zip file due to 2mb upload restriction).
I didn't know you knew ASM. Nice work!
I know very little about assembly, other than its really tedious to use compared to c++ lol. Echo becomes 4 or 5 lines of assembly lol.
Nice, will this work under linux?
No /=
MIPS is a specific architecture - if you want to learn more, I read this great intro to asm, its a little green book and I can't remember the name....
Edit: Hoah google! "Introduction to RISC Assembly Language Programming By John Waldron" Excellent book!
You can however find many MIPS emulators, so if you really want to try it out, go find one of them.
If you download Mars.jar (its zipped), it will run under any os that has the Java Runtime Environment installed
Mips is one of several architectures, and its usually found in game console systems among other systems.
Assembly for x86 is very confusing to me at this point, and it will be a long time before I can write anything that would work on a normal computer. :/
Like I said just message me if you need help...
Interesting. I'm using MARS MIPS simulator. I want to create a MIPS assembly program that plays a midi song ($v0 = 33). But I want to accept 2 inputs:
1. tone (low,medium, and high) that will be used to derive a base integer value for $a0 (48,60,72)
2. instrument (piano,strings,pipe) that will be used to derive an integer value for $a2 (0,40,72)
Once the inputs are validated I need to either set $a0 and $a2. If the inputs don't match the list of available strings, I want to exit with an error.
I know how to take the inputs with system service 8. I know how to do a byte comparison of the input strings and play the song with system service 33. What I'm having a problem with is how to temporarily store the $a0 and $a2 values in memory and then recall them later. Can you help?
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks