"Bill Cunningham" <nospam@net> wrote:
I'm trying to write a program that will take ROM and show the values.
That is possible on many systems - however note that it's undefined
behaviour to read from an address that is not a valid C object.
Generally either it will work, or your program will crash.
Is that address 0-255?
That's implementation-specific - it might be on your computer.
Would this involve malloc or memcpy?
You can't get ROM from malloc, as the memory returned by
malloc must be writeable. Using memcpy is possible but
unnecessary.
How can you alter memory values if you do know how to
find them?
If they are in ROM, you can't alter them.
Here's a program that will print out the values from a specified
memory range, with each byte in hexadecimal then any printable
characters at the end of the line.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define WIDTH 19
int main(int argc, char **argv)
{
if(argc != 3) /* If called with wrong number of arguments */
{
printf("I require two arguments: length to print, and base address in hex\n");
}
else
{
size_t i, n = strtoul(argv[1], 0, 0); /* Parse length */
unsigned char *p = (unsigned char *)strtoul(argv[2], 0, 16); /* Parse base address */
char buf[WIDTH + 1]; /* Allocate buffer for printable characters */
for(i = 0; i < n; i++) /* For the specified length */
{
if(i > 0 && i % WIDTH == 0) /* If at end of line */
{
buf[WIDTH] = 0; /* Terminate the string */
printf("%s\n", buf); /* And print it with a newline */
}
printf("%02X ", p[i]); /* Print the next value from memory */
buf[i % WIDTH] = isprint(p[i]) ? p[i] : ' '; /* If printable store in buffer else store space
*/
}
putchar('\n'); /* Terminate last line */
}
return 0; /* Program ended successfully */
}
Note that this program will only work if your program is allowed
to access the specified memory range.
Sample run:
C:\docs\prog\c>gcc -std=c99 -pedantic -Wall -W -O2 rom.c -o rom
C:\docs\prog\c>rom 99 22ff00
7C 02 00 00 72 6F 6D 00 39 39 00 32 32 66 66 30 30 00 FD | rom 99 22ff00
7F FF FF FF FF 00 00 00 00 01 00 00 00 0D 00 0E 00 20 3C <
23 00 E0 FF 22 00 90 00 01 61 E0 FF 22 00 00 00 00 00 FF # " a "
FF FF FF 78 FF 22 00 90 FF 22 00 ED 52 00 61 E0 FE 0C 61 x " " R a a
FE FF FF FF D8 03 00 00 04 FE 0C 61 00 00 00 00 00 00 00 a
00 02 00 00
As you can see on the right, I picked the memory area that my
implementation uses to store the command line strings.
--
Simon.