2015-02-10 36 views
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组装...

+0

有很多更快的方法来计数位[这里](https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetNaive) – 2015-02-10 08:03:38

+0

我确定有,但我不寻找效率在这里。我需要在LC3程序集中完成它,因为它是一项任务,我只是想弄明白为什么我实现它的方式不起作用。不过谢谢你。 – k1234 2015-02-10 13:44:47

回答

0

你忘了OUT打印ASCII值指向的字符。您需要将答案增加48个。当涉及9个以上的位数时,您将遇到问题,因此您需要在那里进行一些检查。

希望有帮助!