Next Previous Contents

11. Use constants to access absolute memory locations

The compiler produces optimized code, if the value of a pointer is a constant. So, to access direct memory locations, use

        #define VDC_DATA   0xD601
        *(char*)VDC_STATUS = 0x01;

That will be translated to

        lda     #$01
        sta     $D600

The constant value detection works also for struct pointers and arrays, if the subscript is a constant. So

        #define VDC     ((unsigned char*)0xD600)
        #define STATUS  0x01
        VDC [STATUS] = 0x01;

will also work.

If you first load the constant into a variable and use that variable to access an absolute memory location, the generated code will be much slower, since the compiler does not know anything about the contents of the variable.


Next Previous Contents