How do I swap two variables using mips? For example: y = 10 temp = y y = x x = temp
Do you mean MIPS architecture's instruction set? I couldn't find an instruction just for that. You can though do it in general with 3 instructions without an additional variable: ```c a = a ^ b; b = a ^ b; a = a ^ b; ``` There are similar different ways, but I like this one, I think it's simple. In MIPS assembly (if you really need it) it should be something like that: ``` xor $t1,$t1,$t2 xor $t2,$t1,$t2 xor $t1,$t1,$t2 ``` Though I haven't coded MIPS assembly so I'm not sure I did it right.
Yea I meant MIPS assembly. I was wondering how to swap using loading and storing if possible.
oh, you mean from memory? I thought you meant registers.. well in that case I'd simply read both values, and then write to the addresses: ```c a = *p1; b = *p2; *p1 = b; *p2 = a; ``` Somewhat ugly I guess, but I don't think you can do any better with memory.
No with registers. My attempt was something like this: .data .align 2 y: .space 4 x: .space 4 temp: .space 4 .text .globl main main: li $t0, 10 sw $t0, y lw $t1, x lw $t2, temp sw $t0, temp sw $t1, y lw $t3, temp sw $t3, x
I don't really understand, why are you initializing only 'y' and then loading the uninitialized values of x and temp? I'd try something like this: ``` .data .align 2 y: .space 4 x: .space 4 .text .globl main main: li $t0, 10 sw $t0, y li $t0, 5 sw $t0, x lw $t1, x lw $t2, y sw $t1, y sw $t2, x ```
How do I switch only x and y after giving y a value? That is the exercise.
aha.. well in that case, I'll just remove the x initialization I put there ``` .data .align 2 y: .space 4 x: .space 4 .text .globl main main: li $t0, 10 sw $t0, y lw $t1, x lw $t2, y sw $t1, y sw $t2, x ```
Oh ok I found why I needed temp. In the data x = -10. So I need another variable to store one of the values.
I think I miss something. where did the -10 just come* from? O.o
I missed part of the given instruction. Swap variables if x = -10. so .data .align 2 x: .word -10 y: .space 4 temp: .space 4 and in the code you store a number like 5 into y and then swap x and y
so if x is not -10 you don't swap?
No so I'm trying to take a higher level language code and translating it to MIPS assembly. Given: int x = -10; int y; temp; y = 5; temp = y; y = x; x = temp; Take that and make it work in MIPS.
first. you don't have the if(x == -10) in here. and second it's not exactly the same. You see, writing directly in assembler allows you to use processor registers. they are separate from memory, so you can just store the value in them and then right them to opposite memory places.. you don't need temp. The example I gave should still work if you put -10 in x up there, but I'll take a look at how to make a conditional jump
I don't need an if statement. I'm just trying to swap variables.
damn mistyped a register.. sec
``` .data .align 2 y: .space 4 x: .space 4 .text .globl main main: li $t0, 10 sw $t0, y lw $t1, x lw $t2, y li $t0, -10 bne $t1, $t0, end sw $t1, y sw $t2, x end: ```
But both x and y are not empty. In the case that y is given a value in the code and x already has a value. How do I swap them? I need temp as a temporary variable don't I?
no, because t1 will have the value of x and t2 the value of y. if I write t1 to y, t2 still has the old value. I can write that value to x.
Oh I see what you mean. I can overwrite values in variables?
in the memory ye, because you have them stored in the registers. If you were to try and swap between registers it would be more annoying, because after moving from one to another, you lose the value in the other.. In that case you can still do it without an extra register using the method I showed above
Ok so for example. y: .word 5 //y = 5 li $t0, 10 sw $t0, y //then y = 10?
yes, should be, if I read the docs right. I'd try that in emulator to be sure but it makes sense =)
From wiki ( https://en.wikipedia.org/wiki/MIPS_instruction_set#MIPS_assembly_language ) they have short pseudo high level code for each instruction. I used it to try and understand. so in this case it has:`sw $t,C($s) -> Memory[$s + C] = $t`
Hm, there is one problem though, I can't find a pseudo instruction using a label. it might not be possible. let me find an emulator, i wanna check
K i'm using this: http://morriswmz.jit.su/static/simple-mips-pipelined.html Notice you have compile errors below
Yea but I tried that website and I don't think it can handle the MIPS I'm trying to do. It doesn't use syscall which is used when printing.
Lemme try, If I run out of time I'll send you a PM later, ok? :)
Ok thank you for helping so much :) I know there isn't much online about MIPS. I feel like I have searched everywhere.
Ok never mind I got an emulator to work and you were right. I can just override what was written!
great =)
Join our real-time social learning platform and learn together with your friends!