Skip to content

vmcu_operand_t

Milo-D edited this page Aug 11, 2021 · 3 revisions

Definition

Defined in libvmcu_analyzer.h
typedef struct vmcu_operand { ... } vmcu_operand_t;

Description

vmcu_operand_t represents a single instruction operand. An operand contains an anonymous union for the value
and an instance of VMCU_OPTYPE to annotate the actual operand type.

VMCU_OPTYPE type indicates which union variable should be accessed. For example if .type = VMCU_OPTYPE_K4
{anonymous-union}.k can be used to read the operand value.

typedef struct vmcu_operand {                 ///< operand structure

    union {                                   ///< operand value union

        uint8_t k;                            ///< set if type = K4, K6 or K8
        uint8_t b;                            ///< set if type = B
        uint8_t io;                           ///< set if type = IO5 or IO6

        uint16_t d;                           ///< set if type = D7 or D16
        uint32_t p;                           ///< set if type = P22
        int16_t  s;                           ///< set if type = S7 or S12

        VMCU_REGISTER r;                      ///< set if type = R
        vmcu_registerpair_t rp;               ///< set if type = RP, X, Y, or Z
    };

    VMCU_OPTYPE type;                         ///< operand type

} vmcu_operand_t;

Example

#include "libvmcu_analyzer.h" // vmcu_operand_t

static void print_operand_value(vmcu_operand_t *op) {

    /* printing value based on operand's type */

    switch(op->type) {

        case VMCU_OPTYPE_B:

            printf("%" PRIu8 "\n", op->b);

        break;

        case VMCU_OPTYPE_IO5:
        case VMCU_OPTYPE_IO6:

            printf("0x%02" PRIx8 "\n", op->io);

        break;

        case VMCU_OPTYPE_K4:
        case VMCU_OPTYPE_K6:
        case VMCU_OPTYPE_K8: 

            printf("%" PRIu8 "\n", op->k);
 
        break;

        case VMCU_OPTYPE_D7:
        case VMCU_OPTYPE_D16:

            printf("0x%04" PRIx16 "\n", op->d);

        break;


        case VMCU_OPTYPE_P22: 

            printf("0x%04" PRIx32 "\n", op->p);

        break;

        /* and so on... */

        default: break;
    }
}

int main(const int argc, const char **argv) {
   
    vmcu_instr_t instr;

    vmcu_model_t *m328p = vmcu_model_ctor(VMCU_DEVICE_M328P);
    vmcu_disassemble_bytes(0xddf1, &instr, m328p);

    // pass vmcu_operand_t dest by reference
    print_operand_value(&instr.dest);

    // pass vmcu_operand_t src by reference
    print_operand_value(&instr.src);

    vmcu_model_dtor(m328p);
    return 0;
}

Possible Output

0x0029
-18
0xff
r31:r30

References

[1] See https://en.wikipedia.org/wiki/Atmel_AVR_instruction_set for VMCU_OPTYPE abbreviations.