News:

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

endless loop

Started by thomas_remkus, April 28, 2006, 05:26:50 PM

Previous topic - Next topic

thomas_remkus

for some reason, i seem to get an endless loop with some code that looks fine to me. I'm attempting to understand some basic jumps and conditions. I thought a pointless loop might be a fair start. The next thing that I wanted to do was to have "jj" be increased only every third loop and it would be increased by "ii". The way I'm going about this I think I'll have to reuse eax for variables in and out.

TOP: C++
MIDDLE: I think this loops because it never returns
BOTTOM: this is what I am using now

long ii = 1;
long jj = 0;

for(ii = 1; ii < 999999999; ++ii)
{
    ++jj;
}



long ii = 1;
long jj = 0;

__asm
{
lea eax, ii
cmp eax, 999999999
jge jumpdone
jmp jumpincrease
jumpcondition:
lea eax, ii
cmp eax, 999999999
jge jumpdone
jumpincrease:
lea eax, ii
inc eax
mov ii, eax
mov jj, eax
jmp jumpcondition
jumpdone:
}


long ii = 1;
long jj = 0;

__asm
{
lea eax, ii
cmp eax, 999999999
jge jumpdone
jmp jumpincrease
jumpcondition:
cmp eax, 999999999
jge jumpfinish
jumpincrease:
inc eax
jmp jumpcondition
jumpfinish:
dec eax
mov jj, eax
jumpdone:
}

MaynardG_Krebs

lea eax, ii
         cmp eax, 999999999
         jge jumpdone
         jmp jumpincrease


I am pretty new to assembly so I may be totally wrong, but it looks as if in this first series of statements there is nothing to change the value of ii so control never goes past the cmp eax, 999999999 statement.

thomas_remkus

i sort of agree, but i don't see where the logic flaw is located. what i want to do is to have the jj variable increased by 1 during each iteration of the loop. i think that i have the right logic but there must be something that's either no increasing the value or that it's just something else.

i honestly have no idea and the middle code (from my stupid understanding) should just run. this is really not funny because i was feeling pretty good about this until it just locks up.

btw: i can finally see, for the first time, the performance increase. the __asm runs so much faster than the c++ it's amazing. I thought that they would be much closer in processing but they just arn't.

A little more help with this from someone would be really nice!

arafel

thomas,

lea eax, ii

will load the address of variable, not it's content.

do either:
mov eax, ii
or
lea eax, ii
cmp [eax], 9999999



as for: "The next thing that I wanted to do was to have "jj" be increased only every third loop and it would be increased by "ii""
something like this should do

               

        mov        eax, ii
        mov        ecx, 3
l1:     dec        ecx
        jnz        l2
        add        jj, eax
        mov        ecx, 3
l2:    inc        eax
       cmp        eax, 99999999
       jb        l1

MaynardG_Krebs

Quote
btw: i can finally see, for the first time, the performance increase. the __asm runs so much faster than the c++ it's amazing. I thought that they would be much closer in processing but they just arn't.

Yeah, nice isn't it. Not only performance but size also. Just out of curiosity, I wrote a simple "Hello World " program in C++ and also in assembly. The C++ file came in at under 500kb and the assembler file was around 76kb and I'm sure could have been made even smaller by an experienced assembly programmer.

Mark Jones

76kB? Oh my goodness, why so big? :eek
Maybe a bitmap resource was included or something. :toothy

Try this for a "hello world" program:
http://www.masmforum.com/simple/index.php?topic=1202.0
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

thomas_remkus

OH NUTS!!!!!!!! That means that all of the code I have is just trash. I thought I had this working but it seems that somehow I was just faking myself out. I'm going to need to rework my code.

Honestly, THANKS!! I have been reading the ASPINTRO.HLP and was really confused because my inline __asm seemed to be working fine but something was just wrong. Back to my trusty IDE ... notepad ... and trying to cook up a fair example of what I was trying to do. I'll post here my results and have people correct me.

