Thursday, August 20, 2009

Using Bits in 8051 assembly

warning: this is a placeholder written from memory. I need to update it



Useful commands: The JB (Jump if Bit set) will jump if a bit is set. The encoding of the field is the bit offset from data memory address 0x20. If you forget this, and try encoding the bit directly, then you will actually get bit 0x20.0 + 0x20 = 0x24.0 (ie 32 bits down the line).

To declare the bit in ASM:


BSEG 0 ; offset 0 in the 0x20.0 to 2F.7 range.
BIT mybit ; declare mybit at 0x20.0
...

; useful 8-byte entry in the vector table (ie at offset 0x0003 in code space for an ISR)

CSEG AT 0x0003

JB mybit, passhop ; 3 bytes encoding
AJMP fail ; 2 bytes encoding
passhop: LJMP pass ; 3 bytes encoding = 8 bytes total



In C, you would declare the bit as:

extern bit myBit at 0x20;

No comments: