帮助指针和绝对内存地址 [英] Help with pointers and absolute memory addresses

查看:97
本文介绍了帮助指针和绝对内存地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我对指针有一些困惑,并且隐含地指定要写入的内存大小




我会尝试解释一下我想要做些什么。


char * myaddress =(char *)0xB8000;


现在,我可以这样说:


* myaddress = 3;


和'3'的8位值'将写在绝对地址0xB8000


如果我想明确地将16位或32位值写入该地址怎么办?

显然我需要使用演员这样做,但我的记忆很糟糕,而且b $ b编译器对我的尝试不满意。


比如说:

unsigned short somevalue = 0xffff; // watcom C unsigned short = 16 bit Uint


现在我希望''somevalue''的内容写在绝对地址

''myaddress ''


这样做的正确方法是什么?


(无符号短*)* myaddress = somevalue;


编译但生成编译器警告关于演员的左值不是

标准CI实际上看了汇编代码输出这段代码

产生它不是将正确的大小写入该地址。


如果

建议是关于指针的文档,那么任何建议/相关的阅读技巧都会受到赞赏,我需要阅读

来处理这个问题。


提前致谢。

Alex。

Hi Guys,

I have some confusion with pointers and implicitly specifying a size of
memory to write.

I will try to explain what I am trying to do a bit better.

char *myaddress=(char *) 0xB8000;

now, I can say something like:

*myaddress=3;

And the 8 bit value of ''3'' will be written at absolute address 0xB8000

What if I want to explicitly write a 16 or 32 bit value to that address?
Obviously I need to use casts to do this but my memory is terrible and
the compiler is unhappy with what I try.

Say for example:

unsigned short somevalue=0xffff; //watcom C unsigned short = 16 bit Uint

Now I want the content of ''somevalue'' to be written at absolute address
''myaddress''

Whats the correct way to do this?

(unsigned short *) *myaddress=somevalue;

Compiles but generates compiler warnings about lvalue of cast not being
standard C.I have actually looked at the assembly code output this code
produces and it it not writing the correct size to that address.

Any suggestions/relevant reading tips would be appreciated, if the
advice is going to be about documentation on pointers, i need reading
that deals with this very subject.

Thanks in advance.
Alex.

推荐答案

Yep写道:
Yep wrote:

大家好,


我与指针混淆并隐式指定写一个大小

的内存。


我会试着解释一下我想做些什么。

char * myaddress =(char *)0xB8000;


现在,我可以这样说:


* myaddress = 3;


''3'的8位值将写在绝对地址0xB8000


如果我想明确怎么办?写一个16或32位的值到该地址?
Hi Guys,

I have some confusion with pointers and implicitly specifying a size of
memory to write.

I will try to explain what I am trying to do a bit better.

char *myaddress=(char *) 0xB8000;

now, I can say something like:

*myaddress=3;

And the 8 bit value of ''3'' will be written at absolute address 0xB8000

What if I want to explicitly write a 16 or 32 bit value to that address?



只需按照相同的模式,


T * myaddress =(T *)0xB8000;


* myaddress = someT;


-

Ian Collins。

Just follow the same pattern,

T *myaddress = (T*)0xB8000;

*myaddress = someT;

--
Ian Collins.


文章< 48 *********************** @ news.optusnet.com.au> ,

是的< yep @ yepwrote:
In article <48***********************@news.optusnet.com.au> ,
Yep <yep@yepwrote:

>我对指针有一些混淆并隐含地指定了
的大小记忆要写。
>I have some confusion with pointers and implicitly specifying a size of
memory to write.


> char * myaddress =(char *)0xB8000;
>char *myaddress=(char *) 0xB8000;


>(unsigned short *)* myaddress = somevalue;
>(unsigned short *) *myaddress=somevalue;


>编译但生成编译器警告,关于演员的左值不是标准C.
>Compiles but generates compiler warnings about lvalue of cast not being
standard C.



不幸的是,为了你的目的,标准C将它留给

实现来定义什么(如果有的话)含义

将一个整数转换成一个指针,以及什么(如果有的话)含义

来访问结果位置。

那说:你最好不要尝试:


*(unsigned short *)myaddress = somevalue;

-

"总是用这种坚硬的宝石般的火焰燃烧,维持这个

狂喜,是人生的成功。 - Walter Pater

Unfortunately for your purposes, standard C leaves it up to the
implementation to define what (if any) meaning there is to
converting an integer into a pointer, and what (if any) meaning
there is to accessing the resulting location.
That said: you would be better off trying:

