flat assembler
Message board for the users of flat assembler.

Index > Main > Little CMP Logic help.

Author
Thread Post new topic Reply to topic
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
Hello everyone. I'm thinking how to compare values between 2 values. For example, C code:
Code:
if(int a>=0 && int a<=10) {
do something
}    

How to do that in assembly without using macros (if possible) ? Thank you.


Last edited by Overflowz on 30 Jan 2011, 11:55; edited 2 times in total
Post 30 Jan 2011, 11:05
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
I've tried 1 logic but I'll be thankful if some of you will write ur own.. Smile Here's mine what I though.
Code:
xor eax,eax
cmp eax,0
jae .stage1
.stage1:
cmp eax,10
jbe .stage2
ret
.stage2:
do something    

Equal C Code.
Code:
if(int a >=0 && int a<= 10){
do something
}    
Post 30 Jan 2011, 11:54
View user's profile Send private message Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel
Code:
mov  eax,5

cmp        eax,10
jae   exit
test    eax,eax
jbe  exit
; do something
exit:
    


~~~~

Overflowz wrote:

Code:
[...]
jae .stage1
.stage1:
[...]
    


So it's running stage1 either way?
Post 30 Jan 2011, 12:09
View user's profile Send private message Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
For signed a:
Code:
cmp eax,0
jl @f

cmp eax,10
jg @f

; do something

@@:    
But as you're testing for 0 <= a <= 10 you can cheat and use unsigned, as anything less than 0 will be greater than 10 in unsigned comparison:
Code:
cmp eax,10
ja @f

; do something

@@:    
Post 30 Jan 2011, 12:15
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
ManOfSteel
Nice one, thank you.

Yes stage1 will run anyway and if you don't want it then just retn insturcion there. I'll do something later about that.. Smile

cod3b453
Nice trick on second example but I need numbers between values :p
Post 30 Jan 2011, 14:00
View user's profile Send private message Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler
Overflowz wrote:

cod3b453
Nice trick on second example but I need numbers between values :p


Isn't between 0 and 10 numbers between values?

value1 <= eax <= value2

Code:
                sub     eax,value1
                cmp     eax,(value2-value1)
                jbe     .ok            

_________________
This is a block of text that can be added to posts you make.
Post 30 Jan 2011, 14:54
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
mindcooler
Yes it is but look what I mean:
from 0 to 10 = do task 1
from 10 to 20 = do task 2
and so on..
Post 30 Jan 2011, 15:21
View user's profile Send private message Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler
Just build on my example:

value1 <= eax <= value2

value2 <= eax <= value3

Code:
                sub     eax,value1
                cmp     eax,(value2-value1)
                jbe     .ok1

                sub     eax,(value2-value1)
                cmp     eax,(value3-value2)
                jbe     .ok2    


so:

0 <= eax <= 10

10 <(=) eax <= 20

Code:
                ;sub     eax,0
                cmp     eax,10
                jbe     .ok1

                sub     eax,10
                cmp     eax,10
                jbe     .ok2    


This could be simplified greatly by a macro.

_________________
This is a block of text that can be added to posts you make.
Post 30 Jan 2011, 15:52
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
mindcooler
Can you post with using macros now ? Smile Thanks.
Post 30 Jan 2011, 17:37
View user's profile Send private message Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler
I don't have any macro for pascal style case switches, perhaps some macro buff has?
Post 30 Jan 2011, 17:44
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4237
Location: 2018
edfed
post summary:
lowbound<eax<highbound == (eax-lowbound)<(highbound-lowbound)
due to the signed binary representation of integers.
Post 30 Jan 2011, 18:00
View user's profile Send private message Visit poster's website Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler
Perhaps something like this:

Code:
macro switch reg,[a,b,label]
{
   common
      base=0
      lastbase=0

   forward
      sub reg,(a-base)
      cmp reg,b-a
      jbe label
      base=base+a-lastbase
      lastbase=a
}

start:
                mov     eax,25
                switch  eax,0,3,.1,6,15,.2,22,25,.3
 .default:
                mov     edx,22
                jmp     .out
 .1:
                mov     edx,7
                jmp     .out
 .2:
                mov     edx,11
                jmp     .out
 .3:
                mov     edx,14
 .out:                  

_________________
This is a block of text that can be added to posts you make.
Post 30 Jan 2011, 21:14
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
b1528932



Joined: 21 May 2010
Posts: 287
b1528932
bound instruction?
Post 30 Jan 2011, 21:30
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1288
Location: behind the arc
Picnic
Hi, you might find useful this macro.
Code:
macro JumpIf reg, uplimit, lowlimit, lbl {

        lea ecx, [reg-1-uplimit]
        lea edx, [reg-lowlimit]
        xor ecx, edx
        js lbl
        }
    
Code:
     mov eax, 10
     JumpIf eax, 10, 0, someLabel
    
Post 30 Jan 2011, 23:34
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17259
Location: In your JS exploiting you and your system
revolution
b1528932 wrote:
bound instruction?
Have you ever used it? I've never seen it used. It ain't so simple to use, especially in a PM environment.
Post 31 Jan 2011, 00:18
View user's profile Send private message Visit poster's website Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
You can expand what I said to multiple ranges:
Code:
cmp eax,10
jae @f
; do something 0-9
jmp .done

@@:
cmp eax,20
jae @f
; do something 10-19
jmp .done

@@:
; ...
.done:    
But if there are a lot of these range checks the code is slower/larger/messier for higher values so you might want to use a lookup table instead (not tested):
Code:
mov ebx,0x1999999A ; ebx = round(2^32 / 10)
xor edx,edx
mul ebx ; reciprocal multply eax so that edx = eax / 10

mov eax,dword [4*edx+lut] ; find the label in LUT
jmp eax ; jump to it

do_0_9:
; do something 0-9
jmp .done

do_10_19:
; do something 10-19
jmp .done

; ...

.done:
; ...
lut:
dd do_0_9,do_10_19; ...    
This is dangerous if you have unexpected values and will only work for fixed size ranges (in this case 10) but means each block should be equally fast and you only need to add the code block and lut entry for more values.
Post 31 Jan 2011, 00:21
View user's profile Send private message Reply with quote
b1528932



Joined: 21 May 2010
Posts: 287
b1528932
Quote:
Have you ever used it? I've never seen it used. It ain't so simple to use, especially in a PM environment.

im almost sure there is a way to supress exception, propable eflags or msr, i dont recall it now.
Post 31 Jan 2011, 01:32
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-2020, Tomasz Grysztar.

Powered by rwasa.