A variable define in .data? use without initialize(Add a new question)

Started by math_ai, September 19, 2011, 02:24:27 AM

Previous topic - Next topic

math_ai

Now I am learning win32 asm. :dazzled:

the below code is a simple program that demonstrate the timer in windows

the code:

.386
.model flat, stdcall
option casemap:none
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
include windows.inc
include user32.inc
includelib user32.lib
include kernel32.inc
includelib kernel32.lib
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ID_TIMER1 equ 1
ID_TIMER2 equ 2
ICO_1 equ 1
ICO_2 equ 2
DLG_MAIN equ 1
IDC_SETICON equ 100
IDC_COUNT equ 101

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.data?

hInstance dd ?
hWinMain dd ?
dwCount dd ?
idTimer dd ?
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

.code

_ProcTimer proc _hWnd,_uMsg,_idEvent,_dwTime

pushad
invoke GetDlgItemInt,hWinMain,IDC_COUNT,NULL,FALSE
inc eax
invoke SetDlgItemInt,hWinMain,IDC_COUNT,eax,FALSE
popad
ret

_ProcTimer endp

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_ProcDlgMain proc uses ebx edi esi,hWnd,uMsg,wParam,lParam

mov eax,uMsg
.if eax == WM_TIMER
mov eax,wParam
.if eax == ID_TIMER1
inc dwCount
mov eax,dwCount
and eax,1
inc eax
invoke LoadIcon,hInstance,eax
invoke SendDlgItemMessage,hWnd,IDC_SETICON,STM_SETIMAGE,IMAGE_ICON,eax
.elseif eax==ID_TIMER2
invoke MessageBeep,-1
.endif
.elseif eax==WM_INITDIALOG
push hWnd
pop hWinMain
invoke SetTimer,hWnd,ID_TIMER1,250,NULL
invoke SetTimer,hWnd,ID_TIMER2,2000,NULL
invoke SetTimer,NULL,NULL,1000,addr _ProcTimer
mov idTimer,eax
.elseif eax==WM_CLOSE
invoke KillTimer,hWnd,ID_TIMER1
invoke KillTimer,hWnd,ID_TIMER2
invoke KillTimer,NULL,idTimer
invoke EndDialog,hWnd,NULL
.else
mov eax,FALSE
ret
.endif
mov eax,TRUE
ret
_ProcDlgMain endp

start:
invoke GetModuleHandle,NULL
mov hInstance,eax
invoke DialogBoxParam,hInstance,DLG_MAIN,NULL,offset _ProcDlgMain,NULL
invoke ExitProcess,NULL
end start

and the rc file:
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#include <resource.h>
#include <winuser.h>
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#define DLG_MAIN 1
#define ICO_1 1
#define ICO_2 2
#define IDC_SETICON 100
#define IDC_COUNT 101
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ICO_1 ICON "1.ico"
ICO_2 ICON "2.ico"
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
DLG_MAIN DIALOG 50, 50, 113, 40
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "timer"
FONT 9 "Dotum"
{
ICON ICO_1, IDC_SETICON, 8, 9, 18, 21
LTEXT "count:", -1, 35, 16, 25, 10
LTEXT "", IDC_COUNT, 62, 16, 40, 10
}

and the 1.ico, and 2.ico is in the attach file.
my question is:
dwCount dd ?

does not assign any value to the bariable dwCount. but uses directly in the .code segment:
inc dwCount
mov eax,dwCount

why can use directly without initialize? and before inc dwCount, the value in dwCount is what?

hutch--

Simple answer is you can't. That is why you have both an initialised and uninitialised data section.

You could set the variable manually near the beginning of the source code and leave it in the .DATA? section.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

math_ai

 :dazzled:
But the unseasonable things is this program can be execute normally. I think if the variable "dwCount " don't initialize, then
inc dwCount
will have unforeseen value. so the program can't execute normally, but strangely it execute normally ,so I think maybe initialize implicity or something else?

dedndave

it is my understanding that the uninitialized data area is always filled with 0's when the program is loaded
however, i cannot recall where it is documented   :P

in 16-bit programs running under DOS, this was not true
in those cases, the uninitialized data area was filled with garbage

personally, i feel more comfortable either initializing it explicitly in code or placing it in the initialized data area


math_ai

 :bg
thanks very much

A new question:
when we define the subprogram "_ProcDlgMain",its parameters is "hWnd,uMsg,wParam,lParam"

but we  call this subprogram in :
invoke DialogBoxParam,hInstance,DLG_MAIN,NULL,offset _ProcDlgMain,NULL
we don't assign any value to its parameters , so when this subprogram use the parameters in the inner of its body , the value of its parameters is what?
are these values that OS assigned automaticly? :boohoo:

dedndave

there is a call to create the dialog - that is your INVOKE
then, there is a "callback" procedure that the OS calls whenever a message is sent to the window
that is the DlgProc procedure

http://msdn.microsoft.com/en-us/library/ms645469%28v=VS.85%29.aspx

this callback mechanism is the nature of all windows and controls

http://msdn.microsoft.com/en-us/library/ms633573%28VS.85%29.aspx