flat assembler
Message board for the users of flat assembler.

Index > Main > How do faster two Cmp?

Author
Thread Post new topic Reply to topic
Roman



Joined: 21 Apr 2012
Posts: 1815
Roman 08 Jan 2024, 09:41
Fasmw 1.73
How do this in three commands?
Code:
Cmp Al, "0"
Jb @f
Cmp Al, "9"
Ja @f
Do some code
@@:

    
Post 08 Jan 2024, 09:41
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20363
Location: In your JS exploiting you and your system
revolution 08 Jan 2024, 10:10
Code:
sub al,'0'
cmp al,9
ja .out_of_range
; do my thing
.out_of_range:    
Post 08 Jan 2024, 10:10
View user's profile Send private message Visit poster's website Reply with quote
MatQuasar



Joined: 25 Oct 2023
Posts: 105
MatQuasar 08 Jan 2024, 10:33
Try:

Code:
  mov    eax, "1"
  xor    al, 0x30
  cmp    al, 9
  ja     quit       
Post 08 Jan 2024, 10:33
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1815
Roman 08 Jan 2024, 10:39
Thanks.
Both variants work fine.
I tested.
Post 08 Jan 2024, 10:39
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20363
Location: In your JS exploiting you and your system
revolution 08 Jan 2024, 11:02
It is a generic range inclusion test.
Code:
range_begin     =  '0'
range_end       =  '9'

        sub     al,range_begin
        cmp     al,range_end - range_begin
        ja      .out_of_range
        ; do the thing
    .out_of_range:    
And it also works for negative ranges (an exclusion test) that wraps around from the beginning to the end
Code:
range_begin     =  '9'
range_end       =  '0'

        sub     al,range_begin
        cmp     al,range_end - range_begin
        ja      .out_of_range
        ; do the thing
    .out_of_range:    
Post 08 Jan 2024, 11:02
View user's profile Send private message Visit poster's website Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1391
Location: Piraeus, Greece
Picnic 09 Jan 2024, 12:10
How about:
Code:

    lea ecx, [eax-'0']
    cmp ecx, 10
    jc _isdigit
    

_________________
Hobby BASIC Interpreter
Post 09 Jan 2024, 12:10
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: 20363
Location: In your JS exploiting you and your system
revolution 09 Jan 2024, 14:31
Using the full eax value might be a problem if the top 24 bits are not well defined. But you can change it to this:
Code:
    lea ecx, [eax-'0']
    cmp cl, 10
    jc _isdigit    
Post 09 Jan 2024, 14:31
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1815
Roman 09 Jan 2024, 14:57
lea ecx, [eax-'0'] not faster than sub AL,"0"
And sub AL,"0" less bytes

lea ecx, [eax-'0'] 3 bytes

sub AL,"0" 2 bytes
Post 09 Jan 2024, 14:57
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.