简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM