[英]Why isn't my assembly program setting r1 to the correct value?
我正在LC3機器上編寫匯編程序。
我的匯編程序是一個LC3程序,它將R2和R3相乘並將結果存儲在R1中。
這是我的源代碼(帶注釋)
;Sets pc to this address at start of program
.ORIG x3000
;R1 will store the result lets clear it(ANd with 0)
AND R1,R1,x0
;R2 will be multiplied by R3, let's clear both of them
AND R2,R2,x0
AND R3,R3,x0
;Test case 4 * 3 = 12;
ADD R2,R2,4
ADD R3,R3,3
;Add to increment zone
LOOP Add R1,R1,R2;
;Decrement the counter, in this case the 3 or R3
ADD R3,R3,x-1
BrP LOOP
HALT
.END
我的測試用例乘以4 *3。結果應為12,並將其存儲在R1中。 但是,當我在LC3仿真器中運行該程序時,這就是我得到的輸出
R3末尾保留正確的值,但R1保留-1。...有人看到我的代碼有問題嗎? 我確保在開始時清除R1,並繼續將R3加到R1並將結果存儲到R1,而此時計數器R3或3大於零。
HALT
只是用於停止機器的TRAP指令的“偽指令”。
您可以這樣寫:
TRAP x25 ;HALT the machine
但是以這種方式,您需要記住TRAP向量中的位置,在這種情況下為x25
。 因此最好只使用HALT
。
其他常見的TRAP也具有pseduo指令: IN
, OUT
等。
我假設您要將結果存儲在某個地方。 您可以執行以下操作:
;Sets pc to this address at start of program
.ORIG x3000
;R1 will store the result lets clear it(ANd with 0)
AND R1,R1,x0
;R2 will be multiplied by R3, let's clear both of them
AND R2,R2,x0
AND R3,R3,x0
;Test case 4 * 3 = 12;
ADD R2,R2,4
ADD R3,R3,3
;Add to increment zone
LOOP Add R1,R1,R2;
;Decrement the counter, in this case the 3 or R3
ADD R3,R3,x-1
BrP LOOP
ST R1, Result ;STORE R1 at Result
HALT
Result .FILL x0000 ;this will be x000C=12h after execution
.END
- - - - - - - - - - -編輯 - - - - - - - - - - - - -
關於您的最后一個問題(在評論中):
如果HALT停止我的程序,那么Reslt .FILL x0000指令將如何運行?
這更多是關於匯編程序如何工作的問題。
答案是因為: 匯編時間 != 執行時間
指令在匯編時考慮。
實際上,組裝時間由兩步組成:
這是實現匯編程序的一種非常常見的方式,LC3匯編程序也不例外。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.