TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Creating a Fibonacci Generator in Assembly

50 点作者 willvk超过 6 年前

7 条评论

emersonrsantos超过 6 年前
In Forth language would be:<p><pre><code> : fib 2dup + ; </code></pre> 2dup word duplicates n and n-1 elements and put it on the top of the stack.<p>+ word takes n and n-1 elements from the stack and sum them. The result goes on top of the stack.<p>Just start with 1 and 1 at the top of stack.
评论 #17950269 未加载
userbinator超过 6 年前
As someone with considerable experience in using x86 Asm (over a decade), it&#x27;s always nice to see more articles on it, but there are a few things I found odd about this one.<p><i>The basis of the algorithm to use was decided to be a space-optimised method using a dynamic programming approach. This effectively means using an array on the stack to hold our values while giving O(n) time and space complexity.</i><p>That isn&#x27;t &quot;space-optimised&quot; at all --- when I think of a Fibonacci example I think of O(1) space. Unless it was chosen specifically to demonstrate array indexing (in which case it would be better to use an algorithm that actually requires an array, like sorting), the choice of algorithm is unusual. The same applies to finding the length of argv[1] before doing the equivalent of an atoi() on it --- if you&#x27;re going to read the bytes of the string through, might as well accumulate and convert, and oddly enough the length appears to be unused too.<p>The unexplained and useless nop is also puzzling. Why is it there? What is it for? The same goes for the many other instructions which aren&#x27;t actually contributing to the calculation, and initially made me think it was compiler output when I first glanced over the code. Writing a program that performs the given task is relatively straightforward even in Asm, but your solution is unnecessarily complex.<p><i>Making things easier to understand using function calls</i><p>I consider this a premature abstraction. A comment or even label will do just fine to delimit the blocks of functionality, as your &quot;functions&quot; are called only once. Your task has 3 parts [basically atoi(), fib(), itoa()+output] so you will have 3 blocks of code. It cannot be any simpler than that. Better is &quot;making things easier to understand by avoiding unnecessary code&quot;.<p>Here is how I would write the first 2 blocks; I wrote this without testing at all so it could have errors, but it is really quite simple:<p><pre><code> mov esi, [esp+8] ; get argv[1] xor eax, eax ; clear the upper bits xor ecx, ecx ; will hold converted value atoiloop: lodsb ; get a character cmp al, &#x27;0&#x27; jb fibinit cmp al, &#x27;9&#x27; ja fibinit imul ecx, ecx, 10 lea ecx, [ecx+eax-&#x27;0&#x27;] jmps atoiloop fibinit: jecxz invalid ; can&#x27;t do fib(0) xor eax, eax inc eax ; eax = 1 cdq ; edx = 0 fibloop: xadd eax, edx loop fibloop ; now eax and edx will contain fib(n) and fib(n-1) </code></pre> Observe that I chose the specific registers for a reason: esi&#x2F;al will be used by lodsb, ecx by the looping instructions, and edx for xadd. This is one of the things that compilers are not very good at, and also why carefully written Asm can easily beat them.<p>If you want to learn more Asm I recommend any assembler besides GAS, and looking at some examples from advanced users: <a href="http:&#x2F;&#x2F;www.hugi.scene.org&#x2F;compo&#x2F;compoold.htm" rel="nofollow">http:&#x2F;&#x2F;www.hugi.scene.org&#x2F;compo&#x2F;compoold.htm</a> The demoscene in its smaller-size competitions has lots of particularly knowledgeable users too.
评论 #17948537 未加载
评论 #17948961 未加载
评论 #17948669 未加载
exikyut超过 6 年前
FWIW, the article mentions a possible followup &quot;using ... MMX, XMM or SIMD&quot;. (To clarify, XMM is SSE.) SIMD is noted last as though it&#x27;s the newest development, when that would arguably be is at least AVX, and AVX-512 to be specific.
评论 #17948559 未加载
pq0ak2nnd超过 6 年前
every programmer needs to do this at some point in their life. it is too easy to take take modern languages for granted. but i&#x27;m a little disappointed that you dogged `make`. i think the programming landscape would be a lot more effective if people took the time to learn about what already exists (not just make but autoconf&#x2F;automake&#x2F;configure) instead of reinventing the wheel every three years.
评论 #17948043 未加载
emilfihlman超过 6 年前
<p><pre><code> movl $1, %eax movl $1 %eax copy 0 into register eax movl $0, %ebx movl $0 %ebx copy 1 into register ebx </code></pre> Surely the explanations are flipped?
评论 #17948050 未加载
saagarjha超过 6 年前
I think you might be missing the NULL that terminates argv in your stack diagram.
评论 #17949958 未加载
sdfsdfsdff超过 6 年前
It&#x27;s also a problem in 7 Billion Humans or Human Resource Machine (I forgot which - they are both great anyway), fun programming games that also use a kind of assembly language.