Lower all characters by a fixed amount and add that right before printing in a loop
Credit to @LukStorms, who uses a similar approach in his answer for the Hello World challenge.
(STN
used for Space, Tab, and New-line respectively.)
Pushing the values for letters is always 11 bytes (i.e. pushing the value 65 for the character 'A' is SSSTSSSSSTN
; pushing the value 122 for the character 'z' is SSSTTTTSTSN
). When you want to output large amount of text this can be expensive. Instead, you can lower the values of all the characters you want to print by a fixed amount, and then in the loop to print them add this fixed amount.
This can be done with the following code (let's assume the fixed value is 100 in this case):
- Push all values for the characters (minus the fixed amount 100) in reverse order
NSSN
(Create a Label_0; basically starting the loop)
SSSTTSSTSSN
(Push the fixed amount 100)
TSSS
(Add the top two values of the stack together)
TNSS
(Pop and print the now correct value as character)
NSNN
(Jump to Label_0; go to the next iteration of the loop)
This will stop the program with an error (which is allowed according to the meta) as soon as it tries to do the Add (TSSS
) with nothing more on the stack. I've used this to golf this answer of mine (see items 5 and 6 of Things I did to lower the byte-count).
Whether a fixed amount of 100 is the shortest approach depends on what you are printing. @LukStorm for example used 107 in his Hello World answer.
Note that copying the top value (SNS
) for two of the same adjacent characters (like the l
in Hello
), or copying values from another position can still be used in addition to this to golf more bytes.