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

时间:2016-03-16 18:43:20

标签: c++ class rows member-functions

我目前正在编写一个程序,它使用一个类来模拟8位微控制器的操作。我必须创建5个成员函数 - setPC,show,loadFile,step和constructor。我在程序的某些方面遇到了困难,但是在某一刻,特别是show member功能。这就是我到目前为止所做的:

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

2 个答案:

答案 0 :(得分:0)

这是一个非常好的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 */
};

enter image description here

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

    #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;
}

答案 1 :(得分:0)

  

我不确定如何在行和列中显示内存的内容。

假设您的意思是如何输出 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 

打印一个类似

的模式
Peter 45 4345 
Cary 56 12345

Live Demo