Using assembler to access a union

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How can I access individual bytes of the union below in assembler?

volatile union {
 uint32_t Counts;
 uint16_t ints[2];
 uint8_t  bytes[4];
} foo

Can I just use lo8(foo), hi8(foo), hlo8(foo) and hhi8(hlo8)?

Sorry if I've put this topic in the wrong forum, but I've figured that as GAS is a part of GCC, and it involves mixing assembler and C, it should belong here.

Felipe Maimon

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Looks like i've got it, but when I try to compile the code below, the "teste" function is not linked (GCC gives a warning about an implicit declaration of 'teste').
Using AVR-Studio with WinAVR 20081205

the "get_key_short" function is from Danni's debounce code.

here is the main function

int main (void)
{
	char Str_out[20];
	DDRA = 0xFF;
	PORTA = 0xF0;

  	OCR0 = F_CPU / 1024 * 10e-3;
  	// TMR0 CTC mode and divide 1024
  	TCCR0 = (1<<CS02)|(1<<CS00)|(1<<WGM01);			// divide by 1024
  	TIMSK = 1<<OCIE0;				// enable timer interrupt

	LcdInitialize();

	sei();

	for(;;)	{

    	if( get_key_short( 1<<KEY1 )) {
			LcdClear();
			teste();
			ltoa(bar.Counts, Str_out, 10);
			LcdPrintString(Str_out);
		}

		if( get_key_short( 1<<KEY2 )){
			foo.Counts += 10;
			foo.Counts *= -5;
			LcdGotoXY(0,1);
			ltoa(foo.Counts, Str_out, 10);
			LcdPrintString(Str_out);
		}

		_delay_ms(250);		
	}
}

and here is the assembler function. It just copies foo to bar (both are defined as an union that is in the original post)

#include 

.extern foo
.extern bar

	.global teste
teste:
	lds r18, foo
	lds r19, foo+1
	lds r20, foo+2
	lds r21, foo+3
	sts bar, r18
	sts bar+1, r19
	sts bar+2, r20
	sts bar+3, r21
	ret

Whats wrong with it?

Thank you,

Felipe Maimon

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think you need to tell the main program about teste. Try extern in front of the function definition of teste.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I didn't do a foward declaration of teste in the first case. So I've tried with it now, and it's working!

Thank you rstahlhu.

Felipe Maimon