• ch00f@lemmy.world
    link
    fedilink
    arrow-up
    6
    arrow-down
    1
    ·
    17 hours ago

    I’ve been working on disassembling some 8-bit code from the 90s. Fuckers returned bits from functions using the overflow bit. Nuts.

    • CarrotsHaveEars@lemmy.ml
      link
      fedilink
      arrow-up
      1
      ·
      9 hours ago

      What era was that device? Some old games on NES had to use all kinds of quirks like this to overcome hardware limitation.

    • jsomae@lemmy.ml
      link
      fedilink
      arrow-up
      1
      ·
      12 hours ago

      Where do you go to talk about such things? Could be fun to have a retro reversing community.

  • jsomae@lemmy.ml
    link
    fedilink
    arrow-up
    4
    ·
    edit-2
    12 hours ago

    Use bit-fields:

    struct {
      bool a : 1;
      bool b : 1;
      bool c : 1;
      //...
    };
    

    Edit: careful not to use a 1-bit signed int, since the only values are 0 and -1, not 0 and 1. This tripped me up once.

    • Strawberry@lemmy.blahaj.zone
      link
      fedilink
      arrow-up
      0
      ·
      11 hours ago

      In a world where a bigger memory chip is more expensive by only a few cents where this would be most useful, is this feature still relevant?

      • kora@sh.itjust.works
        link
        fedilink
        arrow-up
        3
        ·
        edit-2
        11 hours ago

        Yes, firmware running on bare metal requires good resource management. My current development board processor contains 512KB SRAM. That’s equivalent to half of the size of an average PDF.

      • jsomae@lemmy.ml
        link
        fedilink
        arrow-up
        1
        ·
        edit-2
        11 hours ago

        Yes, because cache optimization is still important. Also useful to keep the size of packets down, to reduce the size of file formats, and anywhere that you use hundreds of thousands of instances of the struct.

  • Tolookah@discuss.tchncs.de
    link
    fedilink
    arrow-up
    3
    ·
    18 hours ago

    I use bit masks, suck it! (Really though, programming on an embedded CPU might be reasonable to do this, depending on the situation, but on a PC, trying to not waste bits wastes time)

    • jsomae@lemmy.ml
      link
      fedilink
      arrow-up
      1
      ·
      12 hours ago

      Unlikely. Most of the time on modern hardware, you’re going to be cache-limited, not cycle-limited. Checking one bit in a register is insanely fast.

    • Binette@lemmy.ml
      link
      fedilink
      arrow-up
      1
      ·
      17 hours ago

      exactly! it is more costly for your pc cpu to check for a bit inside a byte, than just get the byte itself, because adresses only point to bytes

    • mindbleach@sh.itjust.works
      link
      fedilink
      arrow-up
      0
      ·
      17 hours ago

      Even on 6502, the BIT command is useless 99% of the time, and AND ~which_bit is the right answer.

      Interestingly the Intel MCS-51 ISA did have several bit-addressable bytes. Like a weird zero page.

      • jsomae@lemmy.ml
        link
        fedilink
        arrow-up
        1
        ·
        12 hours ago

        Only because 6502 has no BIT immediate – only BIT zero page and BIT absolute. But the contemporary z80 and gameboy cpu too have dedicated bit instructions, e.g. BIT c,6 (set z flag to bit 6 of register c).

        • mindbleach@sh.itjust.works
          link
          fedilink
          arrow-up
          2
          ·
          10 hours ago

          I think it’s intended for checking the same bit in multiple bytes. You load the mask instead of the data.

          So much 6502 ASM involves turning your brain inside-out… despite being simple, clever, and friendly. Like how you can’t do a strided array sensibly because there’s no address register(s). There is no “next byte.” Naively, you want separate varied data at the same index is separate arrays. Buuut because each read address is absolute, you can do *(&array+1)[n], for free.

          What I really miss on NES versus Game Boy is SWAP.

  • lorty@lemmy.ml
    link
    fedilink
    arrow-up
    2
    ·
    17 hours ago

    If you want to optimize to this point, do some embedded development. It’s somewhat fun to work at such a low level (testing tends to be annoying though)