HLA:为什么EAX寄存器在退出递归过程时保持零值?

时间:2014-07-26 03:52:30

标签: stored-procedures recursion hla

我正在尝试使用递归过程来计算高级程序集中的A-B。计算差值后,它将存储在EAX寄存器中,以便在程序结束时显示。

我的问题:在退出程序之前,寄存器EAX和EBX中的值是正确的,但我不明白为什么当A大于B时EAX始终为零。

ret()命令是否有引起这种情况的原因?我的代码出了什么问题?有人请帮助我。

以下是示例代码:

program MainSubtractionFunction;
#include( "stdlib.hhf" );
static
    iDataValue1 : int32 := 0;
    iDataValue2 : int32 := 0;
    DifferenceInt : int32 :=69;
procedure recursiveSubtraction( a: int32; b : int32 ); @nodisplay; @noframe;
static
    returnAddress : dword;
    value: int32;
begin recursiveSubtraction;
    pop( returnAddress );
    pop( b );           
    pop( a );
    push( returnAddress );
    mov (a, EAX);
    mov (b, EBX);
    CompareB:
    cmp (EBX, 0);
    je ExitSequence;
    CompareA:
    cmp (EAX, 0);
    je AEqualsZero;
    NeitherEqualZero:
    sub (1, EAX);
    sub (1, EBX);
    push(EAX);
    push(EBX);
    call recursiveSubtraction;
    AEqualsZero:
        neg (EBX);
        mov (EBX, EAX);
        jmp ExitSequence;
    BEqualsZero:
        jmp ExitSequence;
    ExitSequence:
        ret();
    end recursiveSubtraction;
begin MainSubtractionFunction;
    stdout.put( "Feed Me A: " );
    stdin.get( iDataValue1 );
    stdout.put( "Feed Me B: " );
    stdin.get( iDataValue2 );  
    push( iDataValue1 );
    push( iDataValue2 );
    call recursiveSubtraction;
    mov(EAX, DifferenceInt);
    stdout.put("RecursiveSubtraction of A-B = ",DifferenceInt, nl);
    stdout.put("EAX = ",EAX, nl);
    stdout.put("EBX = ",EBX, nl);
    stdout.put("ECX = ",ECX, nl);   
end MainSubtractionFunction;

1 个答案:

答案 0 :(得分:0)

环境

  • HLA(高级汇编程序-HLABE后端,POLINK链接器) 版本2.16内部版本4413(原型)
  • Windows 10

注意

  • 此问题中的递归算法适用于以下情况:positive - positivepositive - negativenegative - positive,但不适用于情况negative - negative

解决方案

  • 问题是从递归调用返回到recursiveSubtraction后,代码将始终顺序地继续到AEqualsZero标签。
NeitherEqualZero:
    sub(1, EAX);
    sub(1, EBX);
    push(EAX);
    push(EBX);
    call recursiveSubtraction;
AEqualsZero:
    neg(EBX);
    mov(EBX, EAX);
    jmp ExitSequence;
  • 要解决此问题,请在调用jmp ExitSequence之后再添加一个recursiveSubtraction
NeitherEqualZero:
    sub(1, EAX);
    sub(1, EBX);
    push(EAX);
    push(EBX);
    call recursiveSubtraction;
    jmp ExitSequence;
AEqualsZero:
    neg(EBX);
    mov(EBX, EAX);
    jmp ExitSequence;

示例

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

procedure recursiveSubtraction(A: int32; B: int32); @nodisplay; @noframe;
begin recursiveSubtraction;
    pop(EDX); // Return Address
    pop(EBX);           
    pop(EAX);
    push(EDX); // Return Address
    CompareB:
        cmp(EBX, 0);
        je ExitSequence;
    CompareA:
        cmp(EAX, 0);
        je AEqualsZero;
    NeitherEqualZero:
        dec(EAX);
        dec(EBX);
        push(EAX);
        push(EBX);
        call recursiveSubtraction;
        jmp ExitSequence;
    AEqualsZero:
        neg(EBX);
        mov(EBX, EAX);
        jmp ExitSequence;
    BEqualsZero:
        jmp ExitSequence;
    ExitSequence:
        ret();
end recursiveSubtraction;

begin MainSubtractionFunction;
    stdout.put("Feed Me A: ");
    stdin.geti32();
    push(EAX);
    stdout.put("Feed Me B: ");
    stdin.geti32();
    push(EAX);
    call recursiveSubtraction;
    stdout.put("RecursiveSubtraction of A-B = ", (type int32 EAX), nl);  
end MainSubtractionFunction;