Jump to content




Recent Status Updates

View All Updates

Developed by TechBiz Xccelerator
Photo
- - - - -

Mips Assembly: Take user input and write to the console

user input write to assembly mips console

  • Please log in to reply
8 replies to this topic

#1 morefood2001

morefood2001

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,011 posts

Posted 20 September 2008 - 06:52 PM

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:

#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

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.

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

Attached Files

  • Attached File  text.asm   1.9KB   2395 downloads

  • 1
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#2 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 20 September 2008 - 07:33 PM

I didn't know you knew ASM. Nice work!
  • 0

#3 morefood2001

morefood2001

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,011 posts

Posted 20 September 2008 - 08:06 PM

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.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#4 John

John

    CC Mentor

  • Moderator
  • 4,450 posts
  • Location:New York, NY

Posted 20 September 2008 - 10:28 PM

Nice, will this work under linux?
  • 0

#5 TkTech

TkTech

    The Crazy One

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1,144 posts
  • Location:Ottawa, Ontario

Posted 20 September 2008 - 10:51 PM

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!
  • 0
Helpful CODECALL Links: Join Us, Guidelines, FAQ, Post a Tutorial

#6 v0id

v0id

    Retired

  • Retired Mod
  • PipPipPipPipPipPipPipPip
  • 2,313 posts

Posted 20 September 2008 - 11:11 PM

You can however find many MIPS emulators, so if you really want to try it out, go find one of them.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#7 morefood2001

morefood2001

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,011 posts

Posted 21 September 2008 - 06:35 AM

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. :/
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#8 MeTh0Dz

MeTh0Dz

    CC Leader

  • Just Joined
  • PipPipPipPipPipPipPip
  • 1,726 posts

Posted 21 September 2008 - 09:52 AM

Like I said just message me if you need help...
  • 0

#9 ldappro

ldappro

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 20 October 2010 - 07:50 AM

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?
  • 0





Also tagged with one or more of these keywords: user input, write to, assembly, mips, console

Powered by binpress