Reputation: 25
I am writing a HLA (High Level Assembly) to find the largest value. And return in AH register.My results keep giving me "-52" instead of the largest value, 20 in this case. I would appreciate any input to put me back on track.
// File: Largest
program MainLargest;
#include ("stdlib.hhf");
static
iValue1 : int8 := 1;
iValue2 : int8 := 10;
iValue3 : int8 := 20;
iValue4 : int8 := 0;
procedure largest (value1 : int8; value2 : int8; value3 : int8; value4 : int8); @nodisplay; @noframe;
static
iReturnAddress : dword;
iResult : int8;
iTemp : int16;
iRegisterValue : dword;
begin largest;
mov (EBX, iRegisterValue);
pop (iReturnAddress);
pop (iTemp);
pop (iTemp); // This is value4
mov (iTemp, BX);
mov (BL, value4);
pop (iTemp); // This is value3
mov (iTemp, BX);
mov (BL, value3);
pop (iTemp); // This is value2
mov (iTemp, BX);
mov (BL, value2);
pop (iTemp); // This is value1
mov (iTemp, BX);
mov (BL, value1);
push (iReturnAddress);
push (iRegisterValue);
mov (value1, DH);
mov (DH , iResult);
mov (iResult, CH);
cmp (value2, CH);
jg putInAH;
cmp (value3, CH);
jg putInAH;
cmp (value4, CH);
jg putInAH;
jmp ExitSequence;
putInAH:
mov (CH, AH);
jmp ExitSequence;
ExitSequence:
pop (EBX);
ret();
end largest;
begin MainLargest;
stdout.put ("Largest (");
stdout.puti8 (iValue1);
stdout.put (", ");
stdout.puti8 (iValue2);
stdout.put (", ");
stdout.puti8 (iValue3);
stdout.put (", ");
stdout.puti8 (iValue4);
stdout.put (") equals ");
call largest;
stdout.puti8 (AH);
end MainLargest;
The result
Largest (1, 10, 20, 0) equals -52
The result should be 20. Instead I am getting -52.
Here is the C++ equivalent of the code
#include <iostream>
using namespace std;
int largest (int value1, int value2, int value3, int value4) {
int result = value1;
if (value2 > result) {
result = value2;
}
if (value3 > result) {
result = value3;
}
if (value4 > result) {
result = value4;
}
return result;
}
int main()
{
int value1 = 1;
int value2 = 10;
int value3 = 20;
int value4 = 0;
int x = largest (value1, value2, value3, value4);
cout << "Largest (" << value1 << "," << value2 << "," << value3 <<"," << value4 << ") equals " << x << endl;
return 0;
}
Upvotes: 0
Views: 296
Reputation: 39701
call largest;
You are not passing the values to the procedure largest.
Check with hla, but it's either
call largest(iValue1,iValue2,iValue3,iValue4);
or
procedure largest (iValue1 : int8; iValue2 : int8; iValue3 : int8; iValue4 : int8); @nodisplay; @noframe;
Your code does not enough compares.
cmp (value2, CH); jg putInAH; cmp (value3, CH); jg putInAH; cmp (value4, CH); jg putInAH;
If value2 happens to be greater than CH
(and it does because 10 is GT 1), you still have to see if neither value3 nor value4 are greater then value2, before you can conclude that value2 is the greatest.
The C++ code has code blocks that fall through into each other and with multiple assignments to result, but your code just has the one assignment in putInAH.
pop (iReturnAddress); pop (iTemp); pop (iTemp); // This is value4
Why do you need this extra pop (iTemp)
? There should be no dword alignment issues given the dword return addresss and the 4 word arguments totalling 2 dwords.
Upvotes: 1