

Smallest saturating arithmetic functions in x86 assembly: Is 45 bytes the limit? - SlyShy
http://blog.regehr.org/archives/278/comment-page-1

======
ssp
You can shave a byte off the unsigned addition by using the sbb instruction

    
    
        00000000 <original>:
        0:	8b 44 24 04          	mov    0x4(%esp),%eax
        4:	31 d2                	xor    %edx,%edx
        6:	f7 d2                	not    %edx
        8:	03 44 24 08          	add    0x8(%esp),%eax
        c:	0f 42 c2             	cmovb  %edx,%eax
        f:	c3                   	ret    
    
        00000010 <one_shorter>:
         0:	8b 44 24 04          	mov    0x4(%esp),%eax
        14:	03 44 24 08          	add    0x8(%esp),%eax
        18:	89 c3                	mov    %eax,%ebx
        1a:	19 c3                	sbb    %eax,%ebx
        1c:	09 d8                	or     %ebx,%eax
        1e:	c3                   	ret    
    

By (ab)using the short pop instructions we can save another byte:

    
    
       0000001f <another>:
       1f:	58                   	pop    %eax
       20:	5b                   	pop    %ebx
       21:	01 d8                	add    %ebx,%eax
       23:	89 c3                	mov    %eax,%ebx
       25:	19 c3                	sbb    %eax,%ebx
       27:	09 d8                	or     %ebx,%eax
       29:	83 ec 08             	sub    $0x8,%esp
       2c:	c3                   	ret

------
vilda
Not sure about the goals, but instructions like sar, btc, and loop have
significant performance penalty.

~~~
daeken
When you're aiming for small code, performance is the last of your worries. A
couple years back I decided to implement a complete bootloader that could read
a kernel from NTFS in the MBR -- 510 bytes usable. While performance would've
been nice, just getting it into that space was a massive(ly fun) chore.

