Page 1 of 1

macro constant in assembly

Posted: Sun Apr 14, 2013 4:21 pm
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.

Re: macro constant in assembly

Posted: Sun Apr 14, 2013 4:56 pm
by segher

Code: Select all

    .set name, expr
Have you seen the Assembly Programming Manual?

Re: macro constant in assembly

Posted: Sun Apr 14, 2013 9:04 pm
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

Re: macro constant in assembly

Posted: Sun Apr 14, 2013 9:47 pm
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.]

Re: macro constant in assembly

Posted: Sun Apr 14, 2013 10:08 pm
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.

Re: macro constant in assembly

Posted: Sun Apr 14, 2013 11:10 pm
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.