void hexdump(void* ptr, const int buflen)
{
unsigned char* buf = (unsigned char*)ptr;
int i, j, d, hex = 0;
short* ins;
string op;
for (i = 0; i < buflen; i += 16) {
for (j = 0; j < 16; j += 4) {
if (i + j < buflen) {
cout << buflen << endl;
cout << "inst " << (i+j) / 4 << ": ";
我使用的是linux ubuntu服务器。我编程的目的是读取机器代码二进制文件,得到汇编代码并打印出来。然而,上面的代码是打印故障的地方。直到cout<<“inst”<<(i+j)/4<<“:”;它工作,并打印bufen(24),但在此之后**分段错误(核心转储)**出现,我的执行停止。这些是代码的其余部分。(查找,尚未创建或使用功函数)
# include <fstream>
# include <vector>
# include <iostream>
# include <algorithm>
using namespace std;
string find(char op[7]);
void work(string inst, short* ins);
void hexdump(void* ptr, const int buflen)
{
unsigned char* buf = (unsigned char*)ptr;
int i, j, d, hex = 0;
short* ins;
string op;
for (i = 0; i < buflen; i += 16) {
for (j = 0; j < 16; j += 4) {
if (i + j < buflen) {
cout << buflen << endl;
cout << "inst " << (i+j) / 4 << ": ";
for (int a = 0; a < 32; a += 8) {
d = buf[i + j + a / 8];
for (int k = 0; k < 8; k++) {
if (d % 2 != 0) {
ins[k + a] = 1;
}
else {
ins[k + a] = 0;
}
d = d / 2;
}
}
for (int i = 31; i >= 0; i -= 4) {
hex = hex + ins[i] * 8;
hex = hex + ins[i - 1] * 4;
hex = hex + ins[i - 2] * 2;
hex = hex + ins[i - 3] * 1;
if (hex == 10)
printf("a");
else if (hex == 11)
printf("b");
else if (hex == 12)
printf("c");
else if (hex == 13)
printf("d");
else if (hex == 14)
printf("e");
else if (hex == 15)
printf("f");
else
printf("%d", hex);
hex = 0;
}
for (int i = 6; i >=0; i--) {
if (ins[i] == 1)
op.append("1");
else if (ins[i] == 0)
op.append("0");
}
cout << endl << op << endl;
//work(find(op), ins);
printf("n");
}
}
}
}
int main(int argc, char* argv[])
{
ifstream in;
in.open(argv[1], ios::in | ios::binary);
if (in.is_open())
{
// get the starting position
streampos start = in.tellg();
// go to the end
in.seekg(0, std::ios::end);
// get the ending position
streampos end = in.tellg();
// go back to the start
in.seekg(0, std::ios::beg);
// create a vector to hold the data that
// is resized to the total size of the file
std::vector<char> contents;
contents.resize(static_cast<size_t>(end - start));
// read it in
in.read(&contents[0], contents.size());
// print it out (for clarity)
hexdump(contents.data(), contents.size());
}
in.close();
return 0;
}
string find(char op[7]) {
string inst("unknown instruction");
if(op=="")
return inst;
}
void work(string inst, short* ins);
1条答案
按热度按时间uqxowvwt1#
TLDR:变量
ins
指向随机内存,因为代码从未将其赋给任何有效的值。因此,当取消引用该指针并写入其地址时,您会有未定义的行为(崩溃是最有可能的结果)。