*(unsigned short *)myaddress=somevalue;
--
"To burn always with this hard, gem-like flame, to maintain this
ecstasy, is success in life." -- Walter Pater


Yep说:
Yep said:

大家好,


我对指针有一些困惑并且隐含地指定要写入的内存大小




我会尝试解释我想要做什么好一点。


char * myaddress =(char *)0xB8000;
Hi Guys,

I have some confusion with pointers and implicitly specifying a size of
memory to write.

I will try to explain what I am trying to do a bit better.

char *myaddress=(char *) 0xB8000;



这是否试图在x86上涂鸦视频内存?如果是这样,请注意

你需要处于实际模式才能工作(更多关于

comp.os.msdos.programmer)。另外,我建议使用unsigned char * for

this!

Is this an attempt to scribble on video RAM on an x86? If so, be warned
that you''ll need to be in real mode for it to work (more about that in
comp.os.msdos.programmer). Also, I would recommend unsigned char * for
this!


现在,我可以这样说:

* myaddress = 3;


''3'的8位值将写在绝对地址0xB8000
now, I can say something like:

*myaddress=3;

And the 8 bit value of ''3'' will be written at absolute address 0xB8000



关闭。 3而不是''3''。假设我们正在使用8位

char,16位int和ASCII:3是位模式0000000000000011(所以

8 -bit的等价值是00000011),而''3''是位模式

00110011.正如你所看到的,这些并不完全相同!

Close. 3 rather than ''3''. Assuming for the moment that we''re using 8-bit
char, 16-bit int, and ASCII: 3 is the bit-pattern 0000000000000011 (so the
8-bit equivalent would be 00000011), whereas ''3'' is the bit pattern
00110011. As you can see, these are not quite the same!


如果我想明确地将16或32位值写入该地址怎么办?
What if I want to explicitly write a 16 or 32 bit value to that address?



这不是个好主意。这里是你如何做32位(假设很长

int有32位,这不一定是真的,但对于你的平台和/或
你的平台可能是真的无论如何我们知道多头有*至少* 32

位!):


无符号长* mylongaddress =(无符号长*)0xB8000;


(我假设您的分配技术与您选择的

MS-DOS内存模型中的广告一样。我通常会选择大内存模型和

满0xb8000000。)


* mylongaddress = 31415926; / *天堂知道这将是什么样的

DOS屏幕* /

或者你可以简单地这样做:


void arbitrary_dos_screen_scribble(void * vm,size_t len,int x,int y)

{

unsigned char * m = vm;

unsigned char * p =(unsigned char *)0xb8000; / *让'希望我们有一张

色卡* /

p + = y * 160; / *让'希望有80列* /

p + = x * 2; / *让我们希望每个文本字节有一个属性字节* /


而(len--)

{

* p ++ = * m ++;

}

}


然后你可以传递& mylong和sizeof mylong,或者& myshort和sizeof

myshort,或者你喜欢的任何东西。


最终警告:这是*所以*不是一个好主意。坏不好的想法,好吗?不好。

如果你坚持这样做,你就不会得到饼干。


尽管如此,如果你必须这样做,那就是单向的要做到这一点。


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

That is so not a good idea. Here''s how you do it for 32 bits (assuming long
int has 32 bits, which isn''t necessarily true but is probably true for
your platform and in any case we know that longs have *at least* 32
bits!):

unsigned long *mylongaddress = (unsigned long *) 0xB8000;

(I''m assuming your assignment technique works as advertised in your chosen
MS-DOS memory model. I would normally go for large memory model and the
full 0xb8000000.)

*mylongaddress = 31415926; /* heaven knows what this will look like on a
DOS screen */
Or you can simply do this:

void arbitrary_dos_screen_scribble(void *vm, size_t len, int x, int y)
{
unsigned char *m = vm;
unsigned char *p = (unsigned char *)0xb8000; /* let''s hope we have a
colour card */
p += y * 160; /* let''s hope there are 80 columns */
p += x * 2; /* let''s hope we have one attribute byte per text byte */

while(len--)
{
*p++ = *m++;
}
}

and then you can pass &mylong and sizeof mylong, or &myshort and sizeof
myshort, or whatever you like.

Final warning: this is *so* not a good idea. BAD BAD BAD IDEA, okay? Bad.
If you insist on doing this, you will NOT get a biscuit.

Nevertheless, if you must do it, that''s one way to do it.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


这篇关于帮助指针和绝对内存地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