Author Topic: .FOR built in JWasm  (Read 57768 times)

Gunther

  • Member
  • *****
  • Posts: 3723
  • Forgive your enemies, but never forget their names
Re: .FOR built in JWasm
« Reply #45 on: January 10, 2013, 04:50:52 AM »
Good to know habran.  :t

Gunther
Get your facts first, and then you can distort them.

habran

  • Member
  • *****
  • Posts: 1226
    • uasm
Re: .FOR built in JWasm
« Reply #46 on: February 01, 2013, 06:35:52 AM »
I have modified code to work exactly as C

Cod-Father

frktons

  • Member
  • ***
  • Posts: 491
Re: .FOR built in JWasm
« Reply #47 on: February 01, 2013, 12:25:20 PM »
I have modified code to work exactly as C
Interesting stuff. JWasm is written in C, and now we can use JWasm as C in some
of its features. At least I hope the JWasm version is faster/smarter/smaller/...er than
C counterpart.   :t

habran

  • Member
  • *****
  • Posts: 1226
    • uasm
Re: .FOR built in JWasm
« Reply #48 on: February 01, 2013, 01:32:33 PM »
"Frankly" :biggrin: it's well optimized
I've wished someone gave me a feedback after testing it

thanks Frank
Cod-Father

jj2007

  • Member
  • *****
  • Posts: 11551
  • Assembler is fun ;-)
    • MasmBasic
Re: .FOR built in JWasm
« Reply #49 on: February 01, 2013, 06:21:13 PM »
 :icon_confused:
I've wished someone gave me a feedback after testing it

JWasm32 told me that MSVCR110.dll was missing (on Win7-32). After fixing this, here are some timings:
AMD Athlon(tm) Dual Core Processor 4450B (SSE3)

22317   cycles for 100 * C style for loop
31539   cycles for 100 * Basic style for loop

14      bytes for C style for loop
30      bytes for Basic style for loop


So your code is really competitive :t
Here are the two loops compared:

        m2m eax, 40
        .for (ecx = 0¦ecx<=eax¦ecx+=1)
                inc edx        ; inc eax hangs
        .endfor

        m2m eax, 41
        For_ ecx=0 To eax-1
                inc eax        ; inc eax OK
        Next

The Basic loop is a bit more flexible, as it can handle eax+whatever, and references to the initial eax (or whatever) while looping. On the other hand, the C loop is shorter and faster. In real life, if speed was an issue, I would use a .Repeat ... .Until construct anyway, but I guess for C coders your add-on to JWasm is really handy :t

P.S.: I made a little test with Pelles C:

      int n=100;
      __asm int 3;
      __asm nop;
         for (jjc=0; jjc<n; jjc++) {
         n++;
      }
      __asm nop;

00401DD5  |.  90            nop
00401DD6  |.  31C0          xor eax, eax
00401DD8  |.  BA 64000000   mov edx, 64
00401DDD  |>  42            /inc edx
00401DDE  |.  40            |inc eax
00401DDF  |.  39D0          |cmp eax, edx
00401DE1  |.^ 7C FA         \jl short 00401DDD
00401DE3  |.  90            nop


Heavily optimised (bravo, Pelle :t), and it seems indeed that C keeps referring to the current loop counter, i.e. this loop stops only for edx = 80000000h 8)

habran

  • Member
  • *****
  • Posts: 1226
    • uasm
Re: .FOR built in JWasm
« Reply #50 on: February 01, 2013, 10:32:10 PM »
thanks JJ for testing it
if you write it like this:
Code: [Select]
       
         .for (ecx = 0¦ecx<=40¦ecx++)
                inc eax       
        .endfor

you will get:
Code: [Select]
004C879  xor     ecx,ecx
004C87B  jmp    004C87Fh
004C87D  inc     ecx 
004C87F  cmp    ecx,28h
004C882  ja      004C888h
004C884  inc     eax 
004C886  jmp    004C87Dh

I have optimized it so that:
  ++ will be transformed to INC
  -- will be transformed to DEC
but:
 += is ADD
and:
 -= is SUB
« Last Edit: February 01, 2013, 11:34:24 PM by habran »
Cod-Father

frktons

  • Member
  • ***
  • Posts: 491
Re: .FOR built in JWasm
« Reply #51 on: February 01, 2013, 11:28:40 PM »
thanks JJ for testing it
if you write it like this:
Code: [Select]
       
         .for (ecx = 0¦ecx<=40¦ecx++)
                inc eax       
        .endfor

you will get:
Code: [Select]
004C879  xor     ecx,ecx
004C87B  jmp    004C87Fh
004C87D  inc     ecx 
004C87F  cmp    ecx,28h
004C882  ja      004C888h
004C884  inc     eax 
004C886  jmp    004C87Dh



