view memory

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

I'm trying to write a program that will take ROM and show the values. Is
that address 0-255? Would this involve malloc or memcpy? How can you alter
memory values if you do know how to find them?

Bill
 
J

Jack Klein

I'm trying to write a program that will take ROM and show the values. Is
that address 0-255? Would this involve malloc or memcpy? How can you alter
memory values if you do know how to find them?

Bill

C has no concept of ROM. Some computers might have some things in
ROM, or more likely today in flash, but C provides no particular
method to access it, and it certainly has nothing to do with either
malloc or memcpy.

If you want to know if the particular compiler that you use has some
non-standard extension to provide a method of accessing such ROM as
there might be in a particular hardware platform that compiler makes
programs for, ask in a support group for that particular
compiler/platform combination.

This is not a language issue.

And as to how you can alter memory values in ROM (Read Only Memory)
from a C program, the answer is ... you can't. If you could it
wouldn't be ROM.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
S

Simon Biber

Bill Cunningham said:
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); /* Print the next value from memory */
buf[i % WIDTH] = isprint(p) ? p : ' '; /* 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.
 
T

Thomas Matthews

Bill said:
I'm trying to write a program that will take ROM and show the values. Is
that address 0-255?
Depends on your platform. On my platform ROM is 0x0000
to 0x7fff. But on another similar platform, it is RAM.

Would this involve malloc or memcpy?
Not malloc, since malloc _allocates_ read and writeable memory from
some operating system specified area. There is no method in C to
tell malloc() where to allocate from.

You could use memcpy, but that would be duplicating the effort.
Why not just read the values directly?
const unsigned char * p_byte = 0x1000;
printf("Value at 0x1000 is: 0x%02X\n", *p_byte);
{This is provided that the operating system and your platform allow
access to this address and there is something to read at the above
address.}

How can you alter memory values if you do know how to find them?

Bill

You can either find the documentation that describes the memory
mapping of your platform or use random addresses.

But then, maybe you should not be modifying those values.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
B

Bill Cunningham

As you can see on the right, I picked the memory area that my
implementation uses to store the command line strings.
How do you find out what RAM memory you have? Write a C program I guess. I
have 96M CPU.
 
M

Mark Gordon

How do you find out what RAM memory you have? Write a C program I
guess. I have 96M CPU.

You read your documentation and/or ask on a group dedicated to your
platform. Standard C has no mechanism for finding out how much RAM you
have.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top