Mips swapping without temp variable

  • Thread starter Thread starter jiggleswiggly
  • Start date Start date
  • Tags Tags
    Mips Variable
Click For Summary
SUMMARY

The discussion focuses on implementing a swap function in MIPS assembly language without using a temporary variable. The provided code snippet demonstrates how to load values from memory and print them, while the swap function is intended to exchange the values of two integers stored in memory. A participant suggests using XOR operations for swapping, which is confirmed as a valid approach that avoids the need for a temporary variable. The exercise emphasizes understanding stack usage and optimization techniques in MIPS assembly.

PREREQUISITES
  • MIPS assembly language syntax and structure
  • Understanding of stack operations in assembly
  • Knowledge of XOR bitwise operations
  • Familiarity with system calls in MIPS for I/O operations
NEXT STEPS
  • Study MIPS assembly language stack management techniques
  • Learn about optimization strategies in MIPS, specifically regarding variable storage
  • Explore the properties of XOR and its applications in algorithm design
  • Investigate the use of system calls in MIPS for various I/O operations
USEFUL FOR

This discussion is beneficial for MIPS assembly language programmers, computer science students learning about low-level programming, and anyone interested in optimizing algorithms without using additional memory for temporary variables.

jiggleswiggly
Messages
4
Reaction score
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
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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 5 ·
Replies
5
Views
10K
  • · Replies 1 ·
Replies
1
Views
8K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 1 ·
Replies
1
Views
6K