I want the value of "ii" but I can't seem to use mov with "ii" because the scope is inside my function. I'll work on this.

thomas_remkus

OK, now that this is working:

long ii = 1;
long jj = 0;

__asm
{
mov ecx, ii
cmp ecx, 999999999
jge jumpdone
jmp jumpwork
jumpcondition:
cmp ecx, 999999999
jge jumpdone
jumpwork:
mov ebx, jj
add ebx, 1
mov jj, ebx
;jumpincrease
add ecx, 1
jmp jumpcondition
jumpdone:
}


I just can't see that much improvement over my C++ code. In fact, I see that because I am limited to the manner in which I have only about 6 registers to play with as variables I really run out of variables in registers really fast. In fact I'm about 14% faster only. I'm not sure if this is because I'm using Visual Studio 2005 to compile and I really should not use the __asm at all.

Attached is my code. If someone wants to comment on it they can. I really wanted to get an accurate appraisal of assembly code compared to C++ code. This might not be the most accurate but it's what I'm working with. If done in MASM would this perform faster if on the same machine?

Also, not only did I find making my silly __asm code faster than C++ very difficult ... I ran into dozens of attempts where my code was much slower than the C++ output. The output exe from the C++ is so large I thought that for sure just by the very convention of writting this in ASM I would be so much faster.

Needing some direction (feeling a little discouraged),
thomas

[attachment deleted by admin]

Superhai

#8
I used my c++ compiler to generate your cpp code into asm, and cleaned it up. Actually that is one way to learn asm, I am using that approach.


TITLE C:\Program Files\Microsoft C Compiler\loop.cpp
.686P
.XMM
.model flat

_BSS SEGMENT
jj DD 01H DUP (?)
_BSS ENDS

_DATA SEGMENT
ii DD 01H
_DATA ENDS

_TEXT SEGMENT

_main PROC

mov DWORD PTR ii, 1
jmp SHORT ln3
ln2:
mov eax, DWORD PTR ii
add eax, 1
mov DWORD PTR ii, eax
ln3:
cmp DWORD PTR ii, 999999999
jge SHORT ln4

mov ecx, DWORD PTR jj
add ecx, 1
mov DWORD PTR jj, ecx


jmp SHORT ln2
ln4:

ret 0
_main ENDP
_TEXT ENDS
END _main


So when you have the result so you challenge yourself to optimize the code...


PBrennick

Thomas,
QuoteNeeding some direction (feeling a little discouraged)

I think you are doing well and documenting it all on line in this manner is sure to help others in the same situation.  Trying to squeeze out more and more cycles can be frustrating at times but it is always rewarding and when you look back, it will be with a good feeling.  In-line ASM definitely has its uses as it allows you to optimize certain sections only (where speed counts) and the front end stuff can be done in C++.  I really can't find any fault in your model though you seem to get lost, at times, with the content.  I am enjoying the endless thread about the endless loop.  :U

Paul
The GeneSys Project is available from:
The Repository or My crappy website

MaynardG_Krebs

[quote ]
76kB? Oh my goodness, why so big? :eek
Maybe a bitmap resource was included or something. :toothy

Try this for a "hello world" program:
http://www.masmforum.com/simple/index.php?topic=1202.0
Quote

:red

Mark Jones

Binaries coded using C generally are much larger than those created by MASM32 (especially if a non-optimizing C compiler is used.) C does many things without your control or knowlege, like setting up exception handlers, adding run-time code, etc. But a MASM32 "hello world" application should be very tiny because all that stuff is eliminated. If a MASM32 "hello world" application is really that big, something must somehow be included to make it that big. :bg

See \masm32\icztutes\tute02 for a 3kB program which displays a MessageBox. :U
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

hutch--

Try the 1k window in the masm32 example code, you don't have to write bloated junk.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

MaynardG_Krebs

Thanks Mark and Hutch I'll give them a lookover.
I'm learning.