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

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.

No comments:

Post a Comment

Constructive comments are welcome! Spam, or any abusive or profane comments will be deleted.