0

程序 6:相同 编写一个 HLA 汇编语言程序,该程序实现一个函数,该函数正确识别所有四个参数何时相同并在 AL 中返回一个布尔值(当所有四个值相等时为 1;否则为 0)。此函数应具有以下签名:

程序 theSame(w:int16;x:int16;y:int16;z:int16); @无显示;@无框;

下面显示的是一个示例程序对话。

喂我 W:215 喂我 X:215 喂我 Y:480 喂我 Z:91 不一样。铝= 0

喂我 W:0 喂我 X:0 喂我 Y:0 喂我 Z:0 相同。铝= 1

喂我 W:0 喂我 X:221 喂我 Y:100 喂我 Z:40 不一样。铝= 0

**我的输出几乎是正确的,但是当我输入时

w : 2
x : 2
y : 2
z : 1
"the same" instead of "NOT the same"

请帮助我可能遗漏的任何想法。** 我不确定注册推送和弹出是否是问题。

 program Same;
    #include ("stdlib.hhf");

static
    iDataValue1 : int16 := 0;
    iDataValue2 : int16 := 0;
    iDataValue3 : int16 := 0;
    iDataValue4 : int16 := 0;
procedure theSame( w : int16; x : int16; y : int16; z : int16); @nodisplay; @noframe;

static
    
    returnAddress : dword;
    temp: int16;
    
begin theSame;
    pop (returnAddress);
    pop (temp);
    pop (z);
    pop (y);
    pop (x);
    pop (w);
    
    push (returnAddress);
    push (BX);
    
  //Perform Subtask
    mov (z, BX);
    cmp (y, BX);                // Compare z & y
    jne ReturnZero;
    
    mov (y, BX);
    cmp (x, BX);                // Compare y & x
    je ReturnOne;
    jmp ReturnZero;
    
    mov (x, BX);
    cmp (w, BX);                // Compare x & w
    je ReturnOne;
    jmp ReturnZero;

ReturnOne:
    mov (1, AL);
    jmp ExitSequence;

ReturnZero:
    mov (0, AL);
    jmp ExitSequence;

ExitSequence:
    pop (BX);
    ret();
    
end theSame;

begin Same;
    stdout.put ("Feed Me W: ");
    stdin.get (iDataValue1);
    stdout.put ("Feed Me X: ");
    stdin.get (iDataValue2);
    stdout.put ("Feed Me Y: ");
    stdin.get (iDataValue3);
    stdout.put ("Feed Me Z: ");
    stdin.get (iDataValue4);
    
    push (iDataValue1);
    push (iDataValue2);
    push (iDataValue3);
    push (iDataValue4);
    
    call theSame;
    
    cmp (AL, 1);
    je NumbersAreSame;
    jmp NumbersAreDifferent;

NumbersAreSame:
    stdout.put ("Same. AL = 1");
    jmp EndProgram;

NumbersAreDifferent:
    stdout.put ("Not the same. AL = 0");
    stdout.newln();
    jmp EndProgram;
EndProgram:

end Same;
4

1 回答 1

0

环境

  • HLA(高级汇编器 - HLABE 后端,LD 链接器)版本 2.16 build 4463(原型)
  • Ubuntu 20.10

解决方案

  • 调用时将五个值压入堆栈theSame(),它们是return addresswxyz。然而,在序言中,theSame()当前从堆栈中弹出了六个值。
begin theSame;
    pop (returnAddress);
    pop (temp);
    pop (z);
    pop (y);
    pop (x);
    pop (w);
  • 上面看到的附加值pop (temp);导致temp变量存储传入的值 for zz存储传入的值 for yy存储传入的值 for xx存储传入的值 for w,并将w下一个随机值存储在堆栈上。删除pop (temp);指令将导致您的示例案例通过。
Feed Me W: 2
Feed Me X: 2
Feed Me Y: 2
Feed Me Z: 1
Not the same. AL = 0
  • 但是,由于比较逻辑中的错误,以下情况仍然会失败。
Feed Me W: 1
Feed Me X: 2
Feed Me Y: 2
Feed Me Z: 2
Same. AL = 1
  • 在下面的代码中,如果zyx相等,则代码将跳转到ReturnOne而不检查 的值w。这可以通过删除第一个je ReturnOne;并将第一个更改为jmp ReturnZero;来纠正jne ReturnZero;
mov (z, BX);
cmp (y, BX);                // Compare z & y
jne ReturnZero;
    
mov (y, BX);
cmp (x, BX);                // Compare y & x
je ReturnOne;
jmp ReturnZero;
    
mov (x, BX);
cmp (w, BX);                // Compare x & w
je ReturnOne;
jmp ReturnZero;

例子

program Same;
#include("stdlib.hhf");

procedure theSame(w: int16; x: int16; y: int16; z: int16); @nodisplay; @noframe;  
begin theSame;
    pop(EDX);          // Return Address
    pop(z);
    pop(y);
    pop(x);
    pop(w);
    push(EDX);         // Return Address
    mov(z, BX);
    cmp(y, BX);        // Compare z & y
    jne ReturnZero;
    mov(y, BX);
    cmp(x, BX);        // Compare y & x
    jne ReturnZero;
    mov(x, BX);
    cmp(w, BX);        // Compare x & w
    jne ReturnZero;
    mov(1, AL);
    jmp ExitSequence;
    ReturnZero:
        mov(0, AL);
    ExitSequence:
        ret();
end theSame;

begin Same;
    stdout.put("Feed Me W: ");
    stdin.geti16();
    push(AX);
    stdout.put("Feed Me X: ");
    stdin.geti16();
    push(AX);
    stdout.put("Feed Me Y: ");
    stdin.geti16();
    push(AX);
    stdout.put("Feed Me Z: ");
    stdin.geti16();
    push(AX);
    call theSame;
    cmp(AL, 1);
    jne NumbersAreDifferent;
    stdout.put("Same. AL = 1");
    jmp EndProgram;
    NumbersAreDifferent:
        stdout.put("Not the same. AL = 0");
    EndProgram:
end Same;
于 2021-08-06T19:43:04.773 回答