0

我需要帮助解决这种推送流行的情况。我正在尝试编写一个 HLA 汇编语言程序,该程序实现一个函数,该函数正确识别所有四个参数何时相同并在 AL 中返回一个布尔值(当所有四个值相等时为 1;否则为 0)。

到目前为止,这是我的代码:

program same;
#include("stdlib.hhf");
static
w : int16 :=0;
x : int16 :=0;
y : int16 :=0;
z : int16 :=0;
temp : int16;

// Function theSame //
procedure theSame(w:int16; x:int16; y:int16; z:int16); @nodisplay; @noframe;
static
returnAddress : dword;
similar: int16;

begin theSame;
pop(returnAddress);
pop(similar); // Padding - stacking must be 32 bit align
pop(z);
pop(y);
pop(x);
pop(w);
push(returnAddress);

stdout.put( "w = ", w, nl );
stdout.put( "x = ", x, nl );
stdout.put( "y = ", y, nl );
stdout.put( "z = ", z, nl );


stdout.put( "cmp w x ", nl );
mov(w,AX);
cmp(AX,x);
je Equal;
jne Notequal; // (w==x)

stdout.put( "cmp x y ", nl );

mov(x,AX);
cmp(AX,y);
je Equal;
jne Notequal; // (x==y)

stdout.put( "cmp y z ", nl );

mov(y,AX);
cmp(AX,z);
je Equal;
jne Notequal; // (y==z)

stdout.put( "cmp z w ", nl );

mov(z,AX);
cmp(AX,w);
je Equal;
jne Notequal; // (w==z)

Equal:
stdout.put( "equal", nl );
mov(1,AX);
push(similar); // padding
jmp ExitSequence;

Notequal:
stdout.put( "not equal", nl );
mov(0,AX);
push(similar); // padding
jmp ExitSequence;

ExitSequence:
ret();

end theSame;

begin same;
stdout.put("Feed Me W: ");
stdin.get(w);
push(w);
stdout.put("Feed Me X: ");
stdin.get(x);
push(x);
stdout.put("Feed Me Y: ");
stdin.get(y);
push(y);
stdout.put("Feed Me Z: ");
stdin.get(z);
push(z);

call theSame;
cmp(AX, 1);
je YESEQUAL;
jmp NOEQUAL;

YESEQUAL:
stdout.put("True", nl);
jmp EndProgram;


NOEQUAL:
stdout.put("False", nl);
jmp EndProgram;

stdout.newln();
EndProgram:

end same;

但是,输出结果如下:

输入:

喂我 W: 8
喂我 X:7
喂我 Y:6
喂我 Z:5

输出:

宽:-165
X: 8
是:7
Z:6
cmp wx
不相等

有没有理由为什么它彼此具有相同的价值?混淆 push 和 pop 的工作原理。谢谢你。

4

1 回答 1

2
pop(similar); // Padding - stacking must be 32 bit align

显然您需要删除此pop(similar)指令。如果你离开它,它会获得z参数的值。并让w从堆栈中获取垃圾!不要忘记也删除push(similar)说明。

入口时堆栈的布局:

x  x  5  6  7  8  -165
----
^     ^  ^  ^  ^
|     z  y  x  w  all 4 words
dword return address
pop(returnAddress);
pop(z);
pop(y);
pop(x);
pop(w);
push(returnAddress);
于 2021-07-25T21:53:37.630 回答