我正在尝试使用递归过程来计算高级程序集中的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;
答案 0 :(得分:0)
环境
注意
positive - positive
,positive - negative
和negative - 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;