I'm a fan of small code snippets that does usefull things. For example, this snippet places the sign of eax (-1, 0 or 1) in edx:

cdq
cmp eax, edx
jle @F
inc edx
@@:

It's 5 bytes large and can be used for line drawing algorithms. It only uses one branch also. You can do it without branches with something like this:

cdq
inc edx
cmp eax, edx
sbb edx, 0

If you have any small, smart snippet you would like to share, please post it here.
Posted on 2002-08-14 03:08:51 by gliptic
``````
xor eax, eax
``````

That zero's eax :stupid:
Posted on 2002-08-14 03:41:39 by bazik
Posted on 2002-08-14 05:49:10 by JCP
gliptic, the second you code look as wrong.

``````
test eax,eax
setg al
sete dl
lea edx,[2*eax+edx-1]
; dl={-1,0,1}
``````
Posted on 2002-08-15 12:29:27 by Nexo
Check if top two high order bits of reg/mem are complimentary:

``````[size=12]rol reg/mem, 1
jo @label
;jno to check if they are =[/size]``````

Check if high bit and low bit of reg/mem are complimentary:

``````[size=12]ror reg/mem, 1
jo @label
;jno to check if they are =[/size]``````

Non destructive check two values for different cases of 0:

``````[size=12]cmp reg0, 1
inc reg1
dec reg1
ja @label    ; reg0 != 0 and reg1 !=0
jna @label   ; either reg0 or reg1 == 0
[/size]``````
Posted on 2002-08-15 14:02:06 by iblis
This code is too shameful to place at madwizard.org :grin:
``````xor eax, eax
inc eax``````
EAX = 1 :stupid: ??? better than mov eax, 1 ???
Posted on 2002-08-17 17:21:06 by stryker

gliptic, the second you code look as wrong.

``````
test eax,eax
setg al
sete dl
lea edx,[2*eax+edx-1]
; dl={-1,0,1}
``````

Try this one( one byte shorter; doesn't spoil eax; about the same speed for later stones):
``````
cmp eax,1
cdq
cmc
sbb edx,0
;edx = -1;0;1 depending on sign of eax; eax unchanged
``````
Posted on 2002-08-17 19:43:40 by The Svin
``````cmp eax,1
mov edx, eax
sar edx, 31
cmc
sbb edx,0``````
*might* be faster on older processors. :stupid:
Posted on 2002-08-17 19:53:34 by stryker
Nexo, Svin:
``````	cdq
cmp eax,1
sbb edx,1
stryker, the shift instruction effect the carry flag:
``````	mov edx,eax
sar edx,31
cmp eax,1
sbb edx,1
``````
:grin:

Non register version:
``````	cmp memreg1,0
pushfd
pop memreg2
shl memreg2,24
sar memreg2,30
xor memreg2,1``````
Posted on 2002-08-17 22:45:53 by bitRAKE
LOL :grin: never thought of sar and the flags affected. :grin:
Posted on 2002-08-17 23:37:31 by stryker
Very well done, bitRake.
It's both shorter and faster.
May be exchange the 2 first instructions to give possibility
for superscalar execution?
It's NP on <= PMMX, but might be possible on later proc..
Posted on 2002-08-18 07:32:57 by The Svin

``````
or edx,-1
cmp eax,1
sbb edx,-2 ;add 2 if not carry , if carry add one
shl eax,1  ;check forsign
sbb edx,0
``````

bye
eko
Posted on 2002-08-18 12:44:40 by eko
I thought of something like that, eko.
But it also spoils eax, as in Nexo's code.
Good try, but I'm for bitRake last design for the moment for explained reasons.
Posted on 2002-08-18 13:40:35 by The Svin
``````

mov edx, eax
sar edx, 31
neg eax
rcl edx, 1

``````

this is code of friend of mine

bye
eko
Posted on 2002-08-19 19:04:17 by eko
Beautiful code, eko !

However, I prefer adc reg,reg to rcl:

mov edx, eax
sar edx, 31
neg eax

JC
Posted on 2002-08-21 07:14:47 by MCoder
To keep eax:

mov edx, eax
sar edx, 31
neg eax
neg eax

JC
Posted on 2002-08-21 07:21:38 by MCoder
The smallest possible is this:

cdq
dec eax
jl @F
inc edx
@@:

;Put 'inc eax' here to preserve eax
Posted on 2002-08-22 00:44:44 by gliptic

cdq
neg eax

eax preserved:

cdq
neg eax
neg eax

JC
Posted on 2002-08-22 02:16:59 by MCoder
The Assembly Gems Page

Well-known link, but may be it will be new for someone of you.
You'll find a bunch of useful and small snippets there.
Posted on 2002-08-27 03:49:38 by Four-F
my all time favourite is:-

xor eax,ecx
xor ecx,eax
xor eax,ecx

which swaps the values in the two registers. these days it's not the most optimised way of doing it, but very elegant and neat. It's one of the few snippets I saw and had to write it out long hand with pen and paper to convince myself this worked.

umbongo
Posted on 2002-08-27 05:49:30 by umbongo