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).


LinkBack URL
About LinkBacks








Reply With Quote








Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum