Im trying to load an array of 16 dwords with colors for use with the choose color dialog. Im loading them from an ini file. Heres the small snippet i use.

	xor	ebx, ebx

xor ecx, ecx
loadcolors:
invoke wsprintfA, addr Buffer, addr szForStr3, ecx
invoke GetPrivateProfileIntA, addr szAppTitle, addr Buffer, 0, addr szIniFile
mov CustomColors[ebx], eax
inc ecx
add ebx, 4
cmp ecx, 15
jne loadcolors


And custom colors is defined as...

CustomColors	dword		16 dup(?)


Could somone tell me why the line
mov	CustomColors[ebx], eax

creates an invalid page fault?

If i need to explain more, plese let me know.
Posted on 2001-08-10 21:02:18 by ChimpFace9000
Hi.
My guess is register preservation.
EBX is probably being used by Windows in one or both of the prior function calls, therefore its value is not 0 or 4 or 8, etc, when you get to the mov instruction. Either create a variable to hold that value or push/pop it to/from the stack.
Posted on 2001-08-10 22:17:30 by vReal
hi,
i think vReal mentioned the problem, but didn't solve it. i think ChimpFace9000 just forgot to put values like 0, 4, 8, 12, ... into EBX. I think he thought he could use arrays like in VB or something. But this is only possible if you have an array of bytes.
Correct me if i'm wrong.

NOP-erator
Posted on 2001-08-11 03:18:53 by NOP-erator
i forgot something to say:
you should always push ebx at the beginning of a proc and pop it at the end. or just use:



balbla proc uses ebx dbdf:DWORD
...
...
ret
balbla endp


if you use this, masm pushes and pops ebx for YOU.

NOP-erator
Posted on 2001-08-11 03:21:36 by NOP-erator
ChimpFace9000:
You are using ecx as counter:

xor ecx, ecx

loadcolors:

invokes...

inc ecx
cmp ecx, 15
jne loadcolors

The calls to Windows api functions ever change
ecx, eax and edx. So you can write so:

xor ecx, ecx

loadcolors:
push ecx
invokes...
pop ecx
inc ecx
cmp ecx, 15
jne loadcolors
Posted on 2001-08-11 05:09:00 by n u M I T_o r
Windows APIs never touch ebx esi edi so you can uses them as counters rather than the likes of ecx.

Just remember to do as NOP-erator says and push and pop them.
Posted on 2001-08-11 06:25:16 by Eóin
Your put your array in the .code section - which is READ-ONLY!!!
Posted on 2001-08-11 07:04:04 by gfalen
I would use ebx as counter AND index into the color table. Do



mov [CustomColors + ebx*4], eax


to get the proper indexing. As zadkiel and n u M I T_o r say, windows
api has some "register rules". ebx+esi+edi are always preserved,
while eax+ecx+edx are usually trashed. If you only use eax+ecx+edx
you will not need register preservation (unless the rest of your code
depends on those regs not being trashed, of course ;). So the best
is (probably) to use eax/ecx/edx for general use, and ebx/esi/edi
when you need register preservation across api calls (but DO remember
to save the ebx/esi/edi then, at least inside callbacks (wndproc etc))
Posted on 2001-08-11 07:12:05 by f0dder
There is a piece of folklore here that needs to be buried, 32 bit windows only has ONE register preservation convention, if a procedure is going to use any of EBX, ESI or EDI, it must preserve them. It does not matter if its a window callback or not, it only matters IF the procedure uses any of those 3 registers.

The purpose of the convention is so that all procedures, API calls included, have a predictable way of relating to each other. You can assume that all API calls preserve EBX, ESI & EDI so when you call an API function, the only thing you must be aware about is that it can freely modify EAX, ECX and EDX so if you have values in these 3 registers, you will often need to preserve them before calling an API function. If you don't, you can end up with unexpected crashes that come from a register being changed in an external function call.

Regards,

hutch@pbq.com.au
Posted on 2001-08-11 07:24:08 by hutch--
"... 32 bit windows only has ONE register preservation convention, if a procedure is going to use any of EBX, ESI or EDI, it must preserve them" - good point Hutch. Which is why I have redefined the word CALLBACK on my system to the following:

CALLBACK equ <proc uses esi edi ebx>

Now I can say,

someproc CALLBACK, varlist...

instead of

someproc proc uses esi edi ebx, varlist...

you might also want to do this...

WINDPROC equ <CALLBACK, hWin, uMsg, wParam, lParam>

WndProc WINDPROC
...
WndProc endp


G. Falen
Posted on 2001-08-11 07:51:17 by gfalen
Thanks everyone.

1. The custom colors are defined in the .data? section.

2. I eventualy used the mov cutsomcolors, eax way.

3. I never knew you had to presrve esi, edi and ebx, thanks.

4. If i use "uses esi edi ebx", masm will push and pop them for me right. But how does it know when to do it. The proc exits at so many places. Does it just put the pops after every ret?
Posted on 2001-08-11 11:41:18 by ChimpFace9000
no, if you use "uses esi edi ebx" masm automatically puts all the pushes at the beginning at the proc and all the pops at the end (before ret). you can see that if you use a debugger.

cya,
NOP-erator
Posted on 2001-08-11 14:05:18 by NOP-erator