6 ms·
I bought a nice little booklet of small x86 gems a few years back[1][2] and this one is my favorite: .loop: xadd rax,rdx loop .loop Fibo
by jbaiter 8y ago
I bought a nice little booklet of small x86 gems a few years back[1][2] and this one is my favorite:
.loop:
xadd rax,rdx
loop .loop
Fibonacci with two instructions.
[1] https://www.amazon.com/dp/1502958082 https://www.amazon.com/dp/1502958082
[2] https://www.xorpd.net/pages/xchg_rax/snip_00.html https://www.xorpd.net/pages/xchg_rax/snip_00.html
- neilsimp1 8y agoCan someone more versed in x86 explain to me how this works? It's been a long time since I've done any assembly, and that was MIPS, but I don't see how there's any sort of exit condition or anything. I'm guessing there's more to xadd than `x + y = z`?
- giancarlostoro 8y agoI don't know much Assembly, but at what point does Fibonacci end? Shouldn't it just keep going?
- booblik 8y agoNo, loop decrements rcx until it reaches zero and then stops
- abhishekjha 8y agoBut rcx has not been initialised.
- wruza 8y agoIf all functions took (rax, rbx, rcx, ...) and you had loop statement that defrements rcx and jumps unless zero, then how would you write fib-function’s body?
- abhishekjha 8y agoYou raise a good question. At one time you would have only one loop running through the system as per my understanding. I am a little lost here.
- zero_one_one 8y agoI think this is the biggest problem with learning x86 assembly (or ARM or anything else) on modern systems (or more specifically modern operating systems). It’s sometimes difficult to think about the assembly code in situ when you start to think about the operating system doing a ton of context switching and paging etc. in the background, which can distract your thought process from what’s right in front of you (as well as the operating system’s software interrupts / system calls on top of the basic ISA, which is another abstraction!) Older systems had the currently running program as the entire context of the system at that point in time - in a similar way to embedded programming, which is imho a much easier realm to learn assembly in once you’ve got a bit of basic electronics under your belt!
- asveikau 8y agoThe whole point of how interrupt handling works is that it returns back to the same state of the program already in progress when finished. The abstraction is such that the interrupted program doesn't need to care. Even in those "old" systems of single address spaces and no protection, you're constantly getting timer interrupts, interrupts for I/O, etc., which your application might not have installed its own handler for.
- zero_one_one 8y agoI agree - my main point is that an OS is ‘just a program’ as well I suspect we’re both making a similar point in a roundabout way - the operating system is both another layer of abstraction on top of the Instruction Set, while also making the programming process for that chipset somewhat easier (providing software interrupts etc. at the expense of bare metal understanding). My argument is loosely that modern (x64) assembly is not so much targeting hardware as it is programming into a software abstraction (the operating system).
- boomlinde 8y agoBut it has a value regardless
- zero_one_one 8y agoThe code above I believe is the point to jump to when the register has been assigned some value for x. Think along the same lines as a function call once the pre-amble is out of the way (or a ‘goto’ by someone who knows when to use it!)
- pjc50 8y agoThis is correct, from the snippet given - what the other commentators are failing to say is that they're assuming the registers are initialised "offstage" by some other part of the program.
- dangerbird2 8y agoIt's a hardware register, so it has always has a value (in 64-bit mode, at least), whether or not the program explicitly sets it.
- bonzini 8y ago"xadd r1,r2" sets r2=r1 and r1=r1+r2 at the same time.
- giancarlostoro 8y agoHaving barely any assembly experience, and looking up what xadd means[0] it really is quite a nice little snippet of code. [0] https://stackoverflow.com/a/30131285 https://stackoverflow.com/a/30131285
- zero_one_one 8y agoI’d like to add to this comment to say that the author has a fantastic assembly course available on udemy - very thorough and practical (teaching the basics in a win32 environment) https://www.udemy.com/x86-asm-foundations/ https://www.udemy.com/x86-asm-foundations/
- vortico 8y agoIs that series endorsed by xorpd or an account just called xorpd? Udemy is known for allowing anyone to steal, upload, and profit from video series. EDIT: Looks like it is. https://www.xorpd.net/pages/x86_adventures.html https://www.xorpd.net/pages/x86_adventures.html
- zero_one_one 8y agoI think it’s created by the man himself - having been through it it’s a very thorough coverage of the basic instruction set without delving too much into the windows specifics (it uses FASM syntax from what I recall)
- xorpd 8y agoYes, I made the videos a few years ago. This video series is mostly for beginners as it goes pretty slowly. The exercises are open source and can be found here: https://github.com/xorpd/asm_prog_ex https://github.com/xorpd/asm_prog_ex I think that more experienced developers can take the instruction set and bash through the exercises.
- psyc 8y agoI was confused until I noticed the "x" in front of "add".
- partycoder 8y agointeresting but i prefer Binet's formula