How exactly do labels and GOTO work? (Page 1) ● SmileBASIC Source Forums

Register
What's going on?? See: Site Update March 2022

# How exactly do labels and GOTO work?

• #1 ✎ 158 Vakore 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. Posted
• #2 ✎ 407 hakke RNG Hidden Easter Eggs QSP Contest 2 Contest Participant I participated in the second SmileBASIC Source QSP Contest! Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength 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. Posted
• #3 ✎ 158 Vakore 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. Posted
• #4 ✎ 407 hakke RNG Hidden Easter Eggs QSP Contest 2 Contest Participant I participated in the second SmileBASIC Source QSP Contest! Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength
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
Posted
• #5 ✎ 1580 rando Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Third Year My account is over 3 years old Website Avatar Taboo I didn't change my avatar for 180 days Website In a version of BASIC that my dad used years ago (don't know which BASIC srry) GOTO used line numbers instead of lables. Posted
• #6 ✎ 158 Vakore
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!
Posted
• #7 ✎ 1580 rando Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Third Year My account is over 3 years old Website Avatar Taboo I didn't change my avatar for 180 days Website 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 Posted
• #8 ✎ 858 IAmRalsei Forum Leader Hidden Achievements Third Year My account is over 3 years old Website Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength
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
Posted