Search code examples
assembly68000

I can use the value of a variable as a variable name?


I was thinking (within the limits of my knowledge), that to load values with different lengths (but always ending with 0), it would be convenient if I could use the value of a variable as a variable name. For example, in:

lea sc1, a2

if I could use the suffix sc combined with the value of d0 instead of sc1, I could easily scroll through the data section. Is there a way to do this?

example:

     move.w #4,d0    ; loop 5 times

loop
     lea    sc1,a2
     ; .... some code
     dbf    d0,loop

     clr    -(a7)        ; end
     trap   #1

   section data

sc1  dc.b 15,15,"Space",0
sc2  dc.b 10,4,"F1",0
sc3  dc.b 10,9,"F2",0
sc4  dc.b 10,44,"F9",0
sc5  dc.b 10,49,"F10",0

Solution

  • Unfortunately, there's no way to actually do that. Those "sc1","sc2", etc. are just a convenience for the programmer when writing the code. The assembler converts them to memory addresses once the code is assembled. To use the 68000's vector table as an example:

    .org 0
    initial_stack_pointer:
    dc.l $FFFFFFFE
    reset:
    dc.l $00000400   ;your code begins at this address
    

    Once the program is assembled, the labels are destroyed, and any reference to reset in your code becomes the constant number 4 (since we started at 0 and each dc.l takes up four bytes of space.)

    However, all is not lost. There is a little trick you can use to make this happen. If you make a separate table of pointers to each of those strings, you can iterate through them more easily without having to deal with a bunch of padding.

         move.w #4,d0    ; loop 5 times
         lea    sc_table,a2
    loop
         move.l (a2),a1      ;get the address of the string into a1
         move.b (a1)+,D2     ;load the first 15 in 15,15,"Space",0 into D2
         ;then you'd probably want to have an inner loop that repeats this 
         ;until the terminator is reached.
         
         ; .... some code
         adda.l #4,a2        ;next sc
         dbf    d0,loop
    
         clr    -(a7)        ; end
         trap   #1
    
       section data
    
    sc_table:
    dc.l sc1
    dc.l sc2
    dc.l sc3
    dc.l sc4
    dc.l sc5
    
    sc1  dc.b 15,15,"Space",0
    sc2  dc.b 10,4,"F1",0
    sc3  dc.b 10,9,"F2",0
    sc4  dc.b 10,44,"F9",0
    sc5  dc.b 10,49,"F10",0
    .align 2
    

    This technique can be a bit confusing because you have to dereference twice, but once you get the hang of it, string lists will be much easier to manage. It's very common to use a similar technique in video games for loading animations. You'd have a table of pointers to each frame of animation, and use a timer variable (which is incremented each frame) as an index into that table.