def encode_reg_instr(instr):
    """
    Encode an R-type instruction as a number.
    :param instr: list, R-type instruction operands, e.g. ['add', 'r1', 'r2', 'r3']
    :return: int, encoded instruction.
    """
    hex_instr = 0

    # Decode.
    cmd = instr[0]
    dst = instr[1]
    src_1 = instr[2]
    src_2 = instr[3]

    # Encode opcode.
    if cmd == 'add':
        hex_instr += 0x0 << 25
    elif cmd == 'sub':
        hex_instr += 0x1 << 25
    else:
        hex_instr += 0x2 << 25

    # Encode destination register.
    dst_reg = int(''.join(c for c in dst if c.isdigit()), 16)
    hex_instr += dst_reg << 20

    # Encode first source register.
    src_1_reg = int(''.join(c for c in src_1 if c.isdigit()), 16)
    hex_instr += src_1_reg << 15

    # Encode second source register.
    src_2_reg = int(''.join(c for c in src_2 if c.isdigit()), 16)
    hex_instr += src_2_reg << 10

    return hex_instr