Smallest saturating arithmetic functions in x86 assembly: Is 45 bytes the limit?
blog.regehr.org
blog.regehr.org
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