flat assembler
Message board for the users of flat assembler.

flat assembler > Main > Fibonacci sequence in "2 instructions"

Author
Thread Post new topic Reply to topic
moveax41h



Joined: 18 Feb 2018
Posts: 47
I saw a post which stated that the Fibonacci sequence can be computed using 2 asm instructions... However, when I implemented it, I realized that the registers must also be reset so is it really the case that the sequence can occur using only 2 instructions:

Code:
format PE console
entry main
main:
        xor eax, eax
        mov edx, 1
        .loop:
        xadd eax, edx
        loop .loop
    



Is there a more compact way to achieve this? The only way I can think of is if the OS hands off execution to the program with the guarantee that eax is 0 and edx is 1. But even still, we need to be concerned with ecx as well or the loop instruction may malfunction.[b/]

_________________
-moveax41h
Post 25 Aug 2018, 18:44
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16127
Location: Hyperborea
If REP could be used with arbitrary instructions then we could do this:
Code:
rep xadd eax,edx    
But it won't work.
Post 25 Aug 2018, 20:55
View user's profile Send private message Visit poster's website Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2311
Location: Usono (aka, USA)
revolution wrote:
If REP could be used with arbitrary instructions ...


It can, but it won't do what you think. Older cpus will ignore such prefixes. And it has been used to pair with or extend other instructions. "rep ret" (AMD optimization, replaced by "ret 0" in some later cases) or SSE2's "pause" (aka, "rep nop") or even SSE4's "lzcnt" (which IIRC is just "rep bsr").

(You probably were already aware of this. I don't claim to know all such corner cases.)
Post 26 Aug 2018, 02:05
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16127
Location: Hyperborea
I think that in those cases Intel consider the entire byte sequence to be a single instruction. So it isn't being used as a REP prefix, but instead a reuse of the REP byte to make a new instruction.

It is also possible that in the future the REP XADD byte sequence might but redefined as some new instruction also.
Post 26 Aug 2018, 04:09
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2018, Tomasz Grysztar.

Powered by rwasa.