Write a C program to dump sections of memory in hexadecimal format. Specifically
ID: 3885265 • Letter: W
Question
Write a C program to dump sections of memory in hexadecimal format. Specifically, you are required to print a memory map for all memory areas that correspond to the command-line arguments argc and argv.
You may use a 32-bit system, or a 64-bit system. In addition, the output should reflect the native endianness of the system you are using. Two output samples (32-bit, and 64-bit) are provided.
They were both generated using the following command line with little-endian byte order:
$ printMemoryMap A hippopotamus got on the bus
where printMemoryMap is the name of the executable file.
Explanation / Answer
#include <stdio.h>
void hexDump (char *desc, void *addr, int len) {
int i;
unsigned char buff[17];
unsigned char *pc = (unsigned char*)addr;
// Output description if given.
if (desc != NULL)
printf ("%s: ", desc);
if (len == 0) {
printf(" ZERO LENGTH ");
return;
}
if (len < 0) {
printf(" NEGATIVE LENGTH: %i ",len);
return;
}
// Process every byte in the data.
for (i = 0; i < len; i++) {
// Multiple of 16 means new line (with line offset).
if ((i % 16) == 0) {
// Just don't print ASCII for the zeroth line.
if (i != 0)
printf (" %s ", buff);
// Output the offset.
printf (" %04x ", i);
}
// Now the hex code for the specific character.
printf (" %02x", pc[i]);
// And store a printable ASCII character for later.
if ((pc[i] < 0x20) || (pc[i] > 0x7e))
buff[i % 16] = '.';
else
buff[i % 16] = pc[i];
buff[(i % 16) + 1] = '';
}
// Pad out last line if not exactly 16 characters.
while ((i % 16) != 0) {
printf (" ");
i++;
}
// And print the final ASCII bit.
printf (" %s ", buff);
}
int main (int argc, char *argv[]) {
char my_str[] = "a char string greater than 16 chars";
if(argc==2)
{
hexDump (argv[1], &argv, sizeof (argv));
}
else if(argc>2)
{
printf(" too many arguments!!!!");
}
else{
printf(" pass one more argument");
}
getch();
return 0;
}
commands to execute this program:
got to cmd and execute the following command
filename.exe arguments
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.