It is currently Sat Mar 06, 2021 6:35 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Need help or advice
PostPosted: Sun Aug 11, 2013 12:06 pm 
Offline
New User
New User

Joined: Thu Mar 10, 2011 12:18 pm
Posts: 9
Hi!

Just want to unroll a loop, like this:

Code:
!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:
!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


Top
 Profile  
Reply with quote  
 Post subject: Re: Need help or advice
PostPosted: Sun Aug 11, 2013 1:17 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3712
Location: Netherlands
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)
viewtopic.php?p=359802#p359802

_________________
macOS 10.15 Catalina, Windows 10


Top
 Profile  
Reply with quote  
 Post subject: Re: Need help or advice
PostPosted: Sun Aug 11, 2013 1:27 pm 
Offline
New User
New User

Joined: Thu Mar 10, 2011 12:18 pm
Posts: 9
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:
             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


Top
 Profile  
Reply with quote  
 Post subject: Re: Need help or advice
PostPosted: Sun Aug 11, 2013 1:59 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3712
Location: Netherlands
I don't know why you got the result you got.

This x86 code shows 60 (10 x 6)
Code:
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:
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:

_________________
macOS 10.15 Catalina, Windows 10


Top
 Profile  
Reply with quote  
 Post subject: Re: Need help or advice
PostPosted: Sun Aug 11, 2013 2:38 pm 
Offline
New User
New User

Joined: Thu Mar 10, 2011 12:18 pm
Posts: 9
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 )


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye