News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

please advice, dword [i]

Started by bawrobin, May 08, 2010, 09:36:13 AM

Previous topic - Next topic

bawrobin

Hello. I am reading book and I have cycles chapter.
Here is example:


.686p

.model flat, stdcall

option casemap:none

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
include \masm32\include\windows.inc

.data
MsgText     db 'Calculating results is %d',0
MsgTitle    db 'ebx and eax are equal',0
buffer      db 100 dup(?)



.code

start:

for_start:
mov dword [i], 0

for_loop:
inc dword [i]
cmp dword [i], 10
jnz for_loop

for_finish:

mov ecx, dword [i]

    invoke  wsprintf,ADDR buffer,ADDR MsgText,ecx,1
    invoke  MessageBox,0,ADDR buffer,ADDR MsgTitle,MB_ICONINFORMATION
    invoke  ExitProcess,0


end start


It doesn`t work.
When I replace dword with register eax everything is ok.
May be it is not needed to write dword ?

I am using masm32 editor.

Neil

You should define any variables in the data or .data section i.e.  i dd 0 or i dd ?.
dword & brackets are not required & as you have found out it's much better to use a register.
Your code can be improved as someone else will probably point out.

Slugsnack

mov eax, 10

  @@:

dec eax
test eax, eax
jnz @b


or if your loop invariant is actually used for some purpose within the loop :
xor eax, eax

  @@:

inc eax
cmp eax, 10
jnz @b

Ossa

Quote from: Slugsnack on May 08, 2010, 01:02:35 PM
mov eax, 10

  @@:

dec eax
test eax, eax
jnz @b


you don't need the test eax, eax. dec eax actually sets the ZF according to the result, so:

mov eax, 10
@@:

dec eax
jnz @b


will work fine. Also, on newer processors, I believe it is recommended to use sub eax, 1 instead of dec.

Ossa
Website (very old): ossa.the-wot.co.uk

bawrobin


dedndave

inc dword [i]
might work better as
inc dword ptr [i]