0

如何将其转换为汇编语言?if((w == x)&&(x == y)&&(y == z)&&(z == w)) 用一系列 cmp 和 jmp 命令转换成 HLA 形式?

到目前为止我的命令是:

program same;
#include("stdlib.hhf");
procedure theSame(w:int16; x:int16; y:int16; z:int16); @nodisplay; @noframe;

begin theSame;
*if((w == x)&&(x == y)&&(y == z)&&(z == w))*
mov(1, AL);
stdout.put("Same.", nl);
else
mov(0, AL);
stdout.put("Not the same", nl);
endif;
stdout.put("AL: ", (type int16 AL), nl);
end theSame;

begin sameOrnot;
stdout.put("Feed Me W: ");
stdin.get(w);
stdout.put("Feed Me X: ");
stdin.get(x);
stdout.put("Feed Me Y: ");
stdin.get(y);
stdout.put("Feed Me Z: ");
stdin.get(z);
theSame(w, x, y, z);
end sameOrnot;
4

1 回答 1

0
if((w == x)&&(x == y)&&(y == z)&&(z == w))

与按位&运算符不同,&&保证从左到右的评估。因此,如果(w == x)评估为false,则无需进一步评估

  mov ax, w      ; (w == x)
  cmp ax, x
  jne IsFalse
  mov ax, x      ; &&(x == y)
  cmp ax, y
  jne IsFalse
  mov ax, y      ; &&(y == z)
  cmp ax, z
  jne IsFalse
  mov ax, z      ; &&(z == w)
  cmp ax, w
  jne IsFalse
IsTrue:
  ...
IsFalse:
  ...

&&(z == w)部分是多余的!由于保证了从左到右的评估,当我们到达第 4 部分时,我们已经知道w EQ x EQ y EQ z. 关联性决定了这一点w EQ z

我们也可以避免重新加载AX寄存器,因为我们从前面的每个步骤中得到了相等性。

  mov ax, w      ; (w == x)
  cmp ax, x
  jne IsFalse
                 ; AX already holds the value for x (same as w)
  cmp ax, y      ; &&(x == y)
  jne IsFalse
                 ; AX already holds the value for y (same as w)
  cmp ax, z      ; &&(y == z)
  jne IsFalse
IsTrue:
  ...
IsFalse:
  ...
于 2021-07-25T15:40:37.023 回答