“Skipcond”在 MARIE 汇编语言中如何工作?

发布于 2024-10-20 01:11:10 字数 597 浏览 5 评论 0原文

我正在尝试理解 MARIE 汇编语言。我不太明白 skipcond 执行诸如 <>、乘法或除法之类的操作。

我正在使用这个简单的程序:

x = 1
while x < 10 do
x  = x +1
endwhile;

我不明白的是如何使用某些跳过条件:

Skipcond 800 if AC > 0,
Skipcond 400 if AC = 0,
Skipcond 000 if AC < 0

现在,我知道我会从 10 中减去 x 并使用skipcond 进行测试。

我不确定是哪一个以及为什么。我想如果我知道它们是如何工作的,也许会更容易理解。为什么要用它来和零比较呢?

这就是我所拥有的:

100     load one
101     store x
102     subt ten
103     skipcond400  if x-10 = 0?   // or skpcond000 x -10 < 0?? 

I am trying to understand the MARIE assembly language. I don't quite understand skipcond for
doing things like <, or >, or multiply or divide.

I am taking this simple program:

x = 1
while x < 10 do
x  = x +1
endwhile;

What I don't understand is how to use certain skip conditions:

Skipcond 800 if AC > 0,
Skipcond 400 if AC = 0,
Skipcond 000 if AC < 0

Now, I know I would subtract x from 10 and test using skipcond.

I am not sure which one and why. I guess if I knew how they really work maybe it would be easier to understand. Why is it used to compare to zero?

This is what I have:

100     load one
101     store x
102     subt ten
103     skipcond400  if x-10 = 0?   // or skpcond000 x -10 < 0?? 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

著墨染雨君画夕 2024-10-27 01:11:10
while x < 10 do
    x  = x + 1

一旦 x 等于 10,就会跳出循环。如果从 x 中减去 10,您将得到一个负值,直到 x 等于 10(并且该值为 0)。因此使用 skpcond000 是错误的,因为它会很快跳出。所以 skpcond400 是正确的。

如果您更改 C 代码,使其更接近汇编代码,也许会更容易理解:

Original:            while (x < 10) do
Subtract 10:         while ((x - 10) < 0) do
Use != instead of <: while ((x - 10) != 0) do

另请注意,您必须在条件之后增加 x after 才能重现相同的情况while 循环的行为。

while x < 10 do
    x  = x + 1

will jump out of the loop as soon as x equals 10. If you subtract 10 from x, you'll get a negative value until x equals 10 (and the value is 0). So using skpcond000 would be wrong as it would jump out too soon. So skpcond400 is correct.

Perhaps it is easier to understand if you change the C code so it will be closer to the assembly code:

Original:            while (x < 10) do
Subtract 10:         while ((x - 10) < 0) do
Use != instead of <: while ((x - 10) != 0) do

Also note that you have to increase x after the condition to reproduce identical behaviour to the while loop.

人疚 2024-10-27 01:11:10

这可能会有所帮助。有很多方法可以写这个,但我认为这是理解循环中发生的事情的最简单的方法。注意:通常变量放在程序的底部。

    while x<10
          x = x+1
Org 100  
         Load     One  / loads accumulator = 1 from a decimal constant
         Store    X    / initialize the var x = 1

loop,    Load     X    / loads x into the accumulator
         Subt     Ten  / compares x to 10 
         Skipcond 000  / if ac < 0 i.e. if x < 10 run rest of loop body
         Jump     Endloop / if ac => 10  terminate loop
         Load     X    / begin the Loop
         Add      One  / add 1 to x
         Store    X    / store new value in X
         Jump     loop / continue loop

Endloop, Halt   / ends loop

One,     Dec 1  / Constant
Ten,     Dec 10 / Constant
X,       Dec 0  / Variable

This may help. There are many ways to write this but I think this is the easiest way to understand what is happening in the loop. Note: usually variables are placed at the bottom of the program.

    while x<10
          x = x+1
Org 100  
         Load     One  / loads accumulator = 1 from a decimal constant
         Store    X    / initialize the var x = 1

loop,    Load     X    / loads x into the accumulator
         Subt     Ten  / compares x to 10 
         Skipcond 000  / if ac < 0 i.e. if x < 10 run rest of loop body
         Jump     Endloop / if ac => 10  terminate loop
         Load     X    / begin the Loop
         Add      One  / add 1 to x
         Store    X    / store new value in X
         Jump     loop / continue loop

Endloop, Halt   / ends loop

One,     Dec 1  / Constant
Ten,     Dec 10 / Constant
X,       Dec 0  / Variable
兰花执着 2024-10-27 01:11:10

SkipCond 可能会令人困惑。假设您始终将其与紧随其下方的 Jump 结合使用,这可能会有所帮助。无论如何,这是最常见的使用方式,但也有助于将其视为 if...then 构造——这更直观。

您已经给出了这张表:

Skipcond 800 if AC > 0,
Skipcond 400 if AC = 0,
Skipcond 000 if AC < 0

现在,如果我们想将其理解为更直观的 if...then 结构,请将其与 Jump 结合起来,反转条件(在注释中),并缩进 Jump 以便直观地表明它仅在注释中的条件为 true 时执行。以下是查找其中一种 SkipCond 格式的方式:

Skipcond 800 / if AC <= 0, then:
         Jump Address
/ Else continue here

我发现这更容易阅读。强调一点:我建议在 SkipCond 指令旁边添加一条注释,在其中反转它何时跳过的含义,以便表达它何时不是跳过,而是实际上进行了跳跃。

如果我们将此方法应用于伪代码循环,我们会得到:

          Load  One    / AC := 1
While,    Store X
          Subt  Ten    / AC := X - 10
          SkipCond 000 / If X - 10 >= 0, then exit loop
                   Jump EndWhile
          Load  X
          Add   One
          Jump  While
EndWhile, Halt

SkipCond can be confusing. It may help to assume you will always use it in combination with a Jump that follows right below it. This is anyhow the most common way to use it, but also helps to see it as an if...then construct -- which is more intuitive.

You have already given this table:

Skipcond 800 if AC > 0,
Skipcond 400 if AC = 0,
Skipcond 000 if AC < 0

Now if we want to understand this as the more intuitive if...then construct, combine it with a Jump, invert the meaning of the condition (in a comment), and indent the Jump so to visually indicate it is executed only when the condition in the comment is true. Here is how that looks for one of those SkipCond formats:

Skipcond 800 / if AC <= 0, then:
         Jump Address
/ Else continue here

I find this easier to read. To stress the point: I propose here to add a comment next to the SkipCond instruction where you invert the meaning of when it skips, so to express when it does not skip, but actually makes the jump.

If we apply this approach to your pseudocode loop, we get this:

          Load  One    / AC := 1
While,    Store X
          Subt  Ten    / AC := X - 10
          SkipCond 000 / If X - 10 >= 0, then exit loop
                   Jump EndWhile
          Load  X
          Add   One
          Jump  While
EndWhile, Halt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文