+ Reply to Thread
Results 1 to 8 of 8

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

  1. #1
    Guru morefood2001 is just really nice morefood2001 is just really nice morefood2001 is just really nice morefood2001 is just really nice
    Join Date
    Jan 2008
    Posts
    1,724
    Blog Entries
    4

    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

  2. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

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

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

  3. #3
    Guru morefood2001 is just really nice morefood2001 is just really nice morefood2001 is just really nice morefood2001 is just really nice
    Join Date
    Jan 2008
    Posts
    1,724
    Blog Entries
    4

    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.

  4. #4
    Co-Administrator John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John's Avatar
    Join Date
    Jul 2006
    Age
    21
    Posts
    5,885
    Blog Entries
    25

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

    Nice, will this work under linux?

  5. #5
    The Crazy One TkTech will become famous soon enough TkTech's Avatar
    Join Date
    Jun 2006
    Location
    Canada
    Age
    18
    Posts
    1,549
    Blog Entries
    1

    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!

  6. #6
    Retired v0id is a glorious beacon of light v0id is a glorious beacon of light v0id is a glorious beacon of light v0id is a glorious beacon of light v0id is a glorious beacon of light v0id is a glorious beacon of light
    Join Date
    Apr 2007
    Posts
    2,978
    Blog Entries
    3

    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.

  7. #7
    Guru morefood2001 is just really nice morefood2001 is just really nice morefood2001 is just really nice morefood2001 is just really nice
    Join Date
    Jan 2008
    Posts
    1,724
    Blog Entries
    4

    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. :/

  8. #8
    MeTh0Dz is a name known to all MeTh0Dz is a name known to all MeTh0Dz is a name known to all MeTh0Dz is a name known to all MeTh0Dz is a name known to all MeTh0Dz is a name known to all
    Join Date
    May 2008
    Posts
    476

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

    Like I said just message me if you need 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. Replies: 5
    Last Post: 07-07-2008, 09:54 PM
  2. 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, 08:11 PM
  3. [C++] Validating user input
    By Xochiquetzal in forum C and C++
    Replies: 2
    Last Post: 07-08-2007, 05:18 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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