r/TuringComplete • • 24d ago

Help needed; how to solve the stack level for Symphony?

I've been stuck on the level where you need to make a stack that can push and pull for a while now. I tried a few things myself, nothing. Asked ChatGPT, nothing. Looked up a full guide, found a really good one, didn't work. Is it bugged, or does anyone have a working solution?

1 Upvotes

38 comments sorted by

3

u/Medium-Sized-Jaque 24d ago

Your sp register stands for stack pointer and it starts at 0. So if you subtract 1 it will overflow to the last memory position, so your stack is the end of the memory module and you know your instructions won't be overwritten by it. Then you store the value. Every time you push you just subtract again and store again. To pop you just reverse it. Load the value then add 1 so the stack pointer is now pointing to the next value to be popped.

0

u/DruidBtd 24d ago

How do I use the stack pointer? The guides said to use [sp], but it doesn't seem to work when trying to use it to point to the different registers

3

u/Medium-Sized-Jaque 24d ago

The stack pointer IS a register. You use the SP as the address when you store or load to the memory. You want to store the value of r1 with the address in sp. And it's just like other instructions with the instruction first, the destination, which in this case is the value in the SP register, then the source, which is r1.

store_32 [sp], r1

Then to load it from memory into r1 

load_32 r1, [sp]

1

u/DruidBtd 24d ago

Though, why doesn't something like mov r3, [sp] work?

2

u/Medium-Sized-Jaque 24d ago

Mov is set up to copy from one register to another, the RAM instructions are set up to copy from the registers into memory. In the stack level you're just using the instructions you set up in the RAM level.

The only difference is SP is dedicated so every time you use the stack later it automatically works and pints to the highest value on the stack.

1

u/DruidBtd 24d ago

Yeah, I get that, but why does something like load_16 r3, [sp] work to send from r3 into the RAM adress from sp, but not mov [sp], r3 to move from r3 to the adress sp in the register memory

2

u/Medium-Sized-Jaque 24d ago

If you do mov sp, r3 you're telling the system move the value from r3 to sp. When you do store_32 [sp], r3 you're telling the system to move the value from r3 to memory and sp is just the address it uses.

1

u/DruidBtd 24d ago

Is the [] not just syntax for taking the value at that address instead of the value itself? So wouldn’t mov [sp], r3 copy from r3 to the register that sp points to?

1

u/TarzyMmos 24d ago

Its different when you're moving to/from the memory and to/from the registers I guess. That's just how the syntax is. If you want to change it you can by just editing the isa specs. Although idk if it'll let you in the levels but it should let u in the sandbox.

1

u/DruidBtd 23d ago

I've been trying to figure it out and landed on this

stack:

in r1

cmp r1, 0

je pop

push:

sub sp, sp, 2

store_16 \[sp\], r1

jmp stack

pop:

load_16 r1, [sp]

add sp, sp, 2

mov r2, r1

out r2

jmp stack

But I can't figure out how to fix it, cause it's not working.. It fails at cycle 3. Do you have any working code?

→ More replies

1

u/Medium-Sized-Jaque 24d ago

I don't know why it needs [] honestly. But in the previous levels how you set up the switches and decoders make it so when you give the instruction mov it lets the data move from one register to another and when you give the instruction store it lets the data move into the memory block.

1

u/DruidBtd 23d ago

I've been trying to figure it out and landed on this

stack:

in r1

cmp r1, 0

je pop

push:

sub sp, sp, 2

store_16 [sp], r1

jmp stack

pop:

load_16 r1, [sp]

add sp, sp, 2

mov r2, r1

out r2

jmp stack

But I can't figure out how to fix it, cause it's not working.. It fails at cycle 3. Do you have any working code?

→ More replies

0

u/DruidBtd 24d ago

I'll try that, my own solution and from ChatGPT was using the registers for it instead of the main memory, and idk why the guide code didn't work, but I'll try again! Thanks!

3

u/Medium-Sized-Jaque 24d ago

The older version had a designated stack and some older guides used registers but they weren't THE registers. ChatGPT probably read those and got the details messed up.

1

u/Medium-Sized-Jaque 18d ago

Were you ever able to figure it out?

2

u/DruidBtd 18d ago

Yeah, I had to use the main memory and I tried the code from someone else that used load_16, it gave error, so I tried again with load_32 and it worked, thanks for checking in

1

u/TarzyMmos 23d ago

If u want more in depth help I'd recommend asking on the discord! They respond pretty quickly and are very helpful imo!

1

u/Fastpast93 5d ago edited 5d ago

It's very easy once you understand what the purpose of the stack is. I don't know how much you know so I'll start from plain ol' Symphony.

Every pushed/popped value is 32 bits, which is 4 bytes (32 bits / 1 byte = 4). This means you must change the address by 4 to change it by 4 bytes.

The easiest way to do so is to subtract 4 from the stack pointer and store a value there. That places your pushed value at address -4. Want to push again? Subtract 4 again and store the new value. That looks like this:

sub sp, sp, 4
store_32 [sp], register

Since the sp starts at 0, that is selecting the first 4 bytes of your program. You can't store it there, so subtracting 4 moves the pointer down and allows you to store in an empty region (unless you are using an absurd amount of memory already, which you aren't at this stage)

So figure what this does:

sub sp, sp, 4
mov r1, 5
store_32 [sp], r1

sub sp, sp, 4
mov r1, 6
store_32 [sp], r1

If you couldn't tell, this does the following twice:
- Move sp down
- Set r1 to 5
- Store r1 to memory at address of stack pointer

From this, you can almost certainly guess how pop is implemented. If not, here is an example:

load_32 r1, [sp]
add sp, sp, 4
load_32 r1, [sp]
add sp, sp, 4

And this does the following:
- Move sp up
- Set r1 to most recent pushed value
- Move sp up
- Set r1 to the value pushed directly before the last

Since you have already dealt with branching, you will be able to figure out how to implement the stack. It's very easy-just two instructions each for pushing and popping. Good luck!

1

u/Pretty-Potential4733 4d ago

add a value (x) if input is 0 then

output memory [x] and -1 to x

else

store input [x] and +1 to x

(consider x as a register)