Long literals, with continuations: Difference between revisions

m (clarification)
Line 88:
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>ElementsElementNull:
dw nullString
 
Elements:
; this is typed in a compact manner to save on typing, however putting each on its own line with a
; "dw" directive in front will produce the same result. A comment with the element number on each line will aid in
; adding new elements to the list.
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
Line 159 ⟶ 162:
TAX ;use as an offset into the lookup table.
 
LDA ElementNull,x ;ElementCount doesn't account for zero-indexing so we'll need to load one word behind.
LDA Elements,x
STA z_L
LDA ElementsElementNull+1,x
STA z_H
JSR PrintString</lang>
Line 167 ⟶ 170:
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|68000 Assembly}}==
{{trans|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 68000devpac>HelloString:
DC.B "Hello World" ;no null terminator
GoodbyeString:
DC.B "Goodbye World!",0
EVEN
 
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>
 
In addition, it makes no difference to the assembler whether each string is on its own line, or each is one after the other on the same line. It's easier to put each on its own line since you can't have a label mid-line.
 
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 68000devpac>
NullElement:
DC.L nullString
Elements:
; this is typed in a compact manner to save on typing, however putting each on its own line with a
; "DC.L" directive in front will produce the same result. A comment with the element number on each line will aid in
; adding new elements to the list.
DC.L hydrogen,helium,lithium,beryllium,boron,carbon,nitrogen,oxygen,fluorine
DC.L neon,sodium,magnesium,aluminum,silicon,phosphorous,sulfur,chlorine,argon
DC.L potassium,calcium,scandium,titanium,vanadium,chromium,manganese,iron
DC.L cobalt,nickel,copper,zinc,gallium,germanium,arsenic,selenium,bromine
DC.L krypton,rubidium,strontium,yttrium,zirconium,niobium,molybdenum
DC.L technetium,ruthenium,rhodium,palladium,silver,cadmium,indium,tin
DC.L antimony,tellurium,iodine,xenon,cesium,barium,lanthanum,cerium
DC.L praseodymium,neodymium,promethium,samarium,europium,gadolinium,terbium
DC.L dysprosium,holmium,erbium,thulium,ytterbium,lutetium,hafnium,tantalum
DC.L tungsten,rhenium,osmium,iridium,platinum,gold,mercury,thallium,lead
DC.L bismuth,polonium,astatine,radon,francium,radium,actinium,thorium
DC.L protactinium,uranium,neptunium,plutonium,americium,curium,berkelium
DC.L californium,einsteinium,fermium,mendelevium,nobelium,lawrencium
DC.L rutherfordium,dubnium,seaborgium,bohrium,hassium,meitnerium,darmstadtium
DC.L roentgenium,copernicium,nihonium,flerovium,moscovium,livermorium
DC.L tennessine,oganesson
Elements_End:
 
nullString:
DC.B 0
EVEN
hydrogen:
DC.B "hydrogen",0
EVEN
helium:
DC.B "helium",0
EVEN
lithium:
DC.B "lithium",0
EVEN
;etc.
 
RevisionDate:
DC.B "2021-09-20",0
EVEN
 
Finally:
DC.B "elements, the last is",0
EVEN
ElementCount equ (Elements_End-Elements)/4)
; a constant value that cannot change at runtime.
; This counts the number of bytes between the two labels, and automatically adjusts when the size of the list changes.
; The division by 4 gets the actual element count since each address is 4 bytes long.</lang>
 
The required output can be obtained like so:
<lang 68000devpac>LEA RevisionDate,A3 ; the printing routine uses A3 as input
JSR PrintString ; unimplemented printing routine
JSR NewLine ; unimplemented new line routine
 
MOVE.B ElementCount,D0
JSR ConvertHex2BinDec ; converts a hexadecimal value to a trio of BCD digits
JSR PrintBCD ; unimplemented printing routine for numeric values
 
MOVE.B #' ',D0 ; ASCII for spacebar
JSR PrintChar
 
LEA Finally,A3
JSR PrintString
 
MOVE.B #' ',D0 ; ASCII for spacebar
JSR PrintChar
 
MOVE.W ElementCount,D1
LSL.W #2,D1 ; multiply by 4, we are indexing into a table of longs
 
LEA NullElement,A2
; load the base of the lookup table into A2. This is the table's "true base"
; since the "ElementCount" constant doesn't account for zero-indexing
 
MOVEA.L (A2,D1),A3
;dereference the pointer, offsetting by D1. This retrieves the address of the desired element in the list.
JSR PrintString</lang>
 
This routine can easily be expanded for more elements. With a maximum offset of 16,383, it's unlikely that the table will get too large to properly index anytime soon.
{{out}}
<pre>
1,489

edits