James Carter
James Carter

Reputation: 387

LC-3 Assembly Language - swapping values

How can I swap to values in an address. Currently I have 2 registers which contain the addresses. I then had 2 temporary variables which stores those addresses. I then loaded the values since I have the address. But I can not figure out how to swap the values. I am trying to do bubble sort. The code below is what I currently have

IF          ;swapping condition
   ST R2,idata    ;temporily hold the smaller data
   ST R1,imindata ;temporaily hold the larger data
   ST R2,iminaddres ;store the values into that address
   ST R2,iaddress   ;finish the swaping of the two values
   LD R1,iminaddres ;reput the address back into the register
   LD R2,iaddres    ;reput the address back into the register to be used for next cycle

Upvotes: 1

Views: 4128

Answers (2)

old_timer
old_timer

Reputation: 71566

How would you do it in C?

temp = a;
a = b;
b = temp;

Then understand there is a need to load those values from memory, which changes things a bit

tempa = a;
tempb = b;
b = tempa;
a = tempb;

then isolate the loads and stores

rega <= load(a);
regb <= load(b);
store(a) <= regb;
store(b) <= rega;

then implement that in assembly. This smells like a homework assignment so I wont do it for you.

Upvotes: 1

Jim Garrison
Jim Garrison

Reputation: 86774

If all you want to do is swap the contents of two registers, there's a simple bit-twiddling trick:

XOR R1,R2
XOR R2,R1
XOR R1,R2

This will exchange the contents of the two registers without using any memory.

Upvotes: 0

Related Questions