TE
TechEcho
Home
24h Top
Newest
Best
Ask
Show
Jobs
English
GitHub
Twitter
Home
Smallest saturating arithmetic functions in x86 assembly: Is 45 bytes the limit?
23 points
by
SlyShy
over 14 years ago
2 comments
ssp
over 14 years ago
You can shave a byte off the unsigned addition by using the sbb instruction<p><pre><code> 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 </code></pre> By (ab)using the short pop instructions we can save another byte:<p><pre><code> 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</code></pre>
vilda
over 14 years ago
Collapse
Not sure about the goals, but instructions like sar, btc, and loop have significant performance penalty.
评论 #2036298 未加载