macro constant in assembly

Technical questions regarding the XTC tools and programming with XMOS.
Post Reply
diltsman
Member++
Posts: 21
Joined: Sun Mar 03, 2013 11:26 pm

macro constant in assembly

Post by diltsman »

I have a set of constants that I would like to name. I know that I can put them in the constants section and load and use them. However, the constants are sufficiently small that it would be more efficient to use them as immediate arguments.

Is there a way to do the equivalent of a #define that inlines the value? I know that I could change my file extension from .s to .S and use the C preprocessor, but I would rather avoid the preprocessor if possible.


User avatar
segher
XCore Expert
Posts: 844
Joined: Sun Jul 11, 2010 1:31 am
Contact:

Post by segher »

Code: Select all

    .set name, expr
Have you seen the Assembly Programming Manual?
diltsman
Member++
Posts: 21
Joined: Sun Mar 03, 2013 11:26 pm

Post by diltsman »

I read much of the Assembly Programming Manual. I appear to have been confused as to the function of the .set directive.

Code: Select all

  .section .cp.const4, "cM", @progbits, 4
  .set XS1_PORT_4F, 0x40500
I had thought that his code would allocate 4 bytes in the .cp.const4 section and then set them to 0x40500. Does it allocate the bytes or just generate a constant?

I guess, looking at it again, that this would allocate the bytes and label them. Thanks for helping me to straighten this out in my mind.

Code: Select all

  .align 4
XS1_PORT_4F:
  .word 0x40500
User avatar
segher
XCore Expert
Posts: 844
Joined: Sun Jul 11, 2010 1:31 am
Contact:

Post by segher »

.set name,expr creates a symbol "name" (in the current section)
with the value "expr".

Your second example should do what you want, yes. [You might want
to choose another name; XS1_PORT_4F is usually used as a preprocessor
macro, evaluating to the constant 0x040500, not the address of a memory
cell containing that value.]
diltsman
Member++
Posts: 21
Joined: Sun Mar 03, 2013 11:26 pm

Post by diltsman »

Code: Select all

.set CTRL_INUSE_ON, 0x0008
  .section .cp.const4, "caM", @progbits, 4
PORTS:
XS1_PORT_4F: .word 0x40500
  .text
  .align 2
  .globl _start
_start:
  ldap r11, _cp
  set cp,r11
  ldw r10, cp[XS1_PORT_4F]
  setc res[r10], CTRL_INUSE_ON
  bu _start
This gives me the error "Error: A00050 Instruction 'setc' does not allow a label as an immediate" referencing the setc. How would I change this so that it would work? Again, I would like to avoid using the C preprocessor.
User avatar
segher
XCore Expert
Posts: 844
Joined: Sun Jul 11, 2010 1:31 am
Contact:

Post by segher »

In GAS (the GNU assembler), from which most of the syntax
is borrowed, you would use .macro/.endm. Alas, xas has no
support for those (or the super-useful .org and .rept/.irp/.endr).
Let me know if you find something else that works.

I just hardcode the 1 and 3 (for channels); I don't write I/O setup
in assembler generally. I'm sure you can manage a literal 8 for
on, but it quickly becomes more confusing. Maybe the best you
can do is just write the number and add a comment.
Post Reply