News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

problem compiling Donkey's SortArray in GoAsm

Started by garyhess, November 29, 2012, 07:55:27 AM

Previous topic - Next topic

garyhess

This is probably something simple, but I can't figure it out. I've been experimenting with Donkey's string library and hadn't had any problems until now.

I'm trying to use a new function (SortArray) but it won't compile in GoAsm. I get this error:

Error!
Line 102 of the include file SortArray.asm:-
Cannot jump outside a FRAME envelope or USEDATA area:-
Jmp LONG < Split

OBJ file not made

Errors ocurred.


Here's the SortArray function code:

.code

Compare FRAME item1,item2,ascending,case
uses ecx,esi,eax
mov ecx,[item1]
mov esi,[item2]

mov edx,[ebx + ecx*4]
or edx,edx
jz >E1
mov eax,[ebx + esi*4]
or eax,eax
jz >E1

cmp D[ascending],0
je >S1
cmp D[case],0
je >
invoke lszCmp,eax,edx
jmp >E0
:
invoke lszCmpi,eax,edx
jmp >E0
S1:
cmp D[case],0
je >
invoke lszCmp,edx,eax
jmp >E0
:
invoke lszCmpi,edx,eax
E0:

mov edx,eax
RET
E1:
mov edx,0
RET
ENDF

Exchange FRAME item1,item2
uses ecx,esi
mov ecx,[item1]
mov esi,[item2]
mov edx,[ebx + ecx*4]
push [ebx + esi*4]
mov [ebx + esi*4],edx
pop [ebx + ecx*4]
   ret
ENDF

SortArray FRAME pArray,fAscend,fCase
uses esi, edi, ebx

mov ebx, [pArray]
mov esi, 1
mov edi,[pArray]
mov edi, [edi]
call Split
ret

Split:
mov eax,edi
push esi
push edi
sub eax,esi
jle >E1

mov ecx,esi
inc edi

L1:
inc esi
dec eax
js >D1
invoke Compare,ecx,esi,[fAscend],[fCase]
or edx,edx
js L1

H1:
dec edi
dec eax
js >D1
invoke Compare,ecx,edi,[fAscend],[fCase]
or edx,edx
jns H1
invoke Exchange,esi,edi
jmp L1

D1:
dec esi
invoke Exchange,ecx,esi
cmp edi,[esp]
jge >12
dec esi ; pivot is sorted
mov eax,[esp]
mov [esp],esi
xchg edi,eax
xchg esi,eax
call Split
pop edi
pop esi
Jmp Split

12:
pop edi
pop esi
dec edi
jmp Split

E1:
add esp,8
retn
ENDF


Any ideas? I thought the trouble might be a long jump back to "Split", but I wasn't able to fix it.

wjr

The first call to Split sets up a forward reference and at that point Split is not known to be in or outside a FRAME. I will have to dig a bit further to see what happens once the label Split is encountered, as it does not look like this info is being updated at this point for the later jump test which is important to make sure you are not jumping to, into, or outside a FRAME/USEDATA... potential bug added to the to do list.

One way around this would be to get rid of the forward reference:


SortArray FRAME pArray,fAscend,fCase
uses esi, edi, ebx

jmp >.Continue
Split:
;****Split code here stays the same****
.Continue
mov ebx, [pArray]
mov esi, 1
mov edi,[pArray]
mov edi, [edi]
call Split
ret
ENDF


Nice recursive function within a function. However, a call to Split as global label can be a bit misleading since it does depend upon the arguments to the FRAME. A preferred approach would be to change Split to a local label:


SortArray FRAME pArray,fAscend,fCase
uses esi, edi, ebx

mov ebx, [pArray]
mov esi, 1
mov edi,[pArray]
mov edi, [edi]
call SortArray.Split ;****also change here
ret
.Split
; ...
call SortArray.Split ;****also change here
; ...
jmp <.Split ;****also change here x2
; ...
retn
ENDF


This should make doing these Splits a bit easier.

garyhess

Thanks! That worked. There was one more JMP statement a little further down that also had to be changed.

I'm trying to solve the first 25 Project Euler problems in Assembler (for my own edification). I'm on Problem 22 and I've got the Win32 file stuff figured out and am able to parse the strings in the file, but I think I'm going to skip writing my own string sorting routine for now. (Thanks to Donkey.)

http://projecteuler.net/problem=22

Gary

Donkey

Glad you found them useful Gary,

I have a few more advanced algorithms lying around but for simple sorts I tend to use the C library function qsort which does an adequate job and easily handles arrays. If I need speed I write a one off sort function.

Edgar
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable