2013-02-22 86 views
1

如何交換地址中的值。目前我有2個寄存器包含地址。然後我有2個臨時變量存儲這些地址。因爲我有地址,所以我加載了這些值。但我無法弄清楚如何交換這些值。我正在嘗試做冒泡排序。下面的代碼是什麼,我現在有LC-3彙編語言 - 交換值

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 
+0

什麼不工作?你怎麼知道它不工作? – 2013-02-22 19:30:24

回答

0

如果你想要做的是交換的兩個寄存器的內容,有一個簡單的位變換伎倆:

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

這會交換的內容這兩個寄存器沒有使用任何內存。

+0

我其實想改變內存地址而不是寄存器的內容。你會怎麼做? – 2013-02-22 19:43:16

+0

lc3沒有xor,可以用not和and來完成,但需要一些指令。 – 2013-02-23 01:00:29

1

你會怎麼做C?

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

然後理解有必要從存儲器加載的那些值,從而改變事情有點

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

然後隔離加載和存儲

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

然後實現,組裝時。這聞起來像是家庭作業,所以我不會爲你做。