+ Reply to Thread
Results 1 to 9 of 9

Thread: Mips Assembly: Take user input and write to the console

  1. #1
    Join Date
    Jan 2008
    Posts
    1,725
    Blog Entries
    4
    Rep Power
    29

    Mips Assembly: Take user input and write to the console

    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:

    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
    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 Files

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Mips Assembly: Take user input and write to the console

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

  4. #3
    Join Date
    Jan 2008
    Posts
    1,725
    Blog Entries
    4
    Rep Power
    29

    Re: Mips Assembly: Take user input and write to the console

    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.

  5. #4
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Mips Assembly: Take user input and write to the console

    Nice, will this work under linux?

  6. #5
    TkTech's Avatar
    TkTech is offline The Crazy One
    Join Date
    Jun 2006
    Location
    Canada
    Posts
    1,412
    Blog Entries
    1
    Rep Power
    31

    Re: Mips Assembly: Take user input and write to the console

    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!

  7. #6
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42

    Re: Mips Assembly: Take user input and write to the console

    You can however find many MIPS emulators, so if you really want to try it out, go find one of them.

  8. #7
    Join Date
    Jan 2008
    Posts
    1,725
    Blog Entries
    4
    Rep Power
    29

    Re: Mips Assembly: Take user input and write to the console

    Quote Originally Posted by v0id View Post
    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. :/

  9. #8
    Join Date
    May 2008
    Posts
    2,126
    Blog Entries
    1
    Rep Power
    33

    Re: Mips Assembly: Take user input and write to the console

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

  10. #9
    ldappro is offline Newbie
    Join Date
    Oct 2010
    Posts
    1
    Rep Power
    0

    Question Re: Mips Assembly: Take user input and write to the console

    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?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. General how to write this prog in MIPS(Mars)??
    By Ebda3 in forum Assembly
    Replies: 51
    Last Post: 05-03-2011, 03:47 AM
  2. General MIPS Assembly problem
    By srose in forum Assembly
    Replies: 5
    Last Post: 10-26-2010, 12:54 PM
  3. C/mips assembly.
    By rescueme in forum Assembly
    Replies: 4
    Last Post: 02-15-2009, 06:59 AM
  4. Mips Assembly: Take user input and write to the console
    By morefood2001 in forum Tutorials
    Replies: 7
    Last Post: 09-21-2008, 10:52 AM
  5. how to write the code for user to input command ?
    By worried_student in forum C and C++
    Replies: 3
    Last Post: 12-26-2007, 06:11 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts