Page 1 of 1

Need help or advice

Posted: Sun Aug 11, 2013 12:06 pm
by basil99
Hi!

Just want to unroll a loop, like this:

Code: Select all

!mov ecx, ebx

!horloop:

!lodsd
!mov [edi], eax
!add edi, 4

!dec ecx
!jnz horloop
Counter is always limited by some given value, say, 1024. So, idea is to calculate the size
of "chunk", then jump to the entry point, skipping some part of repeating "chunks"

Code: Select all

!xor edx, edx
!mov eax, offset endchunk
!sub eax, offset startchunk                ; Get size of code "chunk" into EAX
!mul ecx                                       ; EAX = code size * counter
!mov ecx, offset endofcode              ; ECX points to the offset of unrolled code sequence
!sub ecx, eax                                 ; ECX = entry point
!jmp ecx                                        ; go to entry point


!startchunk:

!lodsd
!mov [edi], eax
!add edi, 4

!endchunk:

; repeat 1023 times

!lodsd
!mov [edi], eax
!add edi, 4
!lodsd
!mov [edi], eax
!add edi, 4
!lodsd
!mov [edi], eax
!add edi, 4
!lodsd
!mov [edi], eax
!add edi, 4
!lodsd
!mov [edi], eax
!add edi, 4

.....


!lodsd
!mov [edi], eax
!add edi, 4

!endofcode:



I use similar code many years ago in DOS era, but this doesn't work in PB.
Got error message on this

!mov eax, offset endchunk

Any ideas why ? What i'm doing wrong here ? Thanks a lot in advance

Re: Need help or advice

Posted: Sun Aug 11, 2013 1:17 pm
by wilbert
I think you would need to remove the word offset

I asume you unroll because of speed reasons.
If I remember correctly, lodsd is not very fast.

Here's a suggestion Thorium gave for copying memory (if that is what you want to do)
http://www.purebasic.fr/english/viewtop ... 02#p359802

Re: Need help or advice

Posted: Sun Aug 11, 2013 1:27 pm
by basil99
wilbert wrote:I think you would need to remove the word offset

I asume you unroll because of speed reasons.
If I remember correctly, lodsd is not very fast.
Yes, i need to unroll to gain more speed. NP ro replace lodsd with mov eax, [esi] / add esi, 4

Also, I tried to remove offset, and debug the value:

Code: Select all

             DebugVariable = 0

	!mov ecx, endchunk
	!sub ecx, startchunk
	!mov [p.v_DebugVariable], ecx
I got the DebugVariable value smth like 186515488, so I think i'm doing wrong

Re: Need help or advice

Posted: Sun Aug 11, 2013 1:59 pm
by wilbert
I don't know why you got the result you got.

This x86 code shows 60 (10 x 6)

Code: Select all

Define Test.i

!mov ecx, 10

!mov eax, endchunk
!sub eax, startchunk
!imul eax, ecx

!mov [v_Test], eax

MessageRequester("",Str(Test))
End

!startchunk:

!lodsd
!mov [edi], eax
!add edi, 4

!endchunk:
The x64 approach shows 70

Code: Select all

Define Test.i

!mov rcx, 10

!lea rax, [endchunk]
!lea rdx, [startchunk]
!sub rax, rdx
!imul rax, rcx

!mov [v_Test], rax

MessageRequester("",Str(Test))
End

!startchunk:

!lodsd
!mov [rdi], eax
!add rdi, 4

!endchunk:

Re: Need help or advice

Posted: Sun Aug 11, 2013 2:38 pm
by basil99
Thanks, wilbert

I use wrong syntax for mov [v_DebugVar], ecx. Now I get offset working, and correct chunk size on 32bit ( 10 bytes as you ), also seems jmp ecx works fine.

Think, i can finish this code today ) Thanks again. Its hard to get back into assembly )