4
votes

I've found asm inline assembly that is working and that i can't understand (link):

// busy wait
__asm__ __volatile__ (
    "1: sbiw %0,1" "\n\t" // 2 cycles
    "brne 1b" : "=w" (us) : "0" (us) // 2 cycles
);

I've found some gas tutorials like this but i was unable to find explanation. Anyway the file can be compiled by avr-gcc:

/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/bin/avr-gcc -c -g -Os -Wall -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=105 -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/variants/standard /Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/wiring.c -o /var/folders/64/fwfkm1k51zbd4_c5lwpsbljh0000gn/T/build5450310618632673119.tmp/wiring.c.o

So what does “=w” in GCC Inline Assembly mean?

2

2 Answers

7
votes

It is output constraint. Equal sign means "value will be written to". Letter w means for AVR "registers from r24 to r31". Look here to find machine-specific constraint description.

Constraints itself are used by compiler on register allocation pass, in order to allocate correct register (in this case -- exactly such, that can participate in sbiw operation). If you will specify wrong constraint, gcc may allocate wrong register for sbiw, that will yield wrong output assembly, etc.

6
votes

The "=" means its an output. "w" is a constraint. The answer is partially found in the GCC manual, and partially found in a 2012 GCC check-in.

In 6.43.3.1 Simple Constraints of the manual:

Other letters can be defined in machine-dependent fashion to stand for particular classes of registers or other arbitrary operand types. ‘d’, ‘a’ and ‘f’ are defined on the 68000/68020 to stand for data, address and floating point registers.

Here's the check-in that added the constraint: [Patch,AVR]: Add "w" constraint alternative to addhi3. Here was the comment for the check-in:

This patch adds a "w" alternative to *addhi3 and addhi3_clobber in order to vote for class "w". This is similar to the orgiginal addhi3 insn up to version 4.6. And there is no more explicit vote for "l" in addhi3_clobber, it's just "r".
Intention is to get a better usage of ADIW and SBIW instructions.

So "w" is an AVR related constraint.