2. (10 points) Write a MIPS code that would switch the values of registers R1 an
ID: 3709118 • Letter: 2
Question
2. (10 points) Write a MIPS code that would switch the values of registers R1 and R2. Your code SHOULD NOT use Loads/Stores. Your code SHOULD use the MIPS syntax explained in class.
In a style similar to this
ADDI R1, R0, #1
SW R1, 2000 (R0)
LOOP: LW R1, 2000 (R0)
MULT R2, R1, #4
ADDI R3,R2, #5000
LW R4, 0 (R3)
LW R5, 1500 (R0)
ADD R6, R4, R5
LW R1, 2000 (R0)
MULT R2, R1, #4
ADDI R1, R1, #1
SW R6, 0(R7) S
W R1, 2000 (R0)
ADDI R1, R1, #1
SW R1, 2000 (R0)
ADDI R1, R1, #1
LW R1, 2000 (R0)
ADDI R8, R1, #-101
BNEZ R8, LOOP
Explanation / Answer
Below code is to swap the value of two registers R1 and R2
I am using a simple concept here, let's say R1=a and R2=b
then R1=R1+R2=a+b
then R2=R1-R2=(a+b)-b=a
then R1=R1-R2=(a+b)-a=b
This way the values of two registers will be switched
Here is the required code for this problem:
.text
.glob1 main
main:
ADD R1,R1,R2
SUB R2,R1,R2
SUB R1,R1,R2
li $v0,10 # exit
syscall
.data
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.