MARIE 中的 SKIPCOND 用于循环或条件 [英] SKIPCOND in MARIE for looping or conditionals

查看:56
本文介绍了MARIE 中的 SKIPCOND 用于循环或条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须用 MARIE 汇编语言来实现这个伪代码

I have to implement this pseudocode in MARIE assembly language

Input a number and store it in X;
Input a number and store it in Y;
while X < 10 do
X = X + 1;
Output the value of X;
endwhile;
Output the value of Y*2;

到目前为止,我已经写了这个:

So far, I have written this:

Input
Store X
Input
Store Y

Loop, Load X
Subt TEN
Skipcond 400
Jump Endloop
Load X
Add ONE
Store X
Output

(然后我不确定如何将 Y 与 2 相乘)

(And then i am not sure how to multiply Y with 2)

X, Dec 0
Y, Dec 0
ONE. Dec 1
TEN, Dec 10

我是否使用了正确的跳过指令?

Am i using correct skipcond inetruction?

推荐答案

Skipcond 单独是相当有限的.首先,它只能跳过一条指令,其次,它只能测试必要的 6 个条件中的 3 个.Skipcond 可以跳过 <、= 和 >,但不能跳过 <=、<> 或 >=.(这些也是签名条件,没有未签名的测试,但这是另一回事.)

Skipcond alone is quite limited.  First, it can only skip one instruction, and second, it can only test 3 of the necessary 6 conditions.  Skipcond can skip on <, =, and >, but cannot skip on <=, <>, or >=.  (These are signed conditions, as well, there's no unsigned tests, but that's another matter.)

如果您可以在一条指令中做一些有用的事情,那么 SkipCond 可以提供帮助.

If you can do something useful in one instruction, then here's how SkipCond can help.

首先,我们应该注意,C 中的 if-then 需要汇编中的相反条件,因为在 C 中我们说的是何时运行 then 部分,但在汇编中我们说的是何时不运行该部分指令(即​​何时跳过指令).因此,如果您可以在一条指令中做一些有用的事情,您可以编写一个 if-then 来测试 >=、<>、<=,但不能测试其他条件.对于这些,请继续阅读以了解如何将 SkipCond 与 Jumps 结合使用.

First, we should note that an if-then in C requires the opposite condition in assembly, because in C we're saying when to run the then-part, but in assembly we're saying when not to run that one instruction (i.e. when to skip an instruction).  So, if you can do something useful in one instruction, you can write an if-then that tests for >=, <>, <=, but not the other conditions.  For those, read ahead to see how to combine SkipCond with Jumps.

<头>
条件跳过时
Skipcond 000AC <0
Skipcond 400AC = 0
Skipcond 800AC >0

如果您需要的东西 (a) 需要的指令多于 then 部分的指令,或 (b) 需要其他条件测试之一,或 (c) 是更复杂的控制结构,例如 if-then-否则,我们有时可以将 SkipCondJump 结合使用(就像你经常做的那样):

If you need something that (a) requires more instructions than one for the then-part, or (b) needs one of the other condition tests, or (c) is a more complicated control structure such as if-then-else then we can sometimes use SkipCond combined with Jump (as often done and as you are doing):

Skipcond xxx
Jump Label

<头>跳转到Label
条件跳过时
Skipcond 000AC <0AC >= 0
Skipcond 400AC = 0AC <>0
Skipcond 800AC >0AC <= 0
cond Skips when Jumps to Label when
Skipcond 000 AC < 0 AC >= 0
Skipcond 400 AC = 0 AC <> 0
Skipcond 800 AC > 0 AC <= 0

但是,由于 MARIE 只能执行 6 个必要的关系运算符中的 3 个,因此有时我们将不得不引入涉及 2 个 Jumps 的 3 指令序列.

However, since MARIE can only do 3 of the 6 necessary relational operators sometimes we will have to introduce a 3-instruction sequence involving 2 Jumps.

下面的结构会根据上面的表格跳转到Around——然而,Around在这里被设置为跳过另一个跳转,并且基本上保持当前的代码序列,而第二个 Jump 转到 Label (尽管现在在相反的 Skipcond 情况下,选择其他 3 个关系运算符).

