0
我想寫一個簡單的程序,從Data1中取一個16位整數,計算多少位是1位,然後將結果打印到屏幕上。我很確定我的代碼應該做的伎倆,但出於某種原因,我得到的輸出是一個小盒子符號,而不是實際的整數值。有誰知道我爲什麼得到這個輸出,我怎麼能改變它?這是我的代碼:計數位和打印結果LC3彙編
.orig x3000 ;start at address x3000
LD R1, Data1 ;load R1 with Data1
AND R2, R2, #0 ;clear R2
ADD R2, R2, #1 ;set R2 to 1
;R2 will be compared with the data
;value to see if the bit is 1
AND R3, R3, #0 ;clear R3
;R3 will be used as the counter
AND R4, R4, #0 ;R4 will hold the answer
while
ADD R3, R3, #1 ;add 1 to the counter
AND R0, R0, #0
AND R0, R1, R2 ;compare R1 and R2
brp yes ;if the bit is 1, goto yes
no
ADD R2, R2, R2 ;shift bits left
br check ;skip over yes
yes
ADD R4, R4, #1 ;increase the counter
br no ;continue
check
AND R0, R0, #0
ADD R0, R3, #-16 ;if counter is still under 16
brn while
endwhile
LEA R0, msg ;load message
PUTS ;print message
AND R0, R0, #0
ADD R0, R4, #0 ;put R4 (answer) in R0
OUT ;print it to screen
HALT
Data1 .FILL x0002 ;this variable changes
msg .STRINGZ "\nNumber of non-zero bits: "
.end
我應該得到的輸出是:
Number of non-zero bits: 1
(由於被檢查的值是2,且有1位2) 但我得到的輸出是:
Number of non-zero bits: ☐
任何幫助或建議將非常感激!我還是很新的LC3組裝...
有很多更快的方法來計數位[這裏](https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetNaive) – 2015-02-10 08:03:38
我確定有,但我不尋找效率在這裏。我需要在LC3程序集中完成它,因爲它是一項任務,我只是想弄明白爲什麼我實現它的方式不起作用。不過謝謝你。 – k1234 2015-02-10 13:44:47