Kind of a response to https://blog.yoshuawuyts.com/state-machines/ .
I don’t really agree with the sentiment that state machines are clunky to implement in most languages. Most C-style languages have a loop and a switch-statement, which I think is enough. But maybe I’m missing something?
I guess the assembly shows that yeah, any language that uses a simple switch/long-ifs, and some values, can make a simple state machine. :)
I think what is “clunky” is that you still require a bit of boilerplate in C-style langs.
I also think that it highly depends on how simple the state the machine is X)
I’m not seeing the advantage of the assembly code presented that C and some C macros can’t duplicate. Also, the use of λ instead of call isn’t cute since λ is generally used to declare a function, not to call it.
You want to abstract C further with macros on top of it? this code further by rewriting it in C and using macros?
The point is, the macros add just a thin layer of abstraction. C abstracts way more - and then you suggest using macros too?
I use λ just because I associate it with functions. It can be anything you want it to be…! It isn’t supposed to be cute. And “instead of call”. call <routine> <args> is not valid. λ is meant to abstract away calling convention to make code easier to read.
call <routine> <args>
What would you think is cute, @spc476? :P
What you wrote isn’t buying you anything that a C compiler (or Rust compiler, since that’s what you are referencing) won’t buy you. Why not use the function itself as the state value?
GREEN: mov rax,YELLOW
YELLOW: mov rax,RED
RED: mov rax,GREEN
Also, if you are writing everything in assembly, why even bother with the normal calling convention? I can’t tell for certain if you are or not, because everything is masked behind a macro (which you do not provide the source for) but even so, it appears you are mixing 32-bit and 64-bit code. You push and pop EBP, yet your are using RDI for the paramter to exit. You don’t need to use RBP for a stack frame (even in 32-bit code, you don’t need to use EBP for a stack frame). Yes, it makes referencing locals a bit easier but it’s not mandatory.
And as for what I consider cute, coroutine support for C in assembly.
The answer to all your whys is simple: I’m learning :)
You missed my point unfortunately. Also, that is a wicked idea to literally use the function itself…it drives the point further: assembly seems really fit for this (now as you’ve shown me: even without macros)!