Mips swapping without temp variable

In summary, the conversation discusses modifying the code in swap.s to directly translate a procedure from C to MIPS assembly language. The goal is to use 6 lw/sw instructions and not optimize by using $t0 to hold the temp variable. The possibility of using XOR to swap values is also mentioned.
  • #1
jiggleswiggly
4
0
so i have swap.s
Code:
	.text
main:
	la	$a0,n1
	la	$a1,n2
	jal	swap
	li	$v0,1	# print n1 and n2; should be 27 and 14
	lw	$a0,n1
	syscall
	li	$v0,11
	li	$a0,' '
	syscall
	li	$v0,1
	lw	$a0,n2
	syscall
	li	$v0,11
	li	$a0,'\n'
	syscall
	li	$v0,10	# exit
	syscall

swap:
	
	

L1: 

	.data
n1:	.word	14
n2:	.word	27
my goal is to:
Modify swap.s to translate the following procedure directly to MIPS
assembly language. The temp variable, like all local variables in C
(when not optimized), is stored on the stack. In other words you
cannot use $t0 to hold temp, though you may need it briefly. Hint: you
will need to use 6 lw/sw instructions.
This exercise is slightly contrived, and could be easier if we let you
optimize and use $t0 to hold the temp variable, part of the point of this
exercise is to see what kind of difference optimization can make.
Code:
void swap (int *px, int *py) {
int temp;
temp = *px;
*px = *py;
*py = temp;
}
i am confused on jal and jr's though...
for swap could I just do:
Code:
xor $a0 $a0 $a1
	xor $a1 $a0 $a1
	xor $a0 $a0 $a1
	jr $ra

or do i need to save to the stack and such?
 
Technology news on Phys.org
  • #2
Hey jiggleswiggly and welcome to the forums.

That piece of XOR code is actually a real swap that works without needing a temporary variable.

If you wanted to prove it then use the properties of XOR and note that A XOR A = 0 and 0 XOR B = B for any B and A.
 

What is "Mips swapping without temp variable"?

"Mips swapping without temp variable" refers to a technique used in programming in the Mips assembly language to swap the values of two variables without using a temporary variable.

Why would someone want to swap values without using a temp variable in Mips?

Using a temp variable takes up additional memory and can slow down the program. Swapping values without a temp variable can make the program more efficient and save memory.

How does Mips swapping without temp variable work?

This technique involves using bitwise operations, specifically XOR (exclusive OR) to swap the values of two variables without using a temp variable.

What are the potential drawbacks of Mips swapping without temp variable?

One potential drawback is that it can be more difficult to understand and debug, as it is a less common technique. It may also be less efficient for larger data types, such as arrays or structures.

Are there any other alternatives to Mips swapping without temp variable?

Yes, there are other techniques for swapping values in Mips without using a temp variable, such as using a third variable or using a function. The most appropriate method will depend on the specific program and its requirements.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Programming and Computer Science
Replies
1
Views
6K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
4
Views
7K
  • Programming and Computer Science
Replies
1
Views
7K
  • Programming and Computer Science
Replies
5
Views
9K
  • Programming and Computer Science
Replies
1
Views
8K
  • Programming and Computer Science
Replies
5
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top