This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCSv6.1 linked list macro issue



Hi team,

I would like to write a simple macro which do the following (pseudo-code)

define        link   0

setlink       MACRO

$1             dw      link            ; write the link value at the current compile time address, initial value is 0

define       link      $1             ; set link value to the current compile time address

ENDMACRO

Every time you call setlink (during compile time) the link value (an address) is write and link is set to current address.

this is the common way to build a linked list.

Well, using CCSV6.1 and asm430 (the inside assembler) I cannot do that, whatever I try ... Basically

                .asg      0,link

setlink      .macro

$1:           .word    link

                .asg      $1,link           ; or  .asg    :$1:,link, which would be conceptually better

                .endm

don't work, because you can't assign a label to a symbol. an other attempt, for example

setlink      .macro

                .word    link

link           .set       $-2               ; or  link      .set    $1       if  previous statement was labeled withe $1

                .endm


don't work because you cannot redefine link several time (each tiem you call the setlink macro) ...

I'm on the way to forgive CCS, unless someone have the right solution but I'm pessimistic ...

BR

Claude

    

  • I'm accustomed to link lists built out of dynamic memory (i.e. calls to malloc).  I've never seen a linked list like this one.  So, I need some more context to understand what you are doing.  

    Supposing the macro did work, how do you invoke it in your main code?  Please describe the result of those macro invocations.  

    Thanks and regards,

    -George

  • Hi Georges,


    in fact, as is, this piece of code is not very useful  ;o)

    I reduced it to the minimum needed to understand the subject.

    well, suppose you want to create a linked list of items numbered 1, 2, 3, and so on. Each of them keep a specific value.

    At run time you walk through this list to retreive those specific values doing whatever you want and this list itself can grow (by adding items).

    Suppose now you already knows the hundred first items, you would like to build the linked list at compile time to avoid lot of initialization code and lost time at run time.

    It's normally very simple to acheive this goal using a macro with some parameters, like this one (pseudo code):

    define          link                  0                    ; link = 0 mean you reach the end of linked list

    SetItem        MACRO         num, value

                        dw                  num              ; set num in memory

                        dw                  value             ; set value in memory

    $1:              dw                  link                 ; set actual value of link in memory

    define        link                  $1                   ; set new value of link which pointed to this item so the following will be correctly linked

                     ENDMACRO

    So, the last item is pointed to by the last (and actual) value of link

    At compile time, for example, you can write something like:

                    SetItem          1, 0x1546

                    SetItem           2, 0x5486

    ....

                    SetItem         100, 0x1234

    At run time you know that item 100 is pointed to by link, the following piece of code walk through the linked list:

                    mov.w           &link,R5           ; load R5 with link

                    cmp.w          -2(R5), 0x5486 ; this item contain the value I'm searching ?

                    je                  end_of_search  ; and R5 contain the good link @

                   mov.w            @R5, R5         ; not the good item, so R5 is now the @ of following item, and so on

    As you can see, it's very easy to manipulate such data structure, search item, read contents, and add item. Delete item

    it's a bit more complicated.

    Threaded virtual machine make an extensive use of this linked list.

    Voilà !  ;o)

    Well to return to my original question, how can I write the SetItem macro with CCSv6.1 ???!!!!

    Regards,

    Claude

  • claude caselles said:
    how can I write the SetItem macro with CCSv6.1 ?

    Please give this a try ...

    ;---------------------------------------------------------------------
    ; Init substitution symbols used in SetItem macro
    ;---------------------------------------------------------------------
    		.asg	0, link0
    		.asg	0, link_count
    		.asg	0, label_count
    
    ;---------------------------------------------------------------------
    ; Define SetItem macro
    ;---------------------------------------------------------------------
    SetItem		.macro	num, value
    		.word	num
    		.word	value
    label:label_count:	.word	link:link_count:
    
    		.eval	link_count+1,link_count
    		.asg	label:label_count:, link:link_count:
    		.eval	label_count+1, label_count
    
    		.endm	
    
    ;---------------------------------------------------------------------
    ; Example usage of SetItem macro
    ;---------------------------------------------------------------------
    		.sect	"linked_list_section"
    		SetItem	1, 0x1546
    		SetItem	2, 0x5486
    		SetItem	3, 0x1234
    
    ;---------------------------------------------------------------------
    ; Code to start the traversal of the linked list
    ;---------------------------------------------------------------------
    		.text
    		.eval	label_count-1,label_count
    		mov.w	&label:label_count:,R5

    I can only hand verify this code, so I may have missed something.  But I'm confident it is close enough to get you started.

    In particular, I am concerned that .word is not the best directive to use for reserving memory locations.  A better directive may be among those described in the section titled Directives that Initialize Values in the MSP430 assembly tools manual.  

    A description of the directives in the macro is in the chapter titled Macro Language Description in that same manual.

    Thanks and regards,

    -George

  • Hi George,

    first, thank you to devote time to help me.

    Well, your piece of code seems to work fine ;o)  (without .sect "linked_list_section" directive)

    it's still a bit complicated but it works ;o)))

    Btw, the .sect "linked_list_section" directive lead the assembler to catch this error:

    Values at address 0x0000000000000020 do not match Please verify target memory and memory map.

    Anyway, I will understand this later, now I can continue my work.

    Best regards, George

    Claude