DynASM dynamically selecting register size

  • From: Scott Graham <scott.luajit@xxxxxxxxxx>
  • To: luajit <luajit@xxxxxxxxxxxxx>
  • Date: Thu, 23 Mar 2023 20:48:48 -0700

Hi,

I'm retrofitting a compiler that outputs text assembler to instead use
DynASM, and it does:

      printf("  xchg [rdi], %s\n", reg_ax(sz));

where reg_ax() is

    static char* reg_ax(int sz) {
      switch (sz) {
        case 1: return "al";
        case 2: return "ax";
        case 4: return "eax";
        case 8: return "rax";
      }
    }

The manual way of rewriting as DynASM, something like:

      switch (sz) {
        case 1:
          | xchg [rdi], al
          break;
        case 2:
          | xchg [rdi], ax
          break;
        case 4:
          | xchg [rdi], eax
          break;
        case 8:
          | xchg [rdi], rax
          break;
      }

is sort of fine, but a bit cumbersome when used various times with
different instructions.

Is there something like Rb, Rq, etc, to select the 0th register by size?
Or is it possible to write a macro to do so? (I tried for a bit but didn't
have much luck.)
Or some better way of structuring the code?

Thank you,
Scott

Other related posts:

  • » DynASM dynamically selecting register size - Scott Graham