显示输入了多少个正数和负数 [英] Show how many positive and negative numbers were input

查看:184
本文介绍了显示输入了多少个正数和负数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个Little Man Computer程序,该程序输出我有多少个正数和我有多少个负数,但它也将负数也视为正数.

I'm trying to write a Little Man Computer program that outputs how many positive numbers I have and how many negative numbers I have, but it considers the negative numbers as positive too.

这是我到目前为止所拥有的:

This is what I have so far:

LOOP IN 
     STO NUM
     BRZ END
     BRP POS
POS  LDA ZERO 
     ADD UN 
     STO ZERO 
END  LDA ZERO
     OUT 
     BR LOOP
     HLT
NUM  DAT 000
UN   DAT 001
ZERO DAT 000

推荐答案

规范不支持提供具有负输入的LMC.LMC存储器单元只能保存0到999之间的值(来源: Wikipedia ).LMC的标志表示负"的唯一概念.而且该标志是 ,通过执行 ADD SUB 来设置,而不是通过 INP / IN LDA STA / STO (拼写不同).另请参阅 Ian的这些笔记!D. Allen在这里讨论这个令人困惑的概念.

Providing an LMC with negative input is not supported by the specification. LMC memory cells can only hold values between 0 and 999 (source: Wikipedia). The only notion of "negative" is represented by the LMC's negative flag. And that flag is only set by the execution of ADD or SUB, so not by INP/IN, LDA or STA/STO (spellings differ). See also these notes of Ian! D. Allen where he discusses this confusing concept.

这实际上意味着您的 BRP 指令将始终跳转到提供的标签.

This practically means that your BRP instruction will always branch to the provided label.

您可能会发现实际上允许负输入的模拟器,但这将是对原始规范的扩展.

You may find simulators that actually allow negative input, but that would be an extension to the original specifications.

现在,您的代码也与此 BRP POS 指令有关,因为它只是跳转到下一行(标记为 POS ),因此实际上不会 BRP 是否分支的任何区别:这是无操作.

Now, your code also has an issue with this BRP POS instruction, because it just jumps to the next line (labeled POS), so it actually would not make any difference whether BRP would branch or not: it is a no-operation.

由于LMC中并不真正存在负值,因此让我们做些不同的练习:考虑10's补码中的输入,因此将500..999视为负值(即-500 ...-1).或者,当您执行 INP 时,该带符号的输入值实际上将存储在10的补码中.然后计算少于500个输入(正数)和不小于输入数(因为当解释为10's时为负数)的输入数.

As negative values do not really exist in LMC, let's do the exercise a bit differently: consider the input as presented in 10's-complement, so that 500..999 are to be considered negative values (i.e. -500...-1). Or, that signed input values will actually get stored in 10's-complement when you do INP. Then count the number of inputs that are less than 500 (positive) and those that are not (as these are negative when interpreted as 10's-complement).

LOOP    INP
        BRZ FINISH
        SUB COMPARE
        BRP ELSE
        LDA LESS
        ADD ONE
        STA LESS
        BRA LOOP
   ELSE LDA NOTLESS
        ADD ONE
        STA NOTLESS
        BRA LOOP
 FINISH LDA LESS
        OUT
        LDA NOTLESS
        OUT
        HLT
   LESS DAT
NOTLESS DAT
COMPARE DAT 500
    ONE DAT 1
    
<script src="https://cdn.jsdelivr.net/gh/trincot/lmc@v0.6/lmc.js"></script>

请确保为程序提供最终的0值以输出结果.

Be sure to supply a final 0 value for the program to output the result.

这篇关于显示输入了多少个正数和负数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