TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Smallest saturating arithmetic functions in x86 assembly: Is 45 bytes the limit?

23 pointsby SlyShyover 14 years ago

2 comments

sspover 14 years ago
You can shave a byte off the unsigned addition by using the sbb instruction<p><pre><code> 00000000 &#60;original&#62;: 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 &#60;one_shorter&#62;: 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 &#60;another&#62;: 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>
vildaover 14 years ago
Not sure about the goals, but instructions like sar, btc, and loop have significant performance penalty.
评论 #2036298 未加载