Hello everybody,

I have always been under the impression that the directive .CONST started a read-only data segment. Hence, any values used to initialize variables in the .CONST segment would not be changed inadvertently. This apparently isn't true. In the following snippet edi has been left pointing at a variable which has been defined with the .CONST directive.

;
;
;
.CONST

szSecureData db "Secure".0
;
;
;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
OneProc proc uses edi
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;

mov edi, offset szSecureData
invoke OtherProc

ret

OneProc endp

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
OtherProc proc uses edi
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

invoke ReadFile,hFile,edi,2h,ADDR WriteSize,NULL
invoke CloseHandle,hFile

;this reads the first two bytes of the file defined by hFile
;and writes them to the szSecureData variable.

OtherProc endp

ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

strange, is it not? or what is it I don't understand about .CONST

btw, I'm actually guilty of that sloppy programming, i.e leaving edi pointing where it shouldn't be pointing:)

best regards,

czDrillard
Posted on 2003-03-21 11:01:28 by czDrillard
You said it, const is not a segment, it's a directive. If you want to use data in a read only section, do it like that :





.code
MyConst dd 12346
Posted on 2003-03-21 12:00:11 by Axial
Hi czDrillard,

behaviour possibly depends on OS. Which OS did you use for your test? Win9x, I guess.
Posted on 2003-03-21 12:11:09 by japheth

You said it, const is not a segment, it's a directive. If you want to use data in a read only section, do it like that :





.code
MyConst dd 12346



He did it like that actually.
u means

.CONST
MyConst equ 12346
??????????????????

i didn't think that u can make some data of ur prog unwritable by himself
Posted on 2003-03-21 17:04:33 by AceEmbler




He did it like that actually.
u means

.CONST
MyConst equ 12346
??????????????????

i didn't think that u can make some data of ur prog unwritable by himself

Of course you can.
Specify it after LINK key SECTION
Posted on 2003-03-21 17:30:44 by The Svin
i can. Good to know. Btw but what for, isn't imm faster than reading mem ? I see it usefull only while i'm gona release some patch. It will take less space :P
Posted on 2003-03-21 17:46:50 by AceEmbler
.const is defined as section .rdata when linked with MS linker, and works fine here. Makes program crash when attempting to write to a symbol defined in .const section (symbol as in defined with db/dw/dd etc, not a preprocessor constant defined with equ) on Windows 2000. Not sure what is wrong with your OS.
Posted on 2003-03-21 17:47:38 by comrade

i can. Good to know. Btw but what for, isn't imm faster than reading mem ? I see it usefull only while i'm gona release some patch. It will take less space :P


If it is a numeric constant, yes, you are right about the speed. Regarding the space requirement, it depends on the alignment. (I mean, it might add another several bytes to the total size depending on the alignment setting.) But only if you care. Ironically, many optimization enthusiasts don't seem to realize the importance of the alignment (both code and data).

If it is a string constant (just like OP wanted to do), no immediate value can be used, by the very nature of the string. As for another example, your WNDCLASS is likely to be constant for the duration of your application. But there is no way I can think of that I populate WNDCLASS and pass as an immediate value to RegisterClass().
Posted on 2003-03-21 19:05:00 by Starless
Thanks to everybody for the info:alright:

btw, I use Win98

best regards,

czDrillard
Posted on 2003-03-22 19:36:12 by czDrillard