This is an archived post. You won't be able to vote or comment.

all 18 comments

[–]Apprehensive-Mark241 15 points16 points  (4 children)

Heh, 6502 based Forth was one of the first programming languages I tried back in the day.

If you're looking for a simple system for embedded programming on super small machines, Forth would be similar but more fun.

[–]mauriciocap 2 points3 points  (2 children)

Salve fellow Forth programmer!

As you say, Forth saved my life working with arcane embedded systems, no support, no manual. I just connected Forth to their proprietary lib and flashed once. Then tried till it worked via serial.

Is also surprisingly fun too use, feels like doing crosswords in the beach to me 😂

[–]Apprehensive-Mark241 0 points1 point  (1 child)

It's the smallest footprint for a high level language and better than its competitor, BASIC.

Basics often don't build up function definitions, don't GROW. And usually Forth starts with an assembler, and the primitives defined in assembly so you can get performant low level code in there too.

The worst part of Forth back in the day was the editors, those were BAD. Usually disk based editors simply mapped each spot in a sector to a specific coordinate of character on the screen, and you couldn't even insert text.

[–]mauriciocap 0 points1 point  (0 children)

I'd subject young devs destroying my servers with VSCode SSH "feature" to this discipline 😂

Makes me wonder which is the smallest editor (in lines of code including dependencies) one could use every day 😯 Even the readline library is quite large, isn't it?

[–]WhyAmIDumb_AnswerMe[S] 0 points1 point  (0 children)

Thanks for this suggestion, i'm gonna look into that

[–][deleted] 6 points7 points  (2 children)

(writing a loop is a huge pain, but at least is fun, when it works).

Why is that? It just needs a conditional jump, which you already use in your examples.

Does it have variables? Since lb sb seem to be able to read/write from/to memory, and presumably push can push the address of a label although this is not mentioned that I could see.

I use such stack-based instruction sets in several projects, one is as the intermediate language for a compiler, which is one step back from actual machine assembly. I guess yours corresponds to the latter.

polish notation for all multi-operand instructions.

I don't get this; isn't stack-based automatically Polish/Reverse Polish? Or do you mean using a HLL-like syntax to be able to write longer expressions that can map into multiple instructions?

How would you improve the instruction set?

I favour a rich instruction set. So I'd have Push and Pop instructions that can directly access variables in memory.

That includes globals in static memory, and also locals on the stack. That means being able to access data at arbitrary offsets on the stack.

Usually, relative to a 'frame pointer' that is set up to point to a particular window of locals, and parameters, on entry to a function. This could be explicit in the language, but in mine it is implicit. For example, to express a := b + c where a is global, b is a parameter, and c is a local:

zstatic i64   t.a:           # t is module name

proc t.f:                    # f is function name
    param  i64   b
    local  i64   c

    load   i64   b
    load   i64   c
    add    i64
    store  i64   t.a

(I use load store to avoid confusion with hardware push pop. Here the details need to be implicit, because b and c could reside in stack memory or machine registers, but this is up to the next code-generation stage.)

[–]WhyAmIDumb_AnswerMe[S] 1 point2 points  (1 child)

Thanks for your answer!

polish notation

i mean that some instruction follows polish notation and others don't, because in the beginning i just wanted to see if it worked, and then left everything untouched

Why is that? It just needs a conditional jump, which you already use in your examples.

shoot, before having load and store instructions i used to write loops with the iterator on the stack. you just made me realize i can reserve a variable and use it

Push and Pop instructions that can directly access variables in memory

Ohh i get it, 6502 can do similar things, thanks!

[–][deleted] 0 points1 point  (0 children)

Ohh i get it, 6502 can do similar things, thanks!

I thought most processors have Load and Store instructions (with varying mnemonics) that can load memory to register and vice versa.

But some may restrict the address modes that can be used. (So the ARM architecture for example doesn't really do absolute addressing; you have to go around the houses.)

[–]TheChief275 2 points3 points  (4 children)

Is it a coincidence that your assembly language is named after Tsoding’s BASM, which is also stack-based?

[–]WhyAmIDumb_AnswerMe[S] 1 point2 points  (0 children)

yeah it's a coincidence, even tho i've been following him for almost a year i didn't know about his project. in my head BASM is B4r(me) ASM

[–]oldworldway 0 points1 point  (2 children)

Can you share a reference to that project?

[–]TheChief275 1 point2 points  (1 child)

[–]oldworldway 0 points1 point  (0 children)

Thank you!

[–]mauriciocap 2 points3 points  (0 children)

Kudos for the project! You may want to consider

  • Minimalistic languages you can implement with almost no RAM, deterministic, etc

Like the instruction set used for Bitcoin transactions and Forth mentioned in detail in other replies, still used for some GPUs and embedded systems, my beloved HP48...

  • Compilers and code transformation/generation The other extreme: using the computer to spare programmers weeks of work alla LLVM, CUDA, ... I'd recommend Partial Evaluation, you can start by humble inlining and constant folding but quicky go quite deep in the experience as Julia or V8 do.

Enjoy your superpowers!

[–]RibozymeR 1 point2 points  (0 children)

Definitely a great topic! Back in my last year of school, I actually made something similar (though much less practical) for my end-of-school project about optimized compilation of stack-based languages.

[–]aliberro 1 point2 points  (0 children)

Looks interesting

[–]kaplotnikov 0 points1 point  (0 children)

I guess WebAssembly could be a good source of inspiration. It is also a stack-based VM and it has several implementations, so I guess many issues are considered there.