Click here to Skip to main content
15,898,992 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Practice safe programming ! Pin
Yusuf13-Aug-08 15:24
Yusuf13-Aug-08 15:24 
GeneralRe: Practice safe programming ! Pin
Jörgen Sigvardsson22-Aug-08 10:23
Jörgen Sigvardsson22-Aug-08 10:23 
GeneralRe: Practice safe programming ! Pin
geoffs14-Aug-08 9:23
geoffs14-Aug-08 9:23 
GeneralRe: Practice safe programming ! Pin
Johann Gerell21-Aug-08 1:11
Johann Gerell21-Aug-08 1:11 
GeneralRe: Practice safe programming ! Pin
Paul Conrad13-Aug-08 9:27
professionalPaul Conrad13-Aug-08 9:27 
GeneralRe: Practice safe programming ! Pin
KarstenK14-Aug-08 3:48
mveKarstenK14-Aug-08 3:48 
GeneralRe: Practice safe programming ! Pin
Paul Conrad14-Aug-08 6:21
professionalPaul Conrad14-Aug-08 6:21 
GeneralCalling a subroutine Pin
Timothy Baldwin12-Aug-08 11:34
Timothy Baldwin12-Aug-08 11:34 
This horror using GCC for AVR.

static void apicall( void ) __attribute__ ((noinline));
static void apicall( void )
{
  asm volatile("ldi r22, %0" :: "M" ((FLASHEND>>1)&0xFF));	// lo
  asm volatile("push r22");
  asm volatile("ldi r22, %0" :: "M" ((FLASHEND>>9)&0xFF));	// hi
  asm volatile("push r22");
#if( FLASHEND > 0x1FFFF )
  asm volatile("ldi r22, %0" :: "M" ((FLASHEND>>17)&0xFF));	// xhi
  asm volatile("push r22");
#endif
  asm volatile("ldi r22, %0" :: "M" (API_PROG_PAGE));		// function
  return;							// jump to API
}


/**************	copy one page from SRAM to Flash ************************/

unsigned char copy_flash( void *src, void *dst, unsigned char dst_hi )
{
  unsigned char i;

  if( (unsigned int)dst & (SPM_PAGESIZE-1))
    return API_ERR_PAGE;			// not on page limit

  asm volatile("movw r26, %0" :: "r" (src));
  asm volatile("movw r30, %0" :: "r" (dst));
  asm volatile("mov r21, %0" :: "r" (dst_hi));
  apicall();
  asm volatile("clr r1");			// clear zero reg
  asm volatile("mov %0, r22" : "=r" (i));
  return i;					// success
}

Why push a address on the stack and using the compiler emitted ret instruction instead of using a call instruction?

Assuming CPU registers remain unchanged between asm statements: bad
Changing CPU registers in assembler without informing compiler: worse

Why not write the subroutine to use the C calling convention?
GeneralRe: Calling a subroutine Pin
Chris Maunder12-Aug-08 16:42
cofounderChris Maunder12-Aug-08 16:42 
GeneralRe: Calling a subroutine Pin
Mike Dimmick17-Aug-08 10:39
Mike Dimmick17-Aug-08 10:39 
GeneralRe: Calling a subroutine Pin
Timothy Baldwin21-Aug-08 5:14
Timothy Baldwin21-Aug-08 5:14 
GeneralRe: Calling a subroutine Pin
supercat917-Aug-08 11:59
supercat917-Aug-08 11:59 
GeneralCode which caused my PC hang [modified] PinPopular
Anand Desai6-Aug-08 22:41
Anand Desai6-Aug-08 22:41 
GeneralRe: Code which caused my PC hang PinPopular
leppie7-Aug-08 1:35
leppie7-Aug-08 1:35 
GeneralRe: Code which caused my PC hang Pin
Anand Desai7-Aug-08 2:14
Anand Desai7-Aug-08 2:14 
GeneralRe: Code which caused my PC hang Pin
leppie7-Aug-08 2:19
leppie7-Aug-08 2:19 
GeneralRe: Code which caused my PC hang Pin
Anand Desai7-Aug-08 2:21
Anand Desai7-Aug-08 2:21 
GeneralRe: Code which caused my PC hang Pin
Dan Neely7-Aug-08 2:16
Dan Neely7-Aug-08 2:16 
GeneralRe: Code which caused my PC hang Pin
Yusuf8-Aug-08 11:04
Yusuf8-Aug-08 11:04 
GeneralRe: Code which caused my PC hang Pin
AeonBlue7-Aug-08 4:42
AeonBlue7-Aug-08 4:42 
GeneralRe: Code which caused my PC hang Pin
Paul Conrad7-Aug-08 10:50
professionalPaul Conrad7-Aug-08 10:50 
JokeRe: Code which caused my PC hang Pin
Yusuf8-Aug-08 11:06
Yusuf8-Aug-08 11:06 
GeneralAttack of the notepads Pin
Llasus8-Aug-08 22:08
Llasus8-Aug-08 22:08 
GeneralRe: Attack of the notepads Pin
Jonathan C Dickinson9-Aug-08 2:41
Jonathan C Dickinson9-Aug-08 2:41 
GeneralRe: Code which caused my PC hang Pin
Jamie Nordmeyer11-Aug-08 11:03
Jamie Nordmeyer11-Aug-08 11:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.