鉴于伪代码,我必须编写玛丽的汇编语言 [英] Given the pseudocode, I have to write MARIE'S assembly language

查看:83
本文介绍了鉴于伪代码,我必须编写玛丽的汇编语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

Am i using correct skipcond inetruction?

推荐答案

跳过 Jump 指令的想法是正确的,但条件错误.

You have the right idea about skipping a Jump instruction, but the wrong condition.

<头>
cond跳过
Skipcond 000AC <0
Skipcond 400AC = 0
Skipcond 800AC >0

好的,让我们将 SkipcondJump 结合起来(就像你经常做的那样):

Ok, so, let's combine Skipcond with Jump (as often done and as you are doing):

Skipcond xxx
Jump Label

<头>时跳转到Label
cond跳过
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


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


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 除了与零不做比较,让我们应用一些数学:从 >= 表达式的两边减去 10 从 X >= 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 只能执行 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:
    ....

<头>
cond跳过时间跳转周围"跳转到Label
Skipcond 000AC <0AC >= 0AC <0
Skipcond 400AC = 0AC <>0AC = 0
Skipcond 800AC >0AC <= 0AC >0

这篇关于鉴于伪代码,我必须编写玛丽的汇编语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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