Covered Topics

Please see the list of the topics I've covered. It's located near the bottom of the page. Thanks for stopping in!!
Showing posts with label SPIM. Show all posts
Showing posts with label SPIM. Show all posts

Wednesday, December 14, 2011

MIPS 32 Register Swap Example Using QtSPIM




I've been away from my blog the past several weeks due to term papers, final exams and other mini-emergencies.

During that time I have seen a huge number of hits on my MIPS Example code page. You can find it here:
http://inkarlslab.blogspot.com/2011/09/mips-32-example-program-using-qtspim.html

In case folks would like some more code examples, I've published this piece of code - based loosely on one of my lab assignments this semester.

This code prompts the user to enter two integers through the console, places them into registers, then swaps the contents of those registers. It does so without the use of the MIPS "move" command. As with my previous material, it was run using the SPIM emulator version 9.1.4 - dated September 4, 2011.

CAVEATS: This is not the most elegant way of doing it. It was my first attempt at doing a register swap. This can actually be done using three or four lines of code, rather than my five, but my professors might get a bit angry if I gave away all the answers here. But this code does work and should help folks to get a head start on writing simple MIPS programs.

The complete code is as shown below. Simply copy and save this as "integer_swap.asm", or whatever .asm filename you like, using any convenient text editor such as Windows Notepad or LINUX Gedit.

NOTE: Sorry about the crummy formatting - I'm having trouble with tabs in the blogger interface.

===========================================================

## Program reads variables from console into registers
## then swaps the contents of those registers.
## Prints out contents before and after swap.
##
##
##
## Registers used:
## $v0 - syscall parameter and return value
## $a0 - syscall parameter
## $s1 - holds x
## $s2 - holds y
## $t0 - holds temp value


.text

main: # SPIM starts execution here


## Let's get x
la $a0, enter_x # Print msg for x
li $v0, 4
syscall

## read x into $s1.
li $v0, 5 # syscall 5 = read integer
syscall

move $s1, $v0 # x = Integer just read
## Message for x
la $a0, print_s1 # Prints "$s1 = "
li $v0, 4
syscall

## Let's display initial value of $s1 - so read x into $a0.
move $a0, $s1 # Move contents of $s1 into $a0
li $v0, 1 # Syscall for 'print integer'
syscall # Print value in $a0

## Print carriage return

la $a0, print_ret
li $v0,4
syscall

## Now, let's get y
la $a0, enter_y # Print msg for y
li $v0, 4
syscall

## read y into $s2.
li $v0, 5 # syscall 5 = read integer
syscall

move $s2, $v0 # y = Integer just read

## Message for y
la $a0, print_s2 # Prints "$s1 = "
li $v0, 4
syscall

## Let's display initial value of $s2 - so read y into $a0.
move $a0, $s2 # Move contents of $s2 into $a0
li $v0, 1 # Syscall for 'print integer'
syscall # Print value in $a0

## Print carriage return

la $a0, print_ret
li $v0,4
syscall

## Now, let's try and swap them - WITHOUT using 'move' command!!

addi $t0, $s2, 0 # Add the contents of $s2 into $t0
sub $s2, $s2, $s2 # Subtract the contents of $s2 from itself, thus zeroing it out
addi $s2, $s1, 0 # Add contents of $s1 into $s2
sub $s1, $s1, $s1 # Subtract the contents of $s1 from itself, thus zeroing it out
addi $s1, $t0, 0 # Add contents of $t0, same as former $s2, into $s1

## Now, lets print out the results:

la $a0, print_s1 # Prints "$s1 = "
li $v0, 4
syscall

move $a0, $s1 # Prints contents of $s1
li $v0, 1 # Syscall for printing of variable type integer
syscall

## Print single carriage return
la $a0, print_1ret
li $v0,4
syscall

la $a0, print_s2 # Prints "$s2 = "
li $v0, 4
syscall
move $a0, $s2 # Prints contents of $s2
li $v0, 1 # Syscall for printing of variable type integer
syscall

## Print carriage return
la $a0, print_ret
li $v0,4
syscall

exit:
li $v0, 10 # These two lines here for smooth exit from program
syscall

.data

enter_x: .asciiz "Enter a number for x, and press 'ENTER': " # Console prompt to enter the value for x
enter_y: .asciiz "Enter a number for y, and press 'ENTER': " # Console prompt to enter the value for y

