473,779 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

IVT at address 0

Hi,

Just another question for the standards jockeys...

Suppose I have an Interrupt Vector Table located at address 0x0000 (16-bit
machine). I want to dump the context of the IVT, by treating it as an array
starting at (you guessed it) 0x0000. So I would have

struct iv_s* ivt = (struct iv_s *) 0x0000;

Which will yield a NULL pointer which may not be dereferenced, lest
undefined behavior would result.

Question: How do I get by this? Is it possible without copying the table (in
assembly to avoid dereferencing any pointers) as the current solution does
or blatantly ignore any UB and just count on my compiler not to mind too
much?

Nov 14 '05 #1
15 2243
In article <41************ ***********@dre ader7.news.xs4a ll.nl>,
dandelion <da*******@mead ow.net> wrote:
Suppose I have an Interrupt Vector Table located at address 0x0000 (16-bit
machine).


You can't access such a thing without going outside what standard C
defines, so you shouldn't worry about it being address 0 any more than
if it was address 123456. Either your operating system and compiler
make it work, or they don't; the C standard doesn't come into it.

-- Richard
Nov 14 '05 #2

"Richard Tobin" <ri*****@cogsci .ed.ac.uk> wrote in message
news:co******** ***@pc-news.cogsci.ed. ac.uk...
In article <41************ ***********@dre ader7.news.xs4a ll.nl>,
dandelion <da*******@mead ow.net> wrote:
Suppose I have an Interrupt Vector Table located at address 0x0000 (16-bitmachine).


You can't access such a thing without going outside what standard C
defines, so you shouldn't worry about it being address 0 any more than
if it was address 123456. Either your operating system and compiler
make it work, or they don't; the C standard doesn't come into it.


That's what I figured. Thanks.
Nov 14 '05 #3
"dandelion" <da*******@mead ow.net> wrote:
Suppose I have an Interrupt Vector Table located at address 0x0000 (16-bit
machine). I want to dump the context of the IVT, by treating it as an array
starting at (you guessed it) 0x0000. So I would have

struct iv_s* ivt = (struct iv_s *) 0x0000;

Which will yield a NULL pointer which may not be dereferenced, lest
undefined behavior would result.


Well, first of all, if your system allows you to read and write from
address 0x0000 at all, its way to handle this case of UB should be to
behave as if you used a valid pointer.
However, beware of the snark. For all other integer values, I would
expect the above line to yield the expected pointer. A literal 0,
however, is special. It must, as you say (albeit using the wrong
capitalisation) , evaluate to a null pointer. And a null pointer need not
be address zero. So, to make doubly sure that you do get address zero, I
suggest you use

int i=0;
struct iv_s *ivt=(struct iv_s *)i;

Of course, the pointer value resulting from such a conversion is system-
specific in any case, so even this is no guarantee. However, on the
(admittedly rare) systems where a null pointer is not all bits zero
internally, I would expect the one-line snippet to fail, whereas the
two-line code will probably work even there.

Richard
Nov 14 '05 #4

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:41******** ********@news.i ndividual.net.. .
"dandelion" <da*******@mead ow.net> wrote:
<snip>
int i=0;
struct iv_s *ivt=(struct iv_s *)i;
<snip>

Don't worry...

I've just checked. UB or no UB, it works fine. NULL is defined as ((void *)
0) and portability is not an issue. The SW is laced with direct hw I/O
anyway.
Of course, the pointer value resulting from such a conversion is system-
specific in any case, so even this is no guarantee. However, on the
(admittedly rare) systems where a null pointer is not all bits zero
internally, I would expect the one-line snippet to fail, whereas the
two-line code will probably work even there.


