You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.cc 1.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "../checker.h"
  2. // READ: 派生类 <https://zh.cppreference.com/w/cpp/language/derived_class>
  3. class Instruction {
  4. public:
  5. uint32_t raw;
  6. public:
  7. Instruction(uint32_t raw) : raw(raw) {}
  8. };
  9. class RTypeInstruction: public Instruction {
  10. public:
  11. RTypeInstruction(uint32_t raw) : Instruction(raw) {}
  12. void get_rs1() {
  13. std::cout << ((this->raw >> 15) & 0b11111) << std::endl;
  14. }
  15. void get_rs2() {
  16. std::cout << ((this->raw >> 20) & 0b11111) << std::endl;
  17. }
  18. void get_rd() {
  19. std::cout << ((this->raw >> 7) & 0b11111) << std::endl;
  20. }
  21. std::string get_op() {
  22. return "Not implementaed due to unknown instruction type";
  23. }
  24. };
  25. class AddInst: public RTypeInstruction {
  26. public:
  27. AddInst(uint32_t raw) : RTypeInstruction(raw) {}
  28. std::string get_op() {
  29. return "add op";
  30. }
  31. };
  32. int main() {
  33. auto inst = AddInst(0x00728b33);
  34. inst.get_rs1();
  35. inst.get_rs2();
  36. inst.get_rd();
  37. inst.get_op();
  38. auto rinst1 = dynamic_cast<RTypeInstruction*>(&inst);
  39. if (rinst1) {
  40. // TODO: 填入正确答案
  41. ASSERT(rinst1->get_op() == "", "get_op() should return " + rinst1->get_op());
  42. }
  43. RTypeInstruction rinst2 = inst;
  44. // TODO: 填入正确答案
  45. ASSERT(rinst2.get_op() == "", "get_op() should return " + rinst2.get_op());
  46. return 0;
  47. }

计算机大作业

Contributors (1)