Jump to content

Long literals, with continuations: Difference between revisions

no edit summary
(→‎{{header|UNIX Shell}}: Add implementation.)
No edit summary
Line 70:
{{Template:Strings}}
<br><br>
=={{header|6502 Assembly}}==
There is no need for continuation codes, as the language interprets a contiguous data block as a single entity, whether you intended it to be or not. If you had two consecutive strings embedded in the code and you forgot the null terminator on the first one, trying to print just the first string would print both. For example:
 
<lang 6502asm>HelloString:
db "Hello World" ;no null terminator
GoodbyeString:
db "Goodbye World!",0
 
PrintString HelloString ;unimplemented macro.</lang>
The output would be as follows, assuming the <code>PrintString</code> routine uses a null terminator to know when a string ends:
<pre>
Hello WorldGoodbye World!
</pre>
 
Since each string is of variable length, it is much easier to make a lookup table of the elements ordered by atomic number, with each element in the lookup table being a pointer to the actual string consisting of that element's name.
 
<lang 6502asm>Elements:
dw nullString,hydrogen,helium,lithium,beryllium,boron,carbon,nitrogen,oxygen,fluorine
dw neon,sodium,magnesium,aluminum,silicon,phosphorous,sulfur,chlorine,argon
dw potassium,calcium,scandium,titanium,vanadium,chromium,manganese,iron
dw cobalt,nickel,copper,zinc,gallium,germanium,arsenic,selenium,bromine
dw krypton,rubidium,strontium,yttrium,zirconium,niobium,molybdenum
dw technetium,ruthenium,rhodium,palladium,silver,cadmium,indium,tin
dw antimony,tellurium,iodine,xenon,cesium,barium,lanthanum,cerium
dw praseodymium,neodymium,promethium,samarium,europium,gadolinium,terbium
dw dysprosium,holmium,erbium,thulium,ytterbium,lutetium,hafnium,tantalum
dw tungsten,rhenium,osmium,iridium,platinum,gold,mercury,thallium,lead
dw bismuth,polonium,astatine,radon,francium,radium,actinium,thorium
dw protactinium,uranium,neptunium,plutonium,americium,curium,berkelium
dw californium,einsteinium,fermium,mendelevium,nobelium,lawrencium
dw rutherfordium,dubnium,seaborgium,bohrium,hassium,meitnerium,darmstadtium
dw roentgenium,copernicium,nihonium,flerovium,moscovium,livermorium
dw tennessine,oganesson
Elements_End:
 
NullString
db 0
hydrogen:
db "hydrogen ",0
helium:
db "helium ",0
lithium:
db "lithium ",0
;etc.
 
RevisionDate:
db "2021-09-20",0
 
Finally:
db "elements, the last is ",0
ElementCount equ (Elements_End-Elements)/2 ;a constant value that cannot change at runtime.</lang>
 
The required output can be obtained like so:
<lang 6502asm>LDA #<RevisionDate ;get the low byte of the address
STA z_L ;store it in z_L, a zero page memory address
LDA #>RevisionDate ;get the high byte
STA z_H ;store it in z_H, the zero page memory address directly after z_L
JSR PrintString ;unimplemented printing routine
 
JSR NewLine
 
LDA ElementCount
JSR ConvertHex2BinDec ;converts a hexadecimal value to a trio of BCD digits
JSR PrintBCD ;unimplemented printing routine for numeric values
 
LDA #$20 ;ASCII for spacebar
JSR PrintChar
 
LDA #<Finally
STA z_L
LDA #>Finally
STA z_H
JSR PrintString
 
 
LDA ElementCount
ASL A ;multiply by 2, we are indexing into a table of words.
TAX ;use as an offset into the lookup table.
 
LDA Elements,x
STA z_L
LDA Elements+1,x
STA z_H
JSR PrintString</lang>
 
This routine can be expanded as new elements are discovered, but once the 128th element is discovered it will need to be reprogrammed since you can't offset more than x=255.
 
{{out}}
<pre>
2021-09-20
118 elements, the last is oganesson
</pre>
 
 
 
 
=={{header|Arturo}}==
1,489

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.