Re: [cc65] lines of code, memory, etc

From: Tachdaun <tachdaun1gmail.com>
Date: 2008-11-29 01:22:44
Oh that's a good one! maybe I mail you all my code so you optimize it for me :D
Thanks

PS: I'm aware of PEEK, in fact it inspired addr(x), but I use it in place of POKE, for example:
addr( 0x4016 ) = 1; instead of POKE( 0x4016, 1 );
It just looks better for me.

Marc 'BlackJack Rintsch wrote:
On Friday 28 November 2008, Tachdaun wrote:

  
A question of choice: what would be better, taking into account code
size, execution time and memoy consumption( declaring additional
variables ) ?

// addr( x )  means:    (*(unsigned char*) ( x ))      it's the value
of the given address
    

This is available as `PEEK()` macro in `peekpoke.h`.

  
code 1:
    joy_state |= ( addr(joypad) & 1 );            // Push A
    button state bit to joy_state's bit 0
    joy_state |= ( addr(joypad) & 1 ) << 1;    // Push B
button state bit to joy_state's bit 1
    joy_state |= ( addr(joypad) & 1 ) << 2;    // Push Select   
button state bit to joy_state's bit 2
    joy_state |= ( addr(joypad) & 1 ) << 3;    // Push Start
button state bit to joy_state's bit 3
    joy_state |= ( addr(joypad) & 1 ) << 4;    // Push Up
button state bit to joy_state's bit 4
    joy_state |= ( addr(joypad) & 1 ) << 5;    // Push Down
button state bit to joy_state's bit 5
    joy_state |= ( addr(joypad) & 1 ) << 6;    // Push Left
button state bit to joy_state's bit 6
    joy_state |= ( addr(joypad) & 1 ) << 7;    // Push Right
button state bit to joy_state's bit 7

or code 2:
    unsigned char iter;
    for( iter = 0; iter <= 7; ++iter )
        joy_state |= ( addr(joypad) & 1 ) << iter;

of course, code 2 declares one variable that code 1 doesn't, and it
has the overhead of checking the loop conditions.
But code 2 has fewer lines of code than code 1. I know that in asm
it's more than 2 lines, but I guess it's fewer than code 1
Which would you choose?
    

Any chance you can reverse the meaning of the bits in `joy_state`?  Then 
you can use::

    joy_state = (joy_state << 1) | (PEEK(joypad) & 1);
    joy_state = (joy_state << 1) | (PEEK(joypad) & 1);
    joy_state = (joy_state << 1) | (PEEK(joypad) & 1);
    joy_state = (joy_state << 1) | (PEEK(joypad) & 1);
    joy_state = (joy_state << 1) | (PEEK(joypad) & 1);
    joy_state = (joy_state << 1) | (PEEK(joypad) & 1);
    joy_state = (joy_state << 1) | (PEEK(joypad) & 1);
    joy_state = (joy_state << 1) | (PEEK(joypad) & 1);

or::

    for (i = 0; i < 8; ++i) {
        joy_state = (joy_state << 1) | (PEEK(joypad) & 1);
    }

To compare the generated assembler code you can tell the compiler to 
write it into a file.

Ciao,
	Marc 'BlackJack' Rintsch
  
---------------------------------------------------------------------- To unsubscribe from the list send mail to majordomo@musoftware.de with the string "unsubscribe cc65" in the body(!) of the mail. Received on Mon Dec 29 00:26:21 2008

This archive was generated by hypermail 2.1.8 : 2008-12-29 00:26:23 CET