Since the compiler has no way of knowing (well, that's a bit strong, but
probably it don't know) that i is 0 at runtime and therefore treats it like
any other pointer. As soon as i find a compiler for which NULL != ((void *)
0) i'll try your suggestion.

For now i'd like to keep it simple.

Ok. Thanks.
Nov 14 '05 #5
"dandelion" <da*******@mead ow.net> wrote:
int i=0;
struct iv_s *ivt=(struct iv_s *)i;


Since the compiler has no way of knowing (well, that's a bit strong, but
probably it don't know) that i is 0 at runtime and therefore treats it like
any other pointer. As soon as i find a compiler for which NULL != ((void *)
0) i'll try your suggestion.


The only options for NULL are 0 and (void *)0 .
(And any other expression that evaluates to that, eg. (void *)(3 - 3)).
The expression (NULL != ((void *)0) must always be false.

I'm not convinced that the above code won't give you a null
pointer anyway. If your system has a null pointer as
all-bits-zero then the difference is probably academic, but
I would do it this way:

struct iv_s *ivt = (struct iv_s*) sizeof *ivt;
--ivt;
Nov 14 '05 #6

"Old Wolf" <ol*****@inspir e.net.nz> wrote in message
news:84******** *************** ***@posting.goo gle.com...
"dandelion" <da*******@mead ow.net> wrote:
int i=0;
struct iv_s *ivt=(struct iv_s *)i;
Since the compiler has no way of knowing (well, that's a bit strong, but
probably it don't know) that i is 0 at runtime and therefore treats it like any other pointer. As soon as i find a compiler for which NULL != ((void *) 0) i'll try your suggestion.


The only options for NULL are 0 and (void *)0 .
(And any other expression that evaluates to that, eg. (void *)(3 - 3)).
The expression (NULL != ((void *)0) must always be false.


Yes. "NULL != ((void *)" is not the correct way to phrase what I had in
mind. I intended a situation there the null-pointer is not equal to all bits
zero. Sorry for the confusion (I *did* red the FAQ).
I'm not convinced that the above code won't give you a null
pointer anyway. If your system has a null pointer as
all-bits-zero then the difference is probably academic, but
I would do it this way:

struct iv_s *ivt = (struct iv_s*) sizeof *ivt;
--ivt;


The question *is* academic, since on this platform 0x0000 is the
null-pointer, but i'll ask anyway...

The --ivt expression will (just as the snippet of Mr Tobin) evaluate to 0,
whit the same results (in the hypothetical case of a non-0x00..0
null-pointer. Would that not give the same result? Ie. my pointe pointing to
some unwanted part of memory (or worse)?

Hypothetical. I allready checked the real-life situation.
Nov 14 '05 #7
"dandelion" <da*******@mead ow.net> wrote:
"Old Wolf" <ol*****@inspir e.net.nz> wrote:

struct iv_s *ivt = (struct iv_s*) sizeof *ivt;
--ivt;
The --ivt expression will (just as the snippet of Mr Tobin) evaluate to 0,
whit the same results (in the hypothetical case of a non-0x00..0
null-pointer. Would that not give the same result? Ie. my pointe pointing to
some unwanted part of memory (or worse)?


I don't think so. If there is actually an object of type 'struct iv_s'
at address 0, and the first line above worked as expected,
then you have a pointer to one-past-the-end of that object,
so it must be legal to decrement it and then be pointing to
the object.
This code doesn't ever convert an int 0 to a pointer.

Hypothetical. I allready checked the real-life situation.


You found a compiler with NULL not all-bits-zero ?
Nov 14 '05 #8
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote in message news:<41******* *********@news. individual.net> ...
<snip>

int i=0;
struct iv_s *ivt=(struct iv_s *)i;
You can avoid the temp by...

struct iv_s *ivt = (void *) (0,0);
Of course, the pointer value resulting from such a conversion is system-
specific in any case,...


--
Peter
Nov 14 '05 #9
[given a particular problem of constructing a "pointer to address 0"
where CPU-address-0 holds the Interrupt Vector Table or "ivt":]
"Old Wolf" <ol*****@inspir e.net.nz> wrote in message
news:84******* *************** ****@posting.go ogle.com...
I would do it this way:

struct iv_s *ivt = (struct iv_s*) sizeof *ivt;
--ivt;

In article <41************ ***********@dre ader15.news.xs4 all.nl>,
dandelion <da*******@mead ow.net> wrote:The --ivt expression will (just as the snippet of Mr Tobin) evaluate to 0,
whit the same results (in the hypothetical case of a non-0x00..0
null-pointer. Would that not give the same result? Ie. my pointe pointing to
some unwanted part of memory (or worse)?

Hypothetical . I allready checked the real-life situation.


There are a number of flawed ideas behind the question to start
with.

First, all we know about this hypothetical machine is that it has
an Interrupt Vector Table at CPU-address 0.

We need to know more. In order to make progress, I will define
some more about Version 1 of this particular hypothetical machine.

This is a word-addressed machine, with 32-bit words and 8-bit
"char"s.

The C compiler addresses chars with "byte pointers" that are
made by taking the machine's native "word pointers" and shifting
them left two bits. The two low-order bits are then used as
the byte index within the 32-bit word. Converting a byte
pointer to a word pointer uses a right-shift operation, discarding
the byte offset. Code of the form:

int *ip;
void *vp;
int x;

ip = &x;
vp = ip;

printf("(unsign ed int)ip: %x (unsigned int)vp: %x\n",
(unsigned int)ip, (unsigned int)vp);

compiles to assembly of the form:

mov [addr_of_x], r1 # ip = &x
sll r1, 2, r2 # vp = ip

mov [addr_of_str], a0 # string in arg0 register
mov r1, a1 # arg1 in arg1 register
mov r2, a2 # arg2 in arg2 register
call printf # invoke printf()

and hence prints things like:

(unsigned int)xp: 0x100412c1 (unsigned int)vp: 0x40104b04

i.e., the value in vp is numerically four times greater than
that in xp. Pointer-to-integer casts simply take the raw value
stored in the pointer; it is up to the programmer to make sure
that he knows whether he is dealing with a byte pointer (with
the extra low-order bits) or a word pointer.

Structure pointers are always word pointers; structures are
always a multiple of four of the 8-bit bytes long. A struct
holding a single "char" has three bytes of padding.

We are almost there, but we still need to know how integer-to-pointer
conversions work. Here things are a bit odd: the integral constant
zero converts, at compile time, to the machine's internal nil
pointer, which is 0x3fffffff as a word pointer, and thus 0xfffffffc
as a byte pointer:

int *ip;
void *vp;
ip = 0;
vp = 0;

compiles to:

mov #3fffffff, r1 # ip = NULL
mov #fffffffc, r2 # vp = NULL

Since we need to address the IVT structure at CPU-location-zero
(not CPU-location-0x3fffffff), we cannot just write:

struct iv_s *ivt = 0; /* doesn't work - sets register to 0x3fffffff */

Adding a cast does not help, because we are still using a "null
pointer constant" as the C standard defines the term. So we
might resort to Old Wolf's attempt:

struct iv_s *ivt = (struct iv_s*) sizeof *ivt;
--ivt;

Unfortunately, this does not work either. Here sizeof *ivt is,
say, 64 -- big enough to hold 16 4-byte vector entries -- but
we need to set the register to 16, not 64. The reason is that
"--ivt" moves it down by 64 bytes, which is 16 words:

mov 64, r1 # ivt = (struct iv_s *)sizeof *ivs;
sub 16, r1 # ivt--

Hence the correct C code is:

struct iv_s *ivt = (struct iv_s *)16;
--ivt;

which compiles to:

mov 16, r1
sub 16, r1

leaving r1 set to 0 as desired. Or, equivalently, we can try:

const int i = 0;
struct iv_s *ivt = (struct iv_s *)0;

because in C, "i" is not an "integer constant" at all (despite
the red-herring "const" keyword), hence it is not an integer
constant zero. This might compile to:

mov 0, r1 # i = 0
mov r1, r2 # ivt = (struct iv_s *)i

Now we move from Version 1 of this machine to Version 2. Here the
compiler-writer has decided that he regrets his multiple pointer
formats with shift operations at every conversion. But he has not
chosen to make bytes be 32 bits long; instead, he has decided to
smuggle the 8-bit-byte offset into the *high* two bits of a 32-bit
word. (The hardware makes this particularly easy because the top
two bits are never put out on the address bus -- which is only 30
bits wide. The hardware uses 32-bit words, after all, so the
machine still addresses 4 giga-octets of memory.)

On Version 2 of the machine, we still need the same 16 that will
get subtracted by "--ivt", and the same C-source-level tricks work.

Version 3 of this machine, on the other hand, has new and different
hardware. The builders of Version 1 and Version 2 got sick of
trying to deal with an external 8-bit-wide world using 32-bit-wide
instructions, so they have rewired everything to use conventional
byte addressing. The machine's internal null pointers are still
0x3fffffff, for backwards-compatibility with Version 2, so:

struct iv_s *ivt = 0;

still does not work; but now instead of setting ivt to 16, we have
to set it to 64, because "--ivt" generates a "sub 64,r1":

struct iv_s *ivt = (struct iv_s *)64;
--ivt;

Of course, after spending dozens of man-years of work fixing broken
C code, the builders of this machine finally make Version 4, which
uses 8-bit-byte-addressed memory and has its internal null pointers
as all-bits-zero. They do this because any other arrangement is
just too painful. This is, of course, the same reason the IA32
architecture is still bug-for-bug compatible with the 80186: as it
turns out, hardware is quite soft, but software is almost impossibly
hard. :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #10

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

Similar topics

21
15728
by: Alexander N. Spitzer | last post by:
If I have a machine with 3 virtual IP addresses (192.168.1.), how can I start 3 instances of the same RMI application (each started with different properties/configs), each listening on the port 1234, but each instance binds to a different ip address. that is to say: instance #1 binds to 192.168.1.5/port 1234 instance #2 binds to 192.168.1.6/port 1234 instance #3 binds to 192.168.1.7/port 1234
8
4599
by: YAN | last post by:
Hi, I want to get the mac address from a machine, which i have the IP address of that machine, how can i do that? I know how to get the mac address of the local machine from the following code: Dim mc As System.Management.ManagementClass Dim mo As System.Management.ManagementObject mc = New System.Management.ManagementClass("Win32_NetworkAdapterConfiguration")
7
21317
by: Privacy Advocate | last post by:
//crossposted to: comp.lang.javascript, alt.comp.lang.javascript in an effort to get factual answers from JavaScript experts// Simply put; Is it possible to obtain the real (actual) IP address of someone (client) that visits a web site through an anonymous proxy if this person ONLY has JavaScript enabled in their browser? This is NOT a question about PHP, perl, VBScript, Java(.class), or ActiveX. Let us _only_ deal with JavaScript for...
33
3188
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
4
6317
by: andreas.w.h.k. :-\) | last post by:
How do I change the address location in the wsdl <wsdl:port name="SearchSoap12" binding="tns:SearchSoap12"> <soap12:address location="http://searchservices/engine/search.asmx" /> </wsdl:port> Anderas
1
2938
by: Phoenix_ver10 | last post by:
I have a mailing list with multiple names going to the same addresses. I need one address with all the names for that address on it. I checked out the example on microsoft's site, but A: It doesn't work (error that there is an extra parenthise (sp?) ) and B: Will only let in two names for each record. If there are three, the middle on is deleted. Or to make things simpler, if nothing else, I'd like to add a field in the table that shows...
1
2362
by: Jamie J. Begin | last post by:
I'm very new to the world of Python and am trying to wrap my head around it's OOP model. Much of my OOP experience comes from VB.Net, which is very different. Let's say I wanted to create an object that simply outputted something like this: Developer Detroit Michigan
6
7052
by: Nicolas Noakes | last post by:
Hello, I would like to convert to following process to code. Any advice is welcome. I have a hardware device which requires the this procedure to set it's IP address. First create an static ARP entry for the device's MAC address and the desired IP address. Then telnet to this IP address on TCP port 1. This will set the device to temporarily respond to that IP address. Now you can use HTTP to access the device's web interface and
36
3398
by: Julienne Walker | last post by:
Ignoring implementation details and strictly following the C99 standard in terms of semantics, is there anything fundamentally flawed with describing the use of a (non-inline) function as an address? I keep feeling like I'm missing something obvious. -Jul To keep things in context, this is in reference to describing functions to a beginner.
1
3140
by: saravanatmm | last post by:
I need javascript code for validate the email address. Email address field cannot allowed the capital letters, special characters except '@' symbol. But can allowed the small letters, numeric numbers. Now i use this script for validate the email address. But it allows the cpital letters otherwise its working correctly. SCRIPT FUNCTION ************************************************
0
10306
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...
0
10138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9930
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
8961
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
6724
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
5373
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
5503
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2869
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.