3

Let's say I need to test a port pin like this:

sbic  PIND,2

This should work, but in C I would definitely made a #define INPUT ((PORTD&(1<<2))!=0). Is something similar possible in assembler? Something like

.def  INPUT = PIND,2   ; this is not work!

Sorry for the stupid question. Just started to assembler after years of C-coding...

Roman Matveev
  • 2,942
  • 7
  • 32
  • 75
  • [Macros.](https://sourceware.org/binutils/docs/as/Macro.html) – Ignacio Vazquez-Abrams Feb 26 '16 at 19:10
  • Can you not just use a [```#define```](http://www.atmel.com/webdoc/avrassembler/avrassembler.wb_preprocessor.define.html)? e.g. ```#define INPUT PIND,2```, then ```sbic INPUT```. – Tom Carpenter Feb 26 '16 at 19:11
  • Or possibly [```.equ```](http://www.atmel.com/webdoc/avrassembler/avrassembler.wb_directives.html#avrassembler.wb_directives.EQU)? – Tom Carpenter Feb 26 '16 at 19:14
  • @TomCarpenter I assumed that this is C syntax. However this worked! At least the compilation was OK. Do you know why `#define` does not highlighted by the Atmel Studio as it is for `.equ`, `.def` etc.? `.equ INPUT=PORTD,2` did not work :( – Roman Matveev Feb 26 '16 at 19:18
  • @RomanMatveev because it isn't really valid assembly. Atmel Studio passes the code through the AVR ASM preprocessor in the same way it does with the C preprocessor. – Tom Carpenter Feb 26 '16 at 19:20
  • @TomCarpenter so do you think that `#define` is a good approach? Maybe `.macros` (as @Ignacio suggested) is also worth considering for this purpose? How do YOU define a port pins? – Roman Matveev Feb 26 '16 at 19:22
  • 1
    Most people work in C, dropping to inline assembly when required. It's not all that common to work in pure assembly with modern microcontrollers. – Ignacio Vazquez-Abrams Feb 26 '16 at 19:25
  • I tend to use C and inline assembly. What you could do is define just the bit, e.g. ```.def PINFUNCTION_PORTD = 2```, and then do ```sbic PIND, PINFUNCTION_PORTD```. Granted you still have to write the correct register, but if you encode it into the name as in that example, it is quick to verify that you have the correct port. – Tom Carpenter Feb 26 '16 at 19:25
  • @TomCarpenter I know... But I faced with a task strongly needed a lot of assembler. Inline Asm in C is also a headache for me :( – Roman Matveev Feb 26 '16 at 19:26
  • 1
    Given that assembler isn't going to be portable anyway, I see no reason not to use ```#defines```. – Tom Carpenter Feb 26 '16 at 19:28
  • @TomCarpenter could you please post your thoughts as an answer to let me accept this? – Roman Matveev Feb 26 '16 at 19:29

1 Answers1

1

You have three options.

1. Using #defines

AVR Studio passes the code through the AVR ASM Preprocessor. This does similar things to the C/C++ Preprocessor, but works on assembly. Given you are writing in pure assembly, the code is not going to be portable, so you might as well leverage the capabilities of AVR Studio (and in fact anything using AVR libc). You can simply use a define like in C:

#define SOMEPIN PINC,2

sbic SOMEPIN;

You could even go for something more advanced to deal with being able to use input, output, and direction registers. Something along the lines of:

#define CONCAT(a,b,c) a##b##,##c ; Helper for concatenation
#define SOMEPIN(p) CONCAT(p,C,2) ; Define as xC,2

sbic SOMEPIN(PIN)  ; Expands to 'sbic PINC,2', and then to 'sbic 0x##,2'
sbi  SOMEPIN(DDR)  ; Expands to 'sbi  DDRC,2'

Some may say that is abusing the pre-processor, but if it helps you understand the code better, so be it.

2. Just the Bit

This one is possibly easier in that it doesn't require the pre-processor. Rather than defining the entire command, just define the bit you want, but also include what port you are interested in the name. For example:

.equ SOMEPIN_D = 2;

sbic PIND, SOMEPIN_D;

That way if you know the name of the pin (SOMEPIN_D), you can easily remember which port/pin/ddr register it belongs to as it is part of the name.

3. Macros

@Ignacio mentioned this in the comments. You may be able to use macros to do what you want, though it has been several years since I wrote anything in pure assembler, so I can't immediately say how.

Tom Carpenter
  • 63,168
  • 3
  • 139
  • 196