I am sorting 3 numbers in HLA Assembly Language in Ascending order. I am not getting the right answer. What is wrong with my logic in HLA. For Example, if I enter 12, 1, 50, it should sorted 1, 12, 50. Instead,I am getting following result:
Gimme X: 12
Gimme Y: 1
Gimme Z: 50
swapXandY swapXandZ swapYandZ After Sorting, X = 50, Y = 1, Z = 12
Here is the code in 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;
}
Here is my HLA code:
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;
//---------------------------------------------------------------------------------
Where in my code is wrong? I know I am missing a logic