Here is my errored hla program: it always print nothing... it should prompt user for two int8 integers and print the integer quotient after calling divideRec, increment the result with properly pushing and popping in stack aligns...cannot figure out where should I fix..
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;
here is the corresponding c code:
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);
}
This program always ends up outputting with zeros or nothing. I believe there are something wrong with the stack align, pushes and pops that I can't figure out. please kindly offer some helps.