The following construct will jump to Around according to the above tables — however, Around is set up here to jump around the other jump, and basically stay with this current sequence of code, whereas the 2nd Jump goes to Label (though now in the opposite Skipcond cases, which picks up the other 3 relational operators).

    ..
    Skipcond xxx
    Jump Around     # this Jump is executed if AC >= 0, AC <> 0, AC <=0, respectively
    Jump Label      # this Jump is executed if AC < 0, AC = 0, AC > 0, respectively
Around:
    ....

<头>
条件跳过时跳跃Around"跳转到标签
Skipcond 000AC <0AC >= 0AC <0
Skipcond 400AC = 0AC <>0AC = 0
Skipcond 800AC >0AC <= 0AC >0

如果您的意图是执行 if-then-else,另一种方法可以反转条件并交换 then-part 和 else-part — 这有时可以允许使用更简单的 2 指令序列.

If your intent is to do an if-then-else, another approach can be invert the condition an also swap the then-part and the else-part — that can sometimes allow use of the simpler 2 instruction sequence.

虽然这些不是 SkipCond 的唯一用途,但它们确实涵盖了所有 6 个关系运算符,并提供了一个通用的 if-goto-label,它是汇编语言中控制结构的基石.

While these are not the only uses of SkipCond, they do cover all 6 relational operators, and provide a general purpose if-goto-label which is the cornerstone of control structures in assembly language.

对于原始问题帖子:您对跳过 Jump 指令有正确的想法,但条件错误.

For the original question post: you have the right idea about skipping a Jump instruction, but the wrong condition.

好的,现在你要做的是这个伪代码:

Ok, now what you're doing is this pseudo code:

..
while X < 10 do
    X = X + 1;
    Output the value of X;
endwhile;
....

我们先把这段伪代码翻译成汇编语言的 if-goto-label 风格.(这是有效的 C 代码,仅供参考,因此您可以使用实际语言编写、测试和调试您的 if-goto-label 伪代码.)

Let's first translate this pseudo code into the if-goto-label style of assembly language.  (This is valid C code, fyi, so you can write this, test & debug your if-goto-label pseudo code in an actual language.)

..
Loop:
    if ( X >= 10 ) goto Endloop;
    X++;
    output(x);
    goto Loop;
Endloop:
....

请注意,C 中的 while 条件表示何时继续循环,而使用 if-goto-label,我们在这里表示何时退出.因此,循环条件的意义是相反的(C while vs. if-goto-label).逻辑上,在 if-goto-label 中,当 C-while 条件为假时,我们分支(退出)——由于条件是关系运算符,我们可以通过翻转关系运算符来否定条件.(注意:意义不一致并非总是如此;C 的 do-while 和循环末尾的 if-goto-label 向后分支在意义上是一致的:两者都在条件为真时继续循环,在条件为假时退出.)

Let's note that the sense of the while-condition in C says when to continue the loop, whereas with if-goto-label, we are here saying when to exit.  Thus, the senses of the loop conditions are reversed (C while vs. if-goto-label).  Logically, in if-goto-label, we branch (to exit) when the C-while condition is false instead — and since the condition is a relational operator we can negate the condition by flipping the relational operator.  (Note: Disagreement of the sense is not always the case; C's do-while and an if-goto-label backward branch at the end of the loop agree in sense: both continue the loop on condition being true and exit on condition being false.)

接下来,b/c MARIE 除了与零进行比较外,让我们应用一些数学运算:从 X >= 10 中的 >= 表达式的两边减去 10X - 10 >= 0

Next, b/c MARIE doesn't do comparison except against zero, let's apply some math: subtract 10 from both sides of the >= expression from X >= 10 to X - 10 >= 0

..
Loop:
    if ( X - 10 >= 0 ) goto Endloop;
    X++;
    output(x);
    goto Loop;
Endloop:
....

现在您可以从与零的比较和上表中看到您应该在这里使用什么条件.

Now you can see from the comparison to zero and the above tables what condition you should use here.

或者,我们可以使用调试器来帮助我们查看它是否工作(如果不工作,则使用不同的条件).

Alternatively, we can use the debugger to help us see if it is working (and use a different condition if that doesn't work).

这篇关于MARIE 中的 SKIPCOND 用于循环或条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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