LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

How exactly do labels and GOTO work?

Root / Programming Questions / [.]

VakoreCreated:
Confusing since
@level1
Data "11111"
Data "1   1"
Data "111 1
Data "1   1"
Data "11111"

@lvl2
'You get idea right?

DIM currentLevel$[5, 5]'I think dis how arrays work

For j = 0 to 5
  For i=0 to 5
  c$=""
  Restore @level1, c$
  currentLevel$[j][i] = c$'been awhile since code on SB if this wrong I look back at code via SBAPI
  Next
Next
Vs.
@gameLoop
If scene$=="game" then gameLoopFunc

GOTO @gameLoop
I'm going to make an interpeter for Javascript so I must know. Thank you! Also I am on mobile so excuse the poor grammar.

Labels aren't strictly tied to flow nor data. They are names during pre-compilation given to line numbers to improve your code readability. Basically (no pun intended), the GOTO/GOSUB/RESTORE and other instructions that take labels, during pre-compilation labels get converted to line numbers. Now after this step, all these instructions operate purely on line numbers, example:
01 @LOOP
02 PRINT "SCOOP"
03 GOTO @LOOP
Gets converted to:
01 PRINT "SCOOP"
02 GOTO 1
Similarly:
01 RESTORE @DATA
02 @DATA
03 DATA 1,2
04 DATA 9999,10000
Gets converted to:
01 RESTORE 2
02 DATA 1,2
03 DATA 9999,10000
So RESTORE simply starts reading DATA from a point. That's consistent with the behaviour of the following program:
01 RESTORE @TEST
02 @TEST
which errors.

So goto, restore, and gosub just jump to a line of code and reads/ executes it from there? And also, when does it end? Thanks for answering my questions.

So goto, restore, and gosub just jump to a line of code and reads/ executes it from there? And also, when does it end? Thanks for answering my questions.
For GOTO and RESTORE, they don't end (or of you want to think of it like this, they end when you do the instruction with another label) The way GOSUB works is by pushing the current line to a stack, and jumping to the label/line. then, when RETURN is found, we a pop line number from the stack and jump to it. Example: (not SB)
01 GOTO 5
03 PRINT K
04 RETURN
05 GOSUB 3
06 PRINT "DONE"
If you follow the flow, you would jump to line 5, push 5 to the stack and jump to 3, and in 4, since we have a RETURN, we pop the 5 from stack, and we jump to it (plus one to avoid running it again), and continue as usual (print "DONE") hope its understandable im sick af

In a version of BASIC that my dad used years ago (don't know which BASIC srry) GOTO used line numbers instead of lables.

So goto, restore, and gosub just jump to a line of code and reads/ executes it from there? And also, when does it end? Thanks for answering my questions.
For GOTO and RESTORE, they don't end (or of you want to think of it like this, they end when you do the instruction with another label) The way GOSUB works is by pushing the current line to a stack, and jumping to the label/line. then, when RETURN is found, we a pop line number from the stack and jump to it. Example: (not SB)
01 GOTO 5
03 PRINT K
04 RETURN
05 GOSUB 3
06 PRINT "DONE"
If you follow the flow, you would jump to line 5, push 5 to the stack and jump to 3, and in 4, since we have a RETURN, we pop the 5 from stack, and we jump to it (plus one to avoid running it again), and continue as usual (print "DONE") hope its understandable im sick af
I don't follow. I suggest you explain when you feel better. Get well soon!

Somewhat off-topic fun fact I thought would fit best here: you can use GOSUB without RETURN 2^14 power (16384) times before the stack overflows

Somewhat off-topic fun fact I thought would fit best here: you can use GOSUB without RETURN 2^14 power (16384) times before the stack overflows
ah i've been wondering how to get to stack overflow in my smilebasic web browser