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

From: Marc 'BlackJack Rintsch <marc1rintsch.de>
Date: 2008-12-28 22:03:30
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
-- 
“Puritanism: the haunting fear that someone, somewhere, may be happy.”
                                                       -- H.L. Mencken

----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Sun Dec 28 22:03:43 2008

This archive was generated by hypermail 2.1.8 : 2008-12-28 22:03:45 CET