473,782 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5030
"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_Keit h) 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_Keit h) 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******** *************@o 13g2000cwo.goog legroups.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_Keit h) 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.or g> 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
2213
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
3713
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 be written to, since its contents are undefined. The program is allocating a new piece of data, and the previous contents aren't relevant. This memory
27
5695
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 1K bytes of data. 3) These structs are to be used by all of the other 500K lines in various places.
5
5994
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 TraceData) { instanceOfBinaryWriter.Write(d); }
5
2216
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 as the following (edited for length, repetitions, etc): TopMemoryContext: 32792 total in 4 blocks; 9712 free (2 chunks); 23080 used TopTransactionContext: 8192 total in 1 blocks; 8176 free (0 chunks); 16 used DeferredTriggerXact: 0 total in 0...
23
4571
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
5060
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 recommendations for a program that will allow me to create the gui screens? Something useable in Linux. thanks.
30
4703
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; namespace MemLeak { class MemLeak
5
1411
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 growing in memory size. To make sure it was DCOP i wrote the small program below: from dcopext import DCOPClient, DCOPApp
0
10313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9944
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.