Menu

#3838 z80 bitfield struct corruption

closed-invalid
nobody
other
5
2025-02-21
2025-02-20
No

Sample code and listing files are attached.

The build command "zcc +z80 --list -vn -SO3 -compiler=sdcc -startup=1 test.c -create-app"

And upgraded to latest "brew install sdcc" which got me 4.5.0

mcs51/z80/z180/r2k/r2ka/r3ka/sm83/tlcs90/ez80_z80/z80n/r800/ds390/pic16/pic14/TININative/ds400/hc08/s08/stm8/pdk13/pdk14/pdk15/mos6502/mos65c02/f8 TD- 4.5.0 #15242 (Mac OS X x86_64)

The bit-field is corrupted. Specifically in the bit-field struct when setting group field it clobbers the adjacent bits of the byte.

  symbol_t *sym = (symbol_t*)&symbols[ i ];
  sym->group = 0x3F;

typedef struct {
uint8_t last : 1;
uint8_t group : 6;
uint8_t visible : 1;
uint16_t x;
uint16_t y;
uint16_t addr;
uint16_t rotation;
uint8_t scale;
} symbol_t;

results in the output:

395 00000f 7e ld a,(hl) ; Load first byte of symbol_t into A
396 000010 f600 or a,0x7e ; OR with 0x7E (0111 1110)
397 000012 77 ld (hl),a ; Store back in memory

The OR operation (or a, 0x7E) sets bit 7 (visible),

instead it should produce something like:

and a, 0x81 ; Preserve last (bit 0) and visible (bit 7)
or a, 0x3E ; Set group (bits 1-6) to 0x3F (0011 1111)

2 Attachments

Discussion

  • Jeff Mathews

    Jeff Mathews - 2025-02-20

    never mind.
    please close.

     
    • Janko Stamenović

      Yes, if the middle 6 bits in a byte are to be updated, then 0x7e are exactly these 6 bits set, so

      or a,0x7e

      is correct and the bits 0 and 7 remain unchanged there.

      It can be closed.

       
  • Benedikt Freisen

    • status: open --> closed-invalid
     
  • Benedikt Freisen

    Closed as requested.

     

Log in to post a comment.

MongoDB Logo MongoDB