我按升序对 HLA 汇编语言中的 3 个数字进行排序。我没有得到正确的答案。我在 HLA 中的逻辑有什么问题。例如,如果我输入 12、1、50,它应该排序为 1、12、50。相反,我得到以下结果:
Gimme X: 12
Gimme Y: 1
Gimme Z: 50
swapXandY swapXandZ swapYandZ After Sorting, X = 50, Y = 1, Z = 12
这是C++中的代码
#include <iostream>
void Sort(int &a, int &b, int &c){
if(a>b){
int tmp = a;
a = b;
b = tmp;
}
if(a>c){
int tmp = a;
a=c;
c = tmp;
}
if(b>c){
int tmp = b;
b=c;
c=tmp;
}
return;
}
这是我的 HLA 代码:
program SwapperProgram;
#include ("stdlib.hhf");
static
iValue1 : int16;
iValue2 : int16;
iValue3 : int16;
//-------------------------------------------
procedure swapper (var x : int16; var y : int16; var z : int16); @nodisplay; @noframe;
static
dReturnAddress : dword;
iTemp : int16;
dEDXRegister : dword := 0; // preserve EDX
dECXRegister : dword := 0; // preserve ECX
dEBXRegister : dword := 0; // preserve EBX
dEAXRegister : dword := 0; // preserve EAX
begin swapper;
mov (EAX, dEAXRegister);
mov (EBX, dEBXRegister);
mov (ECX, dECXRegister);
mov (EDX, dEDXRegister);
pop( dReturnAddress ); // This is the return address
pop (ECX); // This is the address of z
pop (EBX); // This is the address of y
pop (EAX); // This is the address of x
push (dEAXRegister);
push (dEBXRegister);
push (dECXRegister);
push (dEDXRegister);
cmp (EAX, EBX); // if (x > y)
jg swapXandY; // swap x and y
cmp (EAX, ECX);
jg swapXandZ;
cmp (EBX, ECX);
jg swapYandZ;
swapXandY:
stdout.put ("swapXandY ");
mov ([EAX], EDX);
mov (DX, iTemp);
mov ([EBX], EDX);
mov (DX, [EAX]); // *EAX = DX
mov (iTemp, DX);
mov (DX, [EBX]);
swapXandZ:
stdout.put ("swapXandZ ");
mov ([EAX], EDX);
mov (DX, iTemp);
mov ([ECX], EDX);
mov (DX, [EAX]);
mov (iTemp, DX);
mov (DX, [ECX]);
swapYandZ:
stdout.put ("swapYandZ ");
mov ([EBX], EDX);
mov (DX, iTemp);
mov ([ECX], EDX);
mov (DX, [EBX]);
mov (iTemp, DX);
mov (DX, [ECX]);
jmp ExitSequence;
ExitSequence:
pop (EDX);
pop (ECX);
pop (EBX);
pop (EAX);
push (dReturnAddress);
ret();
end swapper;
//---------------------------------------------------------------------------------
begin SwapperProgram;
stdout.put ("Gimme X: ");
stdin.get (iValue1);
stdout.put ("Gimme Y: ");
stdin.get (iValue2);
stdout.put ("Gimme Z: ");
stdin.get (iValue3);
lea( EAX, iValue1 ); // get address of iValue1
push( EAX );
lea( EAX, iValue2 ); // get address of iValue2
push( EAX );
lea( EAX, iValue3 ); // get address of iValue3
push( EAX );
call swapper;
stdout.put ("After Sorting, X = ", iValue1);
stdout.put (", Y = ", iValue2);
stdout.put (", Z = ", iValue3);
end SwapperProgram;
//---------------------------------------------------------------------------------
我的代码哪里错了?我知道我错过了一个逻辑