def format_reg(regno, fmt):
    try:
        if fmt == 'd':
            return int_argument(regno)
        if fmt == 'u':
            return uint_argument(regno)
        if fmt == 'x':
            return hex_argument(regno)
        if fmt == 's':
            return string_argument(regno)
        return fmt(regno)
    except sim_core.SimExc_Index:
        return "<can't read r%d>" % regno
    except TypeError:
        traceback.print_exc()
        raise "Unknown format element: %s" % fmt