以行列显示内存,每行开始显示每行起始地址

Displaying memory in rows and columns, with starting address of each row displayed at the beginning of each line

我目前正在编写一个程序,它使用 class 来模拟 8 位微控制器的操作。我必须创建 5 个成员函数 - setPC、show、loadFile、step 和构造函数。我在程序的某些方面遇到了困难,但目前,特别是显示成员功能。这是我目前所拥有的:

void cpu::show(void)

{

cout << "AccA = " << hex << static_cast<int>(AccA) << endl;

cout << "AccB = " << hex << static_cast<int>(AccB) << endl;

cout << "PC = " << hex << static_cast<int>(PC) << endl;

cout << "SP = " << hex << static_cast<int> (SP) << endl;

cout << "X = " << static_cast<int> (X) << endl;

cout << "Memory = " << hex << Memory << endl;

}

该成员函数应该以2位十六进制格式显示寄存器和内存的内容。它应该以16行和16列显示内存,每行的起始地址显示在每行的开头(然后该行的其余部分可以为空白)。我不确定如何在行和列中显示内存内容。我将不胜感激有关如何执行此操作的任何指示。

这是程序的头文件:

#ifndef CPU_H
#define CPU_H

#define MEMSIZE 256

class cpu
{
private:
  unsigned char AccA;
  unsigned char AccB;
  unsigned char PC;
  unsigned char SP;
  unsigned char X;
  bool Z;
  unsigned char Memory[MEMSIZE];
public:
  cpu();
  void loadFile(const char *fileName);
  void setPC(unsigned char aAddress);
  bool step(void);
  void show(void);
};

#endif // CPU_H

这里很不错tutorial

基本的二维数据结构之一是数组数组。这就是您声明多维数组的方式:

type arrayName [ x ][ y ];

这就是你最初用数据填充它的方式:

   int a[3][4] = {  
 {0, 1, 2, 3} ,   /*  initializers for row indexed by 0 */
 {4, 5, 6, 7} ,   /*  initializers for row indexed by 1 */
 {8, 9, 10, 11}   /*  initializers for row indexed by 2 */
};

这就是您访问其数据的方式:

    #include <iostream>
using namespace std;

int main ()
{
   // an array with 5 rows and 2 columns.
   int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};

   // output each array element's value                      
   for ( int i = 0; i < 5; i++ )
      for ( int j = 0; j < 2; j++ )
      {
         cout << "a[" << i << "][" << j << "]: ";
         cout << a[i][j]<< endl;
      }

   return 0;
}

I'm unsure of how to display the contents of memory in rows and columns.

假设您的意思是如何输出 Memory 数组的部分,您应该使用这样的循环:

 bool print_address = true;
 std::cout << std::hex << std::setfill('0');
 for(size_t i = 0; i < MEMSIZE; ++i) {
     if(print_address) {
          std::cout << (void*)&Memory[i] << ' ';
          print_address = false;         
     }
     std::cout << std::setw(2) << static_cast<unsigned int>(Memory[i]) << ' ';         
     if(((i + 1) % 16) == 0) {
         print_address = true;
         std::cout << std::endl;
     }
 }

打印类似

的图案
0x7fff95c1a410 06 00 00 00 10 00 00 00 01 00 00 00 00 00 00 00 
0x7fff95c1a420 78 12 60 00 00 00 00 00 00 00 00 00 00 00 00 00 
0x7fff95c1a430 08 a6 c1 95 ff 7f 00 00 18 a6 c1 95 ff 7f 00 00 
0x7fff95c1a440 00 00 00 00 00 00 00 00 74 e7 08 af 46 7f 00 00 
0x7fff95c1a450 01 00 00 00 46 7f 00 00 00 00 00 00 00 00 00 00 
0x7fff95c1a460 48 07 21 ae 46 7f 00 00 28 79 07 af 46 7f 00 00 
0x7fff95c1a470 e0 73 07 af 46 7f 00 00 01 00 00 00 00 00 00 00 
0x7fff95c1a480 02 00 00 00 00 00 00 00 00 10 60 00 00 00 00 00 
0x7fff95c1a490 01 00 00 00 00 00 00 00 35 52 09 af 46 7f 00 00 
0x7fff95c1a4a0 00 00 00 00 00 00 00 00 00 a4 c1 95 ff 7f 00 00 
0x7fff95c1a4b0 b8 12 60 00 00 00 00 00 60 09 40 00 00 00 00 00 
0x7fff95c1a4c0 d4 13 60 00 00 00 00 00 a8 38 24 ae 46 7f 00 00 
0x7fff95c1a4d0 01 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00 
0x7fff95c1a4e0 00 10 60 00 00 00 00 00 bd 0c 40 00 00 00 00 00 
0x7fff95c1a4f0 00 00 00 00 00 00 00 00 60 0c 40 00 00 00 00 00 
0x7fff95c1a500 00 00 00 00 00 00 00 00 38 0b 40 00 00 00 00 00 

Live Demo