Using register in C

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

Hello All,

I'm usung GCC and writing in C. I am trying to dedicate some of the registers as specific variables to save time swapping in and out of RAM. Instead of:

volatile unsigned int  test = 0x0000;

(Which works) I'm using

register int  test asm("r26");

The code compiles without any errors or warnings but when I try "temp = 0x1234;" then the register's values don't seem to change. What am I doing wrong ?

Regards

Dren

<º))))><

I am only one lab accident away from becoming a super villain.

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

Try "nerd = 0x5678" and see whether that makes any difference to "test".

David.

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

Thanks for the fast response.

Sorry that was a typo on the forum, it should read 'test=0x1234;'

<º))))><

I am only one lab accident away from becoming a super villain.

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

For me:

#include  

register unsigned int test asm("r26");

__attribute__ ((OS_main))
int main(void) {
	test = 0x1234;
}

generates:

int main(void) {
  92:	cd b7       	in	r28, 0x3d	; 61
  94:	de b7       	in	r29, 0x3e	; 62
	test = 0x1234;
  96:	a4 e3       	ldi	r26, 0x34	; 52
  98:	b2 e1       	ldi	r27, 0x12	; 18
  9a:	80 e0       	ldi	r24, 0x00	; 0
  9c:	90 e0       	ldi	r25, 0x00	; 0
}
  9e:	08 95       	ret

In which I most definitely see R27:R26 being loaded with 0x1234. Note however the build warning:

../test.c:8: warning: call-clobbered register used for global register variable

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

I don't know why defining global variables as unused registers doesn't seem to work i.e.

register int  test asm("r6"); or volatile register int  test asm("r6"); 

Whereas

volatile unsigned int test = 0x0000;

Does but it's only a few of extra ASM instructions to transfer between RAM locations and the registers, so I'm doing it that way and it's all working perfectly. I sort of suspect that the compiler is optimising out some of the code when not using RAM ? Maybe at the end of the project I'll spend some time on to try to use the registers directly, but as they say... if it ain't broken - dont fix it :) Thanks for your help.

<º))))><

I am only one lab accident away from becoming a super villain.

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

The optimiser will always try to optimise into register only usage when it can unless it is over-ridden by the 'volatile' modifier.