473,379 Members | 1,190 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

write to memory map I/O in a for loop

Hi,

I am trying to write a bit of code to iterate through memory addresses
and if the address is divisable by 2 then write one byte, if not write
a different byte. I am having trouble with the pointer to the address
incremented by for loop.

unsigned long i;

for(i=0xB8000; i<0xC0000; i++)
{
if (i % 2)
*(short *)i = 0x20;
else
*(short *)i = 0x07;
}

Does this look right? I found the *(short *) syntax online in another
bit of code that was used in a if statement, but I am not sure how this
statement works. My guess is it declares the data type at the address
being pointed to, but I cant find anything to confirm that. Or if
someone knows a better way to accomplish this, I would be very
grateful.

In case anyone is wondering, this chunk should blank a video screen.

Thanks for your time...

Feb 20 '06 #1
9 5012
"pointer noob" <de******@gmail.com> writes:
I am trying to write a bit of code to iterate through memory addresses
and if the address is divisable by 2 then write one byte, if not write
a different byte. I am having trouble with the pointer to the address
incremented by for loop.

unsigned long i;

for(i=0xB8000; i<0xC0000; i++)
{
if (i % 2)
Equivalent to "if (i % 2 != 0)", i.e., checks whether i is odd.
*(short *)i = 0x20;
else
*(short *)i = 0x07;
}

Does this look right? I found the *(short *) syntax online in another
bit of code that was used in a if statement, but I am not sure how this
statement works. My guess is it declares the data type at the address
being pointed to, but I cant find anything to confirm that. Or if
someone knows a better way to accomplish this, I would be very
grateful.
i is a variable of type unsigned long.

(short *)i converts the value of i to type short*, i.e., it converts
an integer to a pointer. The way in which this conversion is done
is system-specific.

Once you have this pointer, the unary "*" operator dereferences it.
So

*(short *)i = 0x20;

assigns the value 0x20 to whatever address corresponds to the integer
value of i.

Typically short is 16 bits, and bytes are 8 bits. If so, incrementing
an address is likely to advance the address by one byte; you're
writing a 16-bit value every 8 bits. It's unlikely that that's really
what you want to do. If nothing else, you'll have alignment problems
on many systems. Perhaps you want to use unsigned char* rather than
short*.
In case anyone is wondering, this chunk should blank a video screen.


It may do so on some specific system. Your code is *extremely*
system-specific. Many systems won't allow you to write directly to
video memory or to a specified address.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 21 '06 #2
Thanks Keith,

So I changed the code as follows:

unsigned long i;

for(i=0xB8000; i<0xC0000; i++)
{
if (i % 2)
*(char *)i = 0x07;
else
*(char *)i = 0x20;
}
From your response, I found two errors, one was the boolean modulus

evaluating to true when odd, and then the size of the dereferenced
pointer value. Thanks again for your help...

Feb 21 '06 #3
"pointer noob" <de******@gmail.com> writes:
Thanks Keith,
You're welcome. My fee is quite reasonable; just read
<http://cfaj.freeshell.org/google/>.
So I changed the code as follows:

unsigned long i;

for(i=0xB8000; i<0xC0000; i++)
{
if (i % 2)
*(char *)i = 0x07;
else
*(char *)i = 0x20;
}
From your response, I found two errors, one was the boolean modulus

evaluating to true when odd, and then the size of the dereferenced
pointer value. Thanks again for your help...


I would make the test more explicit: "if (i % 2 == 1)" rather than
"if (i % 2"). It's equivalent (in this case), but I prefer to make
conditional tests more explicit. (A lot of smart people disagree with
me on this point, so feel free to take it with a grain of salt.)

I have no way of knowing whether your code does what you want it to,
but if it works, that's great.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 21 '06 #4
"pointer noob" <de******@gmail.com> writes:
unsigned long i;

for(i=0xB8000; i<0xC0000; i++)
{
if (i % 2)
*(char *)i = 0x07;
else
*(char *)i = 0x20;
}


I might write it like this:

char *base = (char *) 0xb8000;
unsigned offset = 0;
while (offset < 0x8000) {
base[offset++] = 0x20;
base[offset++] = 0x07;
}
--
Bite me! said C.
Feb 21 '06 #5

"pointer noob" <de******@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Thanks Keith,

So I changed the code as follows:

unsigned long i;

for(i=0xB8000; i<0xC0000; i++)
{
if (i % 2)
*(char *)i = 0x07;
else
*(char *)i = 0x20;
}
From your response, I found two errors, one was the boolean modulus

evaluating to true when odd, and then the size of the dereferenced
pointer value. Thanks again for your help...


The if-else and modulo are slow operations. Since the even and odd
addresses will always alternate, increment the counter by 2 storing 2 bytes
at a time. This works as long as your starting address is even.

for(i=0xB8000; i<0xC0000; i+=2)
{
*(unsigned char *)i = 0x07;
*(unsigned char *)(i+1) = 0x20;
}

for(i=0xB8000; i<0xC0000; i+=2)
{
*(unsigned short *)i = 0x2007; /* little endian */
}

Rod Pemberton
Feb 21 '06 #6
On 2006-02-20, pointer noob <de******@gmail.com> wrote:
Hi,

I am trying to write a bit of code to iterate through memory addresses
and if the address is divisable by 2 then write one byte, if not write
a different byte. I am having trouble with the pointer to the address
incremented by for loop.

unsigned long i;

for(i=0xB8000; i<0xC0000; i++)
{
if (i % 2)
*(short *)i = 0x20;
else
*(short *)i = 0x07;
}

