Thread: Program to write to memory map addresses

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    7

    Program to write to memory map addresses

    I am trying to write a very simple C program in which I am trying to write to the memory on my computer in DOS. This is what I am trying to do.

    Say I have the data stored as:
    Code:
    short data=0x1111;
    and my memory location and size is defined as follow:
    Code:
    #define ADDRESSOFMEMORY 0xe0000 //address of memory
    #define SIZEOFMEMORY 0x4000  //size of meory
    Basically I am trying to write the data (0x1111) at the memory map location 0xe000.

    How can I do that?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    First of all, you probably don't want to do that. You'd only want to do that unless you're doing graphical stuff in mode 13h or something, and not only is that a bad idea, but you also need a DOS compiler to do it.

    To answer your question:
    Code:
    short *memory = (short *)ADDRESSOFMEMORY;
    
    *memory = 0x1111;
    That should work, I think. You'll almost certainly get a segmentation fault if you try it with most modern compilers, however.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    7
    Quote Originally Posted by dwks View Post
    First of all, you probably don't want to do that. You'd only want to do that unless you're doing graphical stuff in mode 13h or something, and not only is that a bad idea, but you also need a DOS compiler to do it.

    To answer your question:
    Code:
    short *memory = (short *)ADDRESSOFMEMORY;
    
    *memory = 0x1111;
    That should work, I think. You'll almost certainly get a segmentation fault if you try it with most modern compilers, however.
    Thanks for the info. I am in a situation where I have to run a program like this.

    Also what if I have to write at 4 consequetive memory locations (say e000h,e0002h, e0004h, e0008h) with the data 0x1111.

    For example I have this pseudo code:

    Code:
    for(memoffset=0x0;memoffset<0x8;memoffset+=2)  
        {
    	memory[memoffset]=data;   
        }
    how can I build up a simple program using all the above.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You should be able to figure it out . . . just declare memory as I have done, and then modify it as you have done.

    Also remember that memory+1 is one byte beyond memory+0, if you use chars, but it's two bytes ahead, if you use shorts (on a 32-bit machine). So in other words, your loop would probably look like this:
    Code:
    for(memoffset=0x0;memoffset<0x8;memoffset++)
    Because every time you increment memoffset, you're moving ahead sizeof(short) bytes.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    7
    Quote Originally Posted by dwks View Post
    You should be able to figure it out . . . just declare memory as I have done, and then modify it as you have done.

    Also remember that memory+1 is one byte beyond memory+0, if you use chars, but it's two bytes ahead, if you use shorts (on a 32-bit machine). So in other words, your loop would probably look like this:
    Code:
    for(memoffset=0x0;memoffset<0x8;memoffset++)
    Because every time you increment memoffset, you're moving ahead sizeof(short) bytes.
    Thanks for your persistence.

    Here is the code that I have written. Although it compiles fine and runs I am not sure if I wrote it correctly to do as I want it to do. A quick review will be really helpful.

    Code:
    //The essentials
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    #define ADDR 0xe0000
    
    
        void main (void)
        {
            //Declaration of Variables      
      
            int offset;   
    
            //Declare a pointer to the memory
            short *memory = (short *)ADDR;
    
            short data=0x1234;
    
            for(offset=0x0;offset<0x8;offset++)  
            {
                memory[offset]=data;
            }        
        }
    I am new to this area and I am trying to learn.

    Thank you once again.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Your logic looks pretty good to me. One thing I forgot to mention is that if you're trying to fill in four consecutive blocks of memory, you'd want to use 0x4 instead of 0x8 in your loop.

    Of course, there are some other things with your program that could be improved. For example:
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    7
    Quote Originally Posted by dwks View Post
    Your logic looks pretty good to me. One thing I forgot to mention is that if you're trying to fill in four consecutive blocks of memory, you'd want to use 0x4 instead of 0x8 in your loop.

    Of course, there are some other things with your program that could be improved. For example:
    Thanks dwks!

    Also I noticed that you mentioned to modify the for loop as:
    Code:
    for(memoffset=0x0;memoffset<0x8;memoffset++)
    instead of:

    Code:
    for(memoffset=0x0;memoffset<0x8;memoffset+=2)
    Is that true even if my data variable contains following:

    Code:
    short data=0x11;
    instead of:

    Code:
    short data=0x1111;
    ?

    For example, as far as I understand writing 0x1234 will cause the following:

    e000:0000 - 34
    e000:0001 - 12

    What would happen if my data is just 0x12 ?
    Will it just write to first location and skip to the e000:0002, like this:

    e000:0000 - --
    e000:0001 - 12
    e000:0002 - <Next Two bytes>

    Thank you once again for helping me out.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by modest View Post
    What would happen if my data is just 0x12 ?
    Will it just write to first location and skip to the e000:0002, like this:

    e000:0000 - --
    e000:0001 - 12
    e000:0002 - <Next Two bytes>
    Then it would look like:
    e000:0000 - 00
    e000:0001 - 12
    e000:0002 - <Next Two bytes>
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I think I'll explain a bit about how pointers work. When you have a char* pointer, each element is one byte, so each time you increment the pointer it goes up by one byte. For a short* pointer, assuming 2 byte shorts, each element is 2 bytes, so each time you increment the pointer it advances two bytes.

    This is actually what you want. You want to do this
    Code:
    short data[] = {1, 2, 3};
    printf("&#37;hd\n", *(data + 2));
    printf("%hd\n", data[2]);
    rather than this.
    Code:
    short data[] = {1, 2, 3};
    printf("%hd\n", *(data + 2 * sizeof(short)));
    printf("%hd\n", data[2 * sizeof(short)]);
    In other words, remember that a short pointer goes up in 2-byte increments.

    (Incidentally, this is also why you can't dereference void pointers. Because the compiler doesn't know how big each element is, it can't calculate *(void_pointer + 1).)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    7
    Guys thank you very much for all the advice and help. It seems that my program is not writing to the desired location (e000h) I verified this with DOS "debug" utility. How can I debug my program?

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Do you want to learn how to debug a program or what is wrong with your specific program?

    If the latter, post your current code.

  12. #12
    Registered User
    Join Date
    May 2008
    Posts
    7
    Quote Originally Posted by robwhit View Post
    Do you want to learn how to debug a program or what is wrong with your specific program?

    If the latter, post your current code.
    here is my program that I am using to write to address location e000h
    Code:
    //The essentials
    #include <stdio.h>
    #include <stdlib.h>
    
    #define ADDR 0xe0000
    
    
        int main (void)
        {
            //Declaration of Variables      
      
            int offset;   
    
            //Declare a pointer to the memory
            short *memory = (short *)ADDR;
    
            short data=0x1234;
    
            for(offset=0x0;offset<0x4;offset++)  
            {
                memory[offset]=data;
            }        
        }
    Thanks for offering your help.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    I wasn't offering my help, I was just asking you so that people could help you if they wanted.

    > #define ADDR 0xe0000

    hmm...
    Last edited by robwhit; 06-02-2008 at 05:58 PM.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But are you actually running this in DOS or some other OS (Windows, Linux, Mac)?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    May 2008
    Posts
    7
    Quote Originally Posted by Elysia View Post
    But are you actually running this in DOS or some other OS (Windows, Linux, Mac)?
    DOS.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Replies: 1
    Last Post: 10-13-2004, 12:15 PM
  4. my C++ program hangs during memory deallocation.
    By the-tzar in forum C++ Programming
    Replies: 6
    Last Post: 03-06-2004, 10:39 AM