flat assembler
Message board for the users of flat assembler.

Index > Heap > I've put my tiny bit of assembly knowledge to use!

Author
Thread Post new topic Reply to topic
mattst88



Joined: 12 May 2006
Posts: 260
Location: South Carolina
mattst88

_________________
My x86 Instruction Reference -- includes SSE, SSE2, SSE3, SSSE3, SSE4 instructions.
Assembly Programmer's Journal
Post 20 Mar 2008, 01:39
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
nice to see someone finally doing something useful Wink
Post 20 Mar 2008, 02:11
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2915
Location: [RSP+8*5]
bitRAKE
Little bit of knowledge is a dangerous thing. Laughing
(seems like you are using more than you're letting on.)

_________________
¯\(°_o)/¯ unlicense.org
Post 20 Mar 2008, 03:06
View user's profile Send private message Visit poster's website Reply with quote
Goplat



Joined: 15 Sep 2006
Posts: 181
Goplat
Is it possible for val to be 0? In that case, the C implementation returns -1, but the assembly will just return whatever was already in eax - bsr doesn't change the destination register if the source is 0.

Although, the C implementation has an even bigger possible bug - it loops forever if val is negative! (Gotta be careful with those signed integers...)
Post 20 Mar 2008, 17:06
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17287
Location: In your JS exploiting you and your system
revolution
Goplat wrote:
bsr doesn't change the destination register if the source is 0.
You need to be careful there, officially the destination register is undefined. While the current batch of CPU's may leave the destination register untouched it is not guaranteed.
Post 20 Mar 2008, 17:10
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17287
Location: In your JS exploiting you and your system
revolution
What was wrong with?:
Code:
RADEONLog2:
        bsrl    %edi, %eax
        ret    
If you need to return -1 for a zero input you can use this:
Code:
RADEONLog2:
    xor     ecx,ecx
     bsr     eax,edi
     setz    cl
  neg     ecx
 or      eax,ecx
     ret    
BTW: I changed it to the more usual Intel syntax. I just plain hate the ridiculous AT&T syntax, the developers should have been shot before releasing it to the world.
Post 20 Mar 2008, 21:20
View user's profile Send private message Visit poster's website Reply with quote
MCD



Joined: 21 Aug 2004
Posts: 604
Location: Germany
MCD
revolution wrote:
If you need to return -1 for a zero input you can use this:
Code:
RADEONLog2:
     xor     ecx,ecx
     bsr     eax,edi
     setz    cl
  neg     ecx
 or      eax,ecx
     ret    


well, if you are planning to write this code for 80686+ (Pentium Pro or newer, which is almost always the case if you plan to use a radeon card), then this can even be simplified to:
Code:
RADEONLog2:
  mov     ecx,-1
      bsr     eax,edi ; always modifies the zero flag
     cmovz   eax,ecx
     ret
    


revolution wrote:

BTW: I changed it to the more usual Intel syntax. I just plain hate the ridiculous AT&T syntax, the developers should have been shot before releasing it to the world.

100% aggree on that. AFAIK inline assembly in C can also be in intel style syntax, but I don't know whether this is allowed by the kernel-policy. Anybody knows?

_________________
MCD - the inevitable return of the Mad Computer Doggy

-||__/
.|+-~
.|| ||
Post 11 May 2008, 21:09
View user's profile Send private message Reply with quote
MCD



Joined: 21 Aug 2004
Posts: 604
Location: Germany
MCD
ok. I have made a small patch for the linux kernel to use this bsr/cmovz approach for the log2 also for the 32bit i386 architecture (x86_64 had this from the beginning i think) in the "include/asm-i386/bitops.h".

AFAIR this has already been changed in 2.6.24 and newer kernels when they where merging the 32bit and 64bit x86 trees.

Damn, I think this post should be moved to the linux section and linked with this thread.

anyway, here its is


Description: patch for the linux kernel (tested with 2.6.23).
unfortunately the extension .patch is not allowed.

Download
Filename: i386-cmov_bitsearches-patch.txt
Filesize: 795 Bytes
Downloaded: 79 Time(s)


_________________
MCD - the inevitable return of the Mad Computer Doggy

-||__/
.|+-~
.|| ||
Post 12 May 2008, 02:21
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
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 vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.