Assuming char==byte and not being too anal and speeding it up considerably:

for(i=0xB8000; i<0xC0000; i+=2)
*(short *)i = 0x2007;
Its HW specific, but that doesnt matter since I'm assuming HW
specificness is a requirement because you are writing directly to
video memory. They were the days :) You may need "0x0720 depending on
endian issues.

You can make it twice as fast too : see if you can guess how.

Does this look right? I found the *(short *) syntax online in another
bit of code that was used in a if statement, but I am not sure how this
statement works. My guess is it declares the data type at the address
being pointed to, but I cant find anything to confirm that. Or if
someone knows a better way to accomplish this, I would be very
grateful.

In case anyone is wondering, this chunk should blank a video screen.

Thanks for your time...

--
Remove evomer to reply
Feb 21 '06 #7
"Richard G. Riley" <rg***********@gmail.com> writes:
On 2006-02-20, pointer noob <de******@gmail.com> wrote:
I am trying to write a bit of code to iterate through memory addresses
and if the address is divisable by 2 then write one byte, if not write
a different byte. I am having trouble with the pointer to the address
incremented by for loop.

unsigned long i;

for(i=0xB8000; i<0xC0000; i++)
{
if (i % 2)
*(short *)i = 0x20;
else
*(short *)i = 0x07;
}

Assuming char==byte and not being too anal and speeding it up considerably:


char==byte is guaranteed by the language. Your assumption is that
short is 2 bytes.
for(i=0xB8000; i<0xC0000; i+=2)
*(short *)i = 0x2007;
Its HW specific, but that doesnt matter since I'm assuming HW
specificness is a requirement because you are writing directly to
video memory. They were the days :) You may need "0x0720 depending on
endian issues.


This may or may not work, depending on the underlying hardware. Since
the code is writing to specific hardwired addresses, whatever is at
those addresses might not even behave like ordinary memory. It's not
obvious that writing a 16-bit short will have the same effect as
writing two 8-bit bytes. (I suspect it does, but I wouldn't bet large
sums of money on it.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 21 '06 #8
On 2006-02-21, Keith Thompson <ks***@mib.org> wrote:
"Richard G. Riley" <rg***********@gmail.com> writes:
On 2006-02-20, pointer noob <de******@gmail.com> wrote:
I am trying to write a bit of code to iterate through memory addresses
and if the address is divisable by 2 then write one byte, if not write
a different byte. I am having trouble with the pointer to the address
incremented by for loop.

unsigned long i;

for(i=0xB8000; i<0xC0000; i++)
{
if (i % 2)
*(short *)i = 0x20;
else
*(short *)i = 0x07;
}

Assuming char==byte and not being too anal and speeding it up considerably:


char==byte is guaranteed by the language. Your assumption is that
short is 2 bytes.
for(i=0xB8000; i<0xC0000; i+=2)
*(short *)i = 0x2007;
Its HW specific, but that doesnt matter since I'm assuming HW
specificness is a requirement because you are writing directly to
video memory. They were the days :) You may need "0x0720 depending on
endian issues.


This may or may not work, depending on the underlying hardware.

Since

As I made clear.
the code is writing to specific hardwired addresses, whatever is at
those addresses might not even behave like ordinary memory. It's not
obvious that writing a 16-bit short will have the same effect as
He is clearing a memory buffer. It does.

writing two 8-bit bytes. (I suspect it does, but I wouldn't bet large
sums of money on it.)


I would.
--
Remove evomer to reply
Feb 22 '06 #9
On 20 Feb 2006 15:43:52 -0800, in comp.lang.c , "pointer noob"
<de******@gmail.com> wrote:
Hi,

I am trying to write a bit of code to iterate through memory addresses
and if the address is divisable by 2 then write one byte, if not write
a different byte.
I assume you're aware that on any modern OS this will abort with a
protection violation, since one is not allowed to directly write to
memory.
In case anyone is wondering, this chunk should blank a video screen.


Only on a decade old version of DOS. :-)
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Feb 22 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Vincent | last post by:
Hi I have a problem with pear DB my code : for ($i=0;$i<100000;$i) { $sql = "select id from table where id=x";
18
by: jacob navia | last post by:
In C, we have read-only memory (const), read/write memory (normal data), and write only memory. Let's look at the third one in more detail. Write only memory is a piece of RAM that can only...
27
by: Sune | last post by:
Hi! Pre-requisites: ------------------- 1) Consider I'm about to write a quite large program. Say 500 K lines. 2) Part of this code will consist of 50 structs with, say, no more than at most...
5
by: John Dumais | last post by:
Hello, I have been trying to figure out how to write an array of doubles (in this specific case) to a binary stream without using a loop. What I have been doing is... foreach(double d in...
5
by: Aaron Birkland | last post by:
I have a long but straightforward query (on some very large tables) that always ends in 'Memory exhausted in AllocSetAlloc(108)'. Even stranger are some messages that appear in the logfile, such...
23
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
22
by: Glurt Wuntal | last post by:
I am a newbie with Python. It's a great language, but I would like to be able to present a simple gui menu for some of my scripts; something better than using 'raw_input' prompts. Any...
30
by: MAG1301 | last post by:
I've detected memory leaks in our huge .NET 1.1 C# application but couldn't localize them directly. So I've reduced the code to the following console application: using System; using System.IO;...
5
by: TimDGCB | last post by:
Hello, I'm writing a python script for Amarok, I communicate with Amarok using DCOP. Now, I have to call DCOP very often and I noticed that every time I make a DCOP call my program keeps...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.