简体   繁体   English

计算列表中负数的出现次数

[英]Counting occurances of a negative number from a list

I'm trying to learn LC-3 assembly and am looking at the below example: 我正在尝试学习LC-3组装并查看以下示例:

        .orig   x3100   
        ADD R3, R0, #0  ;copy R0 into R3
        AND R1, R1, #0  ;clear count

        ADD R3, R3, #0  ;test for Neg
        BRZP NEXT       ;count if Neg
        ADD R1, R1, #1

NEXT    AND R2, R2, #0  ;check remaining 15 bits
        ADD R2, R2, #-15
LOOP    ADD R3, R3, R3  ;shift R3 left
        BRZP AGAIN      ;count if Neg
        ADD R1, R1, #1
AGAIN   ADD R2, R2, #1  ;loop until done
        BRN LOOP

There's a couple points I don't quite understand: 有几点我不太了解:

ADD R3, R3, #0 ;test for Neg

I don't see how this is testing for a negative value: I'm reading it as it adds nothing to R3, ie it does nothing. 我看不到它是如何测试负值的:我正在阅读它,因为它对R3没有任何作用,即它什么也没做。 The following line as well, I don't quite understand what it's doing with BRZP . 在下面的代码行中,我不太了解BRZP

I want to change this example to instead check from a list of integers: 我想更改此示例,而不是从整数列表中进行检查:

INTEGERS    .fill    84
            .fill    -2
            .fill    -13
            .fill    4
            .fill    -4

In the above, there's three negative integers, so the count (R1) would then be 3 at program end. 在上面,有三个负整数,因此在程序结束时,计数(R1)将为3。 How would I do this? 我该怎么做?

I assume ADD R3, R3, #0 ;test for Neg sets flags based on the result, so the conditional branch will be based on the result that ADD stores into R3 . 我假设ADD R3, R3, #0 ;test for Neg将根据结果设置标志,因此条件分支将基于ADD存储到R3的结果。

As Jester points out, it seems to be looping on counting set bits in a single integer, using add r3,r3,r3 to left-shift it and set flags. 正如Jester指出的那样,似乎在循环使用单个整数对设置位进行计数,使用add r3,r3,r3将其左移并设置标志。


I don't know lc3 specifically, but it looks like you could save an instruction by clearing the counter and having the ADD R3, R0, #0 ;copy R0 into R3 take care of setting flags initially. 我不特别了解lc3,但看起来您可以通过清除计数器并ADD R3, R0, #0 ;copy R0 into R3来保存指令ADD R3, R0, #0 ;copy R0 into R3最初会负责设置标志。

LC3 doesn't have mov instructions, I take it. LC3没有mov指令,我接受。 You copy data around by adding an immediate zero, since unlike x86, it uses 3-operand instructions where the dest doesn't have to be one of the src instructions. 您可以通过添加立即数零来复制数据,因为与x86不同,它使用3操作数指令,而dest不必是src指令之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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