flat assembler
Message board for the users of flat assembler.

Index > Main > Fasm 1.73 Many cmp one jz.

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
Roman



Joined: 21 Apr 2012
Posts: 815
Roman
Some time need do many compares. How do many cmps and one jz ?
Code:
    mov    eax,4
    cmp    eax,5
    cmp    eax,3
    cmp    eax,11
    cmp    eax,13
    cmp    eax,23
    cmp    eax,4    ;z=1
    cmp    eax,1    ;reset z = 0 !
    jz        @f
    Msg 'ok'            ;= invoke MessageBox,0,'ok',0,0
@@:          
    
Post 10 Mar 2020, 05:08
View user's profile Send private message Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 575
Location: Russian Federation, Sochi
ProMiNick
cmp eax,23
ja notinTbl
jmp [eax*4+JmpTbl]
JmpTbl:
dd case0
dd case1
dd case2
dd case3
dd case4
dd case5
dd case6
dd case7
dd case8
dd case9
dd case10
dd case11
dd case12
dd case13
dd case14
dd case15
dd case16
dd case17
dd case18
dd case19
dd case20
dd case21
dd case22
dd case23
case1: case3: case4: case5: case11: case13: case23: Msg 'OK'
notinTbl:
other cases somewhere else (they equal notinTbl forexample)
Post 10 Mar 2020, 05:53
View user's profile Send private message Send e-mail Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 815
Roman
Thanks.
I thinked exist simple asm command for my case.
Many times I see that there are not enough many useful commands in modern CPU.
If modern CPU have many simples and powerfuls asm commands. Many peoples writes program on fasm.
And not used c++.

My opinion Intel not good implemented asm commands for easy write code.
This led to the emergence of c ++


Intel should be ashamed for this Smile
Post 10 Mar 2020, 06:23
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17663
Location: In your JS exploiting you and your system
revolution
Roman wrote:
Thanks.
I thinked exist simple asm command for my case.
Many times I see that there are not enough many useful commands in modern CPU.
If modern CPU have many simples and powerfuls asm commands. Many peoples writes program on fasm.
And not used c++.

My opinion Intel not good implemented asm commands for easy write code.
This led to the emergence of c ++


Intel should be ashamed for this Smile
There certainly some things to shame Intel for, but this isn't one of them IMO. If you think x86 assembly is bad then ALL of the others are also bad, and many are worse, in this respect. I guess some things just don't make sense to do at the low level. The cost/benefit ratio probably doesn't work out well.
Post 10 Mar 2020, 06:41
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7796
Location: Kraków, Poland
Tomasz Grysztar
If this is a simple yes/no check with multiple numbers for each case, you could save space by using a bit field and BT instruction:
Code:
        mov     eax,4
        cmp     eax,32
        jae     ok
        bt      [bits],eax
        jc      @f
     ok:
        invoke MessageBox,0,'ok',0,0
     @@:    
Code:
bits:
dd   1 shl 3 \
  or 1 shl 4 \
  or 1 shl 5 \
  or 1 shl 11 \
  or 1 shl 13 \
  or 1 shl 23    
Note that BT does not need to be limited to 32 cases, you can extend the field if needed:
Code:
        mov     eax,111
        cmp     eax,256
        jae     ok
        bt      [bits],eax
        jc      @f
     ok:
        invoke MessageBox,0,'ok',0,0
     @@:    
Code:
bits:
emit 256/8:   1 shl 3 \
           or 1 shl 4 \
           or 1 shl 5 \
           or 1 shl 11 \
           or 1 shl 13 \
           or 1 shl 23 \
           or 1 shl 111    
(Here I used fasmg syntax.)
Post 10 Mar 2020, 07:24
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17663
Location: In your JS exploiting you and your system
revolution
Roman wrote:
Some time need do many compares. How do many cmps and one jz ?
Code:
    mov    eax,4
    cmp    eax,5
    cmp    eax,3
    cmp    eax,11
    cmp    eax,13
    cmp    eax,23
    cmp    eax,4    ;z=1
    cmp    eax,1    ;reset z = 0 !
    jz        @f
    Msg 'ok'            ;= invoke MessageBox,0,'ok',0,0
@@:          
    
In ARM32 code you can use the bitmap trick Tomasz suggested, or make use of the conditional CMP:
Code:
        mov     r0,4
        cmp     r0,5
        cmpne   r0,3
        cmpne   r0,11
        cmpne   r0,13
        cmpne   r0,23
        cmpne   r0,4
        cmpne   r0,1
        beq      @f
        Msg     'ok'
@@:    
The first CMP to match the value will disable the remaining CMPs
Post 10 Mar 2020, 12:35
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 815
Roman
Its good example how arm architecture more thoughtful.
Why Intel do not develop new asm commands and do not speed up x86 CPU ?

Efective asm commands give less code and increase speed.
Post 10 Mar 2020, 15:07
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17663
Location: In your JS exploiting you and your system
revolution
Roman wrote:
Efective asm commands give less code and increase speed.
ARM is targeted to a different market segment so you can't really compare. But in general the performance of ARM is lower than x86. So perhaps all that extra circuitry to support CMPcc is too much of a burden and holds it back?
Post 10 Mar 2020, 16:15
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 815
Roman
Quote:
So perhaps all that extra circuitry to support CMPcc is too much of a burden and holds it back?

This question should be asked to engineers , who develops modern CPU.
And how do this more efective.
Post 10 Mar 2020, 17:11
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 815
Roman
Good example inverse matrix 4x4 ! Nice have one sse asm command.
Inverse matrix 4x4 174 asm sse commands !

It's hard to write 174 asm commands and do not make mystake !


Or one sse command two matrix4x4 multiply. Or cross product.
Quaternion very handfull in math and physics.

