这是我错误的 hla 程序:它总是不打印任何内容...它应该提示用户输入两个 int8 整数并在调用 divideRec 后打印整数商,通过正确推送和弹出堆栈对齐来增加结果...无法弄清楚应该在哪里我修。。
program prog1;
#include( "stdlib.hhf" );
static
n : int8 := 0;
d : int8 := 0;
procedure divideRec( numerator: int8; denominator : int8 ); @nodisplay; @noframe;
static
iReturnAddress : dword;
iResult : int8;
temp : int16;
iRegisterValue : dword;
begin divideRec;
mov( EBX, iRegisterValue );
pop( iReturnAddress );
pop( temp);
//junk
//this is denominator
pop( temp);
mov(temp,BX);
mov(BL, denominator);
//this is numerator
pop(temp);
mov(temp,BX);
mov(BL, numerator);
push(iReturnAddress);
push(iRegisterValue);
//subtask
mov(0,DH);
mov(numerator,AL);
cmp(AL,denominator);
jge recursiveCall;
recursiveCall:
mov(numerator,BL);
sub(denominator,BL);
mov(BL, numerator);
push(BX);
push(temp);
call divideRec;
inc(DH);
jmp ExitSequence;
ExitSequence:
pop(EBX);
ret();
end divideRec;
begin prog1;
stdout.put("Provide a numerator: ");
stdin.get(n);
stdout.put("Provide a denominator: ");
stdin.get(d);
//16-bits
//n
mov(0,BX);
mov(n,BL);
push(BX);
//d
mov(0,BX);
mov(d,BL);
push(BX);
mov(0,BX);
push(BX);
call divideRec;
stdout.put("divide( ",n,", ",d, ") = ");
stdout.puti8(DH);
end prog1;
这是相应的c代码:
int main(){
int n, d;
printf("gimme a numerator:");
scanf("%d",&n);
printf("gimme a denominator:");
scanf("%d",&d);
int result = divideRec(n,d);
printf("the quotient is %d", result);
return(0);
}
int divideRec(int numerator, int denominator){
int result = 0;
if(numerator>=denominator){
result = 1+(divideRec(numerator-denominator, denominator));
}
return(result);
}
这个程序总是以零输出或什么也不输出。我相信堆栈对齐、推送和弹出有问题,我无法弄清楚。请提供一些帮助。