Luigi Auriemma

aluigi.org (ARCHIVE-ONLY FORUM!)
It is currently 19 Jul 2012 11:54

All times are UTC [ DST ]





Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: divisions optimizations
PostPosted: 22 Sep 2009 11:12 

Joined: 13 Aug 2007 21:44
Posts: 4068
Location: http://aluigi.org
just a few lines for talking about a typical assembly optimization which is usually encountered during reversing: the divisions.
the reason behind the existence of such optimizations is that the divisions (div/idiv) take many cpu cycles and so this is a way for saving some nanoseconds at the price of a not easily readable assembly code (which affects only who reverses it).

take the following 2 examples:
Code:
    B8 79 78 78 78      MOV EAX,78787879
    F7 E9               IMUL ECX
    C1 FA 03            SAR EDX,3
Code:
    B8 79 78 78 78      MOV EAX,78787879
    F7 E9               IMUL ECX
    C1 FA 03            SAR EDX,3
    8B C2               MOV EAX,EDX
    C1 E8 1F            SHR EAX,1F
    03 C2               ADD EAX,EDX
    8B D0               MOV EDX,EAX
    C1 E2 04            SHL EDX,4
    03 D0               ADD EDX,EAX

the first one is a simple "eax / 17" (division by 17) and the second is "eax % 17" (modulo 17), nice eh? :)

if someone wants to go deeper in this thing I suggest the Agner's document http://www.agner.org/optimize/optimizing_assembly.pdf in the section "Integer division by a constant".


Top
 Profile  
 
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC [ DST ]


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
You cannot post attachments in this forum

Search for: