diff --git a/devcmd.c b/devcmd.c index a5c1bed..0188c94 100644 --- a/devcmd.c +++ b/devcmd.c @@ -240,17 +240,14 @@ static int cmd_set(cproc_t cp, char **arg) return -1; } - while (*reg_text && !isdigit(*reg_text)) - reg_text++; - reg = atoi(reg_text); - - if (expr_eval(stab, val_text, &value) < 0) { - fprintf(stderr, "set: can't parse value: %s\n", val_text); + reg = dis_reg_from_name(reg_text); + if (reg < 0) { + fprintf(stderr, "set: unknown register: %s\n", reg_text); return -1; } - if (reg < 0 || reg >= DEVICE_NUM_REGS) { - fprintf(stderr, "set: register out of range: %d\n", reg); + if (expr_eval(stab, val_text, &value) < 0) { + fprintf(stderr, "set: can't parse value: %s\n", val_text); return -1; } diff --git a/dis.c b/dis.c index a3c29dc..3814ead 100644 --- a/dis.c +++ b/dis.c @@ -526,7 +526,7 @@ const char *dis_opcode_name(msp430_op_t op) return NULL; } -msp430_op_t dis_opcode_from_name(const char *name) +int dis_opcode_from_name(const char *name) { int i; @@ -544,19 +544,9 @@ static const char *const msp430_reg_names[] = { "R12", "R13", "R14", "R15" }; -msp430_reg_t dis_reg_from_name(const char *name) +int dis_reg_from_name(const char *name) { - const char *num = name; - - while (num && isdigit(*num)) - num++; - - if (*num) { - msp430_reg_t r = atoi(num); - - if (r >= 0 && r <= 15) - return r; - } + int i; if (!strcasecmp(name, "pc")) return 0; @@ -565,7 +555,18 @@ msp430_reg_t dis_reg_from_name(const char *name) if (!strcasecmp(name, "sr")) return 2; - return -1; + if (toupper(*name) == 'R') + name++; + + for (i = 0; name[i]; i++) + if (!isdigit(name[i])) + return -1; + + i = atoi(name); + if (i < 0 || i > 15) + return -1; + + return i; } const char *dis_reg_name(msp430_reg_t reg) diff --git a/dis.h b/dis.h index 9f45f0d..3575600 100644 --- a/dis.h +++ b/dis.h @@ -207,9 +207,9 @@ int dis_decode(const uint8_t *code, struct msp430_instruction *insn); /* Look up names for registers and opcodes */ -msp430_op_t dis_opcode_from_name(const char *name); +int dis_opcode_from_name(const char *name); const char *dis_opcode_name(msp430_op_t op); -msp430_reg_t dis_reg_from_name(const char *name); +int dis_reg_from_name(const char *name); const char *dis_reg_name(msp430_reg_t reg); #endif diff --git a/rtools.c b/rtools.c index dc21ff9..da2f71c 100644 --- a/rtools.c +++ b/rtools.c @@ -54,6 +54,7 @@ static int isearch_opcode(cproc_t cp, const char *term, char **arg, struct isearch_query *q) { const char *opname = get_arg(arg); + int opc; if (q->flags & ISEARCH_OPCODE) { fprintf(stderr, "isearch: opcode already specified\n"); @@ -65,11 +66,12 @@ static int isearch_opcode(cproc_t cp, const char *term, char **arg, return -1; } - q->insn.op = dis_opcode_from_name(opname); - if (q->insn.op < 0) { + opc = dis_opcode_from_name(opname); + if (opc < 0) { fprintf(stderr, "isearch: unknown opcode: %s\n", opname); return -1; } + q->insn.op = opc; q->flags |= ISEARCH_OPCODE; return 0; @@ -170,9 +172,12 @@ static int isearch_reg(cproc_t cp, const char *term, char **arg, return -1; } - while (*reg_text && !isdigit(*reg_text)) - reg_text++; - reg = atoi(reg_text); + reg = dis_reg_from_name(reg_text); + if (reg < 0) { + fprintf(stderr, "isearch: unknown register: %s\n", + reg_text); + return -1; + } q->flags |= which; if (which == ISEARCH_SRC_REG)