print_s1: .asciiz "$s1 = " # Screen output to console
print_s2: .asciiz "$s2 = " # Screen output to console
print_1ret: .asciiz "\n"
print_ret: .asciiz " \n \n \n"



=======================================================
The picture above shows what should happen when you run the program using SPIM.

A slight variation of the problem asks you to do this without using a third register for temporary storage of one value (as was done here).

This basic algorithm can be modified slightly to permit the integers to be swapped WITHOUT using a third register.

HINTS: You will only need three or four lines of code to do the actual swap. There are basically two ways I know of to do it: 1) Use "add" and "sub" operations or 2) use XOR operations.

Hope this information is of help to someone.

Friday, September 9, 2011

SPIM In a LINUX Environment

In my last post I discussed MIPS 32 and the SPIM emulator for running MIPS 32 assembly code on your PC. Today I will briefly talk about how I got SPIM running in a LINUX environment. My earlier post is located at http://inkarlslab.blogspot.com/2011/09/spim-mips-32-bit-simulator.html

The problem I was having yesterday was in compiling SPIM on a Fedora system. I was getting this error message when running the makefile:

make: *** No rule to make target `../CPU/spim.h', needed by `spim.o'. Stop.

All the files were there and apparently in their proper folders, ...

I am still in communication with someone in attempt to get that figured out. The person helping me suggested I try installing the pre-compiled Debian binary. So I downloaded the 32-bit Debian file for QtSPIM from the following link:

SPIM MIPS Simulator Homepage - You can find more information and LINUX versions by going here.
http://spimsimulator.sourceforge.net/

I next unpacked it with the GNOME "File Roller" tool and extracted everything to the "SPIM" directory I had created earlier. Basically that's it. There are two ways to run QtSPIM from this installation.
Suppose you installed your "SPIM" directory in your home directory:

1) On the command line, simply navigate to the "SPIM" directory, then to "/usr", then to "/bin". NOTE that these /usr and /bin directories are NOT the same ones as are in your UNIX file system! Once you are in /bin, type qtspim to start the application, or

2) locate the SPIM/usr/bin folder via the GNOME desktop and click on the QtSPIM executable.



As you can see, QtSPIM is now running on a Fedora system. Having never tried to install a Debian executable on Fedora, I thought this was pretty cool.

Thursday, September 8, 2011

SPIM: MIPS 32-Bit Simulator

SPIM is a simulator that allows one to run programs in MIPS assembly language on one's own PC. Many college courses in computer architecture and engineering teach MIPS assembly language as part of the curriculum. MIPS stands for Microprocessor without Interlocked Pipeline Stages, and is an example of a RISC (Reduced Instruction Set Computer) architecture. MIPS is used in lots of embedded microprocessors, video game systems, some desktop workstations such as DEC (Digital Equipment Corporation) and SGI (Silicon Graphics, Inc.), and many other applications. MIPS based chips can be found in hand held computers and cell phones. There are both 32 bit and 64 bit versions of MIPS.

This semester, I am taking a computer hardware and architecture course and am getting prepared for our first MIPS 32 programming assignment. We will be using SPIM. SPIM is written by James Larus. You can find more information and downloads at the links below:

SPIM Sourceforge Project Page - You can download the Windows version conveniently here.
http://sourceforge.net/projects/spimsimulator/

SPIM MIPS Simulator Homepage - You can find more information and LINUX versions by going here.
http://spimsimulator.sourceforge.net/

From my understanding of the documentation, the more recent versions of SPIM are apparently called "QtSPIM".

The LINUX downloads there appear to cater to Debian and Ubuntu users. I am currently in the process of finding out what is available for those of us who use Fedora and other distros. Last night I attempted to compile SPIM directly from the source code onto my Fedora system, but to no avail. There is a problem with the makefile that I am currently trying to resolve. I have contacted some people about it, so as soon as I find out what is going on with that I'll post it here. Meanwhile, in the interest of quickly getting something going for the class, I loaded QtSPIM to my Windows VISTA partition from the first of the two links shown above. Here is a screenshot:



Note there are two windows open. One is the "console" window that shows any results or dialogs when the program runs. The "Qt" window shows you your source code, registers, and other relevant information.

This promises to be quite an interesting semester. Will write more as things progress.