Compliments habran, a really good job.   :eusa_clap:

habran

  • Member
  • *****
  • Posts: 1226
    • uasm
Re: .FOR built in JWasm
« Reply #52 on: February 01, 2013, 11:43:10 PM »
Thanks Frank :biggrin:

One who can see quality of others, posses quality himself
    who wrote that? :shock:
      habran himself  :lol:
Cod-Father

habran

  • Member
  • *****
  • Posts: 1226
    • uasm
Re: .FOR built in JWasm
« Reply #53 on: February 04, 2013, 03:41:52 PM »
I added Jwasm.zip with 32 bits version with /MT option

needs at least Windows version 6
(Japheth)

however, you have a source code and can build for your system if it is older
« Last Edit: February 05, 2013, 06:12:09 AM by habran »
Cod-Father

japheth

  • Guest
Re: .FOR built in JWasm
« Reply #54 on: February 04, 2013, 07:51:16 PM »
I added Jwasm.zip with 32 bits version with /MT option

It should be noted that the binary is marked as "needs at least Windows version 6" ( which is Vista ). With XP, you get a message box saying that jwasm.exe "is not a valid Win32 application".

jj2007

  • Member
  • *****
  • Posts: 11551
  • Assembler is fun ;-)
    • MasmBasic
Re: .FOR built in JWasm
« Reply #55 on: February 04, 2013, 08:44:53 PM »
 :redface:
thanks JJ for testing it
if you write it like this:
Code: [Select]
       
         .for (ecx = 0¦ecx<=40¦ecx++)
                inc eax       
        .endfor

Habran,

The immediate case ecx<=40 is trivial. More tricky is what happens when you pass the bounds in (volatile/non-volatile) registers:
        ; assume eax was set somewhere else, as a return value:
        .for (ecx = 0¦ecx<=eax¦ecx+=1)
                inc eax        ; hangs in C/your JWasm version, in contrast to BASIC/MasmBasic
        .endfor


The point is that ecx<=eax is not checked only at the beginning, but rather for every loop. Which implies that in practice you cannot use eax, ecx, edx for bounds if calls are inside the loops.

In MasmBasic it's possible, which is the reason why it is some cycles slower. Speed against convenience, as usual  :biggrin:

habran

  • Member
  • *****
  • Posts: 1226
    • uasm
Re: .FOR built in JWasm
« Reply #56 on: February 04, 2013, 10:04:30 PM »
thanks Japheth I was not aware of that
I don't have Xp nor Vista I have Windos 7 on all computers

jj2007 it is possible to write this:
Code: [Select]
LIMIT EQU 40
THE_BREAK EQU 20
.for (ecx = 0,eax=LIMIT,edx=THE_BREAK¦ecx<=eax¦ecx++)
                ;inc eax      it is illogical to increase a LIMIT especially with the same step
                                  ;of course it will hang
                .break .if (ecx > edx)
        .endfor

 
Cod-Father

jj2007

  • Member
  • *****
  • Posts: 11551
  • Assembler is fun ;-)
    • MasmBasic
Re: .FOR built in JWasm
« Reply #57 on: February 04, 2013, 11:56:10 PM »
                ;inc eax      it is illogical to increase a LIMIT especially with the same step

It depends on where you come from. For C coders, it may look illogical, but assembler coders might want to use return values directly...

include \masm32\MasmBasic\MasmBasic.inc        ; download
        Init
        GetFiles \Masm32\m32lib\*to*        ; list conversion to something algos please

        ; # of files found is returned in eax, as usual
        For_ ebx=0 To eax-1        ; it is illogical to forbid the use of eax inside the loop
                Print Str$(ebx), Tb$, Files$(ebx), CrLf$
        Next

        Inkey
        Exit
end start

And no, I don't want you to change your logic :biggrin:

japheth

  • Guest
Re: .FOR built in JWasm
« Reply #58 on: February 05, 2013, 12:49:58 AM »
The point is that ecx<=eax is not checked only at the beginning, but rather for every loop.

Yes, of course. Anything else would be VERY unintuitive ( at least for non-BASIC coders ).

Quote
In MasmBasic it's possible, which is the reason why it is some cycles slower. Speed against convenience, as usual

... another MB promotion thread ...  :icon_rolleyes:


jj2007

  • Member
  • *****
  • Posts: 11551
  • Assembler is fun ;-)
    • MasmBasic
Re: .FOR built in JWasm
« Reply #59 on: February 05, 2013, 01:39:37 AM »
Yes, of course. Anything else would be VERY unintuitive

japheth,

Take it easy. Assembler != C, and assuming some"logic" is a source of difficult to chase bugs. Remember the debate about Switch/Case and falling thru? VERY unintuitive what C does, at least for the rest of the programming world.