我正在编写 HLA(高级程序集)以找到最大值。并在 AH 寄存器中返回。我的结果一直给我“-52”而不是最大值,在这种情况下为 20。我将不胜感激任何让我重回正轨的意见。
// 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
结果应该是 20。相反,我得到 -52。
这是代码的 C++ 等价物
#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;
}