Write a C program to dump sections of memory in hexadecimal format. Specifically
ID: 3888552 • 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.
Your program must work correctly with any number of command-line arguments. In addition, your program must produce the exact same format as given in the 32-bit or 64-bit examples. Of course, even if you use the same command-line as illustrated, the addresses in your output will be different than the ones shown in the sample output. In addition, the option system in use will impact the way argv[0] is constructed (e.g., it may or may not include the full path).
Note that the memory locations where the command-line arguments are stored may not follow the same order in which the command-line arguments are stored in argv. For example, notice how in the sample output the string pointed to by argv[0] is stored at a higher memory location than the string pointed to by argv[1].
Hints
• The printf() C library function supports the %p specifier to print pointer address. For example, you canuse:printf("%p", q)toprinttheaddressstoredinq,foranypointerq.Alternatively,youcan alsousethe%Xspecifierasfollows:printf("%08X", q)toprintanyintegertypeinhexadecimalleft- padded with zeroes (0) up to a width of 8 characters.
• To chop an address into its byte components (4 bytes or 8 bytes, depending on whether you are using a 32-bit or 64-bit system, respectively) you can use either bit-wise operators or convert the address to a hexadecimal string first and then perform string manipulation. If you decide to use the latter approach, you may find the itoa C Standard library function handy.
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.