But Intel not do this in new AVX instructions.


I think this is probably one of the many reasons why Intel it loses to AMD Ryzen.
No new efective instruction and does not develop new CPU architectures.
Post 10 Mar 2020, 17:37
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 821
Location: Belarus
DimonSoft
Roman wrote:
Its good example how arm architecture more thoughtful.
Why Intel do not develop new asm commands and do not speed up x86 CPU ?

Efective asm commands give less code and increase speed.

You would cry seeing how certain easy things (like, say, loading a 32-bit immediate) are done in ARM.
Post 11 Mar 2020, 07:00
View user's profile Send private message Visit poster's website Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 821
Location: Belarus
DimonSoft
Roman wrote:
Good example inverse matrix 4x4 ! Nice have one sse asm command.
Inverse matrix 4x4 174 asm sse commands !

It's hard to write 174 asm commands and do not make mystake !


Or one sse command two matrix4x4 multiply. Or cross product.
Quaternion very handfull in math and physics.

But Intel not do this in new AVX instructions.


I think this is probably one of the many reasons why Intel it loses to AMD Ryzen.
No new efective instruction and does not develop new CPU architectures.

What would the inverse matrix instruction do with a zero-determinant matrix?

Does AMD Ryzen have such instructions or you’re just trying to use your personal preferences to explain something unrelated? Think about it.
Post 11 Mar 2020, 07:06
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17663
Location: In your JS exploiting you and your system
revolution
A "good" example where ARM vs x86 and ARM loses is getting the parity of a number. x86 has a handy flag giving you the parity, ARM has no equivalent, you have to do it longhand.

For most applications this makes no difference, since very few care about the parity. But if your app does make heavy use of parity then you will love x86 and hate ARM.
Post 11 Mar 2020, 07:27
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7796
Location: Kraków, Poland
Tomasz Grysztar
revolution wrote:
A "good" example where ARM vs x86 and ARM loses is getting the parity of a number. x86 has a handy flag giving you the parity, ARM has no equivalent, you have to do it longhand.
I feel that this phrasing might be a bit misleading, because "parity of a number" usually means whether the number is even or odd, while x86 parity flag is something different (it is the parity of the sum of 8 bits).
Post 11 Mar 2020, 08:12
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17663
Location: In your JS exploiting you and your system
revolution
Thanks. I meant the sum of all bits modulo 2. Comms and protocol documents tend to use this definition of parity.

Indeed, the parity in the mathematical sense would simply be the LSb, which is trivial to get in both x86 and ARM.
Post 11 Mar 2020, 09:01
View user's profile Send private message Visit poster's website Reply with quote
guignol



Joined: 06 Dec 2008
Posts: 727
guignol
Post 18 Apr 2020, 11:47
View user's profile Send private message Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 202
Overclick
Use this macro
Code:
macro cmp arg1, [arg2] {
     common
     counter = 0
     forward
     counter = counter + 1
     common
     if counter > 1
         push           eax
         xor            eax,eax
     forward
         cmp            arg1,arg2
         lahf
         or             al,ah
     common
         mov            ah,al
         sahf
         pop            eax
     else
         cmp            arg1,arg2
     end if
}

Examples:
     cmp      eax,13           ;compiled as usual nothing extra in code
     je       @F
     cmp      eax,13,11,56,44  ;compiled as flags stacker
     je       @F
    
Post 23 Jul 2020, 07:57
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17663
Location: In your JS exploiting you and your system
revolution
Overclick wrote:
Use this macro ...
You need to take care that arg1 and arg2 are not rax, eax, ax, ah, or al.

So your example "cmp eax,13,11,56,44" will likely fail.
Post 23 Jul 2020, 10:24
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 202
Overclick
Fixed Cool

For 32 bit version it needs to change registers to eax esp
Code:
macro cmp arg1, [arg2] {
     counter = 0
     forward
     counter = counter + 1
     common
     if counter > 1
         push           rax
         xor            rax,rax
         push           rax
     forward
         mov            rax,[rsp+8]
         cmp            arg1,arg2
         lahf
         or             [rsp],ah
     common
         pop            rax
         mov            ah,al
         sahf
         pop            rax
     else
         cmp            arg1,arg2
     end if
}  
    


Even better if eax not in use

Code:
macro cmp arg1, [arg2] {
     counter = 0
     forward
     counter = counter + 1
     common
     if counter > 1
         push           rax
         xor            rax,rax
         push           rax
      forward
        if `arg1 in <'rax','eax','ax','ah','al'> | `arg2 in <'rax','eax','ax','ah','al'>
         mov            rax,[rsp+8]
        end if
         cmp            arg1,arg2
         lahf
         or             [rsp],ah
      common
         pop            rax
         mov            ah,al
         sahf
         pop            rax
     else
         cmp            arg1,arg2
     end if
}    
Post 23 Jul 2020, 14:14
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 3043
Location: vpcmipstrm
bitRAKE
<OFF TOPIC> fasmg
Code:
macro compare reg0,N&
        iterate n,N
                cmp reg0,n
                if %% > 1
                        pushfq
                        if % = %%
                                repeat %% - 1
                                        pop reg0
                                        or [rsp],reg0
                                end repeat
                                popfq
                        end if
                end if
        end iterate
end macro

compare r15, 0xFF0000,0xFF00,0xFF    
only trashes the destination register and flags. extra IF/ENDIF to cover single value use. defining variant names for non-64bit registers would allow macro to be used more generally.

for byte/word size set tests the SIMD string instructions are very effective: PCMP?STR?. the implicit memory forms are not that slow - requires SSE4.2 though.

_________________
¯\(°_o)/¯ unlicense.org
Post 23 Jul 2020, 15:44
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.