Assembly Marie汇编语言中的while语句 输入一个数字并将其存储在x中 输入一个数字并将其存储在y中 当x
While语句使用if-goto-label转换为汇编语言。我们可以说明使用C:Assembly Marie汇编语言中的while语句 输入一个数字并将其存储在x中 输入一个数字并将其存储在y中 当x,assembly,marie,Assembly,Marie,While语句使用if-goto-label转换为汇编语言。我们可以说明使用C: input a number and store it in x input a number and store it in y while x < 10 do x =x +1 output value of x; end while output the value of y*2 请注意,在if goto标签样式中,条件x=10b/c我们希望在此条件下退出循环,而cwhile语句告诉我们何时继续循环
input a number and store it in x
input a number and store it in y
while x < 10 do
x =x +1
output value of x;
end while
output the value of y*2
请注意,在if goto标签样式中,条件x<10
被反转为x>=10
b/c我们希望在此条件下退出循环,而cwhile
语句告诉我们何时继续循环。因为这两种感觉(cwhile
与c if goto标签)是相反的,我们选择x<10
的否定,即x>=10
,以退出循环
与C的goto语句相对应,MARIE使用
Jump
汇编指令进行无条件分支
与C的if-goto构造相对应,MARIE使用条件跳过进行条件分支:在多指令序列中使用Skipcond
指令
Skipcond
将AC
累加器寄存器与零进行比较,允许执行下一条指令,或者不允许通过跳过它来执行下一条指令-当它跳过时,它只跳过一条指令:不多,不少。通常下一条指令是用于更改控制流的跳转(它不必是跳转,它可以是您希望有条件跳过的任何其他单个指令)
完成条件分支(if goto),我们将条件跳过指令Skipcond
与无条件分支指令Jump
结合起来。因为我们告诉Skipcond
何时跳过,它只会在Skipcond
不跳过时进行分支。因此,我们从if goto标签表单中否定该条件:
loop1:
if ( x >= 10 ) goto loop1Done; // this is a conditional branch
x++;
print(x);
goto loop1; // this is an unconditional branch
loop1Done:
...
变成:
while ( x < 10 ) {
x++;
print(x);
}
...
if ( x >= 10 ) goto loop1Done;
由于跳过/跳转序列使我们从if goto标签表单中取反(如果您是从C开始的,这是第二次),我们希望在Skipcond;jump
序列中设置x<10
首先,我们应用一些基本的数学:x-10<0
,从关系运算符的两边减去10,请发布您到目前为止所做的尝试。非常感谢,我会尝试一下,然后给您回复
Skipcond 000 // Skip the Jump if AC < 0
Jump loop1Done // Jump may or may not fire, depending on prior Skip
if ( x >= 10 ) goto loop1Done;
loop1,
// if ( x >= 10 ) goto loop1Done;
load X // AC = x
Subt Ten // AC = x - 10
Skipcond 000 // MARIE will skip the next instruction if AC < 0, i.e. x < 10
Jump loop1Done // Will only fire if AC >= 0, i.e. x >= 10
// x++;
Load X // AC = x
Add One // AC = x + 1
Store X // x = AC
Output // outputs the AC (note that the AC == x here)
Jump loop1 // continue the loop
loop1Done, // end of loop, on to next statement after
...
// Variables & Constants
X, Hex 0
Ten, Dec 10
One, Dec 1
if ( x >= 10 ) goto label1; // goes to label1 if x >= 10
... // comes here otherwise, i.e. x < 10
if ( x < 10 ) goto uniqueNewlabel; // goes to uniqueNewLabel if x < 10
goto label1; // goes here (which jumps to label1) if x >= 10
uniqueNewlabel: // comes here if x < 10
if ( x < 10 ) goto somewhere;
Skipcond >= // skips the succeeding Jump on >=
Jump somewhere // goes here on <, so executes the Jump
Skipcond 000 // < that MARIE does have
Jump uniqueNewLabel // fires only when >=, so used to "skip" the next line
Jump somewhere // desired target for when <
uniqueNewLabel,
if ( x < 10 ) goto somewhere;
if ( x <= 9 ) goto somewhere;