Click to See Complete Forum and Search --> : improper operand type


laura
November 6th, 2004, 11:51 PM
trying to simulate insertion sort in descending order in ASM.. based off this Java code:


public static void insertionSort (int [] a) {
// method to sort using the insertion sort
for (int k = 1; k < a.length; k++)
{
int item = a[k];

int i = k-1;
while ((i >= 0) && a[i] < item)
{
a[i+1] = a[i];
i--;
}
a[i+1] = item;
}

in ASM:

_asm
{
mov esi, 1 ; k = 1
mov eax, a ; pointer to a[k-1]
forloop:
mov ebx, [eax + 4] ; item = a[k]
mov ecx, esi ; i = k
dec ecx ; i = k-1
cmp ecx, 0 ; compare i with 0
jl next ; if less than skip loop
cmp [eax],ebx ; compare a[i] with ebx (item)
jge next ; if >= skip loop
whileloop:
mov edx, [eax] ; a[i]
mov [eax+4], edx ; a[i+1] = a[i]
sub eax, 4 ; i--, move to previous element
dec ecx ; decrement the counter
cmp ecx, 0 ; compare with 0
jl next ; if less than 0 break out of loop
cmp [eax],ebx ; compare a[i] with item
jge next ; if >= skip loop
jmp whileloop ; otherwise go through while loop again
next:
mov [eax+4],ebx ; a[i+1] = item
inc esi ; k++
cmp esi, 5 ; compare with 5 (which is the size of the array)
jge done ; if >= we're done
jmp forloop ; otherwise begin the for loop again
done:
}

I fixed my illegal operand problem by using a separate register to store the value [eax].. anyway.. now this code compiles, but doesn't seem to do a **** thing. The array is exactly the same.

Amit_Roy
November 8th, 2004, 12:35 AM
hi ,
have u fixed your code ?
i tried to fixed it and found 2 errors see below
I have checked this code in vc++ 6.0 it is working fine



_asm{
mov ESI, 1 //; k = 1
mov EBX, b // ; pointer to a[k-1]
forloop:
mov EAX, [EBX + 4] // ; item = a[k]
mov ECX, ESI // ; i = k
dec ECX // ; i = k-1
cmp ECX, 0 //; compare i with 0
jl next // ; if less than skip loop
cmp [EBX],EAX //; compare a[i] with BX (item)
jge next // ; if >= skip loop
whileloop:
mov Edx, [EBX] // ; a[i]
mov [EBX+4], Edx // ; a[i+1] = a[i]
sub EBX, 4 // ; i--, move to previous element
dec ECX // ; decrement the counter
cmp ECX, 0 //; compare with 0
jl next // ; if less than 0 break out of loop
cmp [EBX],EAX //; compare a[i] with item
jge next // ; if >= skip loop
jmp whileloop // ; otherwise go through while loop again
next:
mov [EBX+4],EAX // ; a[i+1] = item
inc ESI
add EBX , 8 // ; k++
cmp ESI, 6 //; compare with 5 (which is the size of the array)
jg done // ; if >= we're done
jmp forloop // ; otherwise begin the for loop again
done:
}