flat assembler
Message board for the users of flat assembler.

 Index > Main > Little CMP Logic help.
Author
Overflowz

Joined: 03 Sep 2010
Posts: 1046
Overflowz 30 Jan 2011, 11:05
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
30 Jan 2011, 11:05
Overflowz

Joined: 03 Sep 2010
Posts: 1046
Overflowz 30 Jan 2011, 11:54
I've tried 1 logic but I'll be thankful if some of you will write ur own.. 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
}
30 Jan 2011, 11:54
ManOfSteel

Joined: 02 Feb 2005
Posts: 1154
ManOfSteel 30 Jan 2011, 12:09
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?
30 Jan 2011, 12:09
cod3b453

Joined: 25 Aug 2004
Posts: 618
cod3b453 30 Jan 2011, 12:15
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

@@:
30 Jan 2011, 12:15
Overflowz

Joined: 03 Sep 2010
Posts: 1046
Overflowz 30 Jan 2011, 14:00
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..

cod3b453
Nice trick on second example but I need numbers between values :p
30 Jan 2011, 14:00
mindcooler

Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler 30 Jan 2011, 14:54
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.
30 Jan 2011, 14:54
Overflowz

Joined: 03 Sep 2010
Posts: 1046
Overflowz 30 Jan 2011, 15:21
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..
30 Jan 2011, 15:21
mindcooler

Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler 30 Jan 2011, 15:52
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.
30 Jan 2011, 15:52
Overflowz

Joined: 03 Sep 2010
Posts: 1046
Overflowz 30 Jan 2011, 17:37
mindcooler
Can you post with using macros now ? Thanks.
30 Jan 2011, 17:37
mindcooler

Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler 30 Jan 2011, 17:44
I don't have any macro for pascal style case switches, perhaps some macro buff has?
30 Jan 2011, 17:44
edfed

Joined: 20 Feb 2006
Posts: 4324
Location: Now
edfed 30 Jan 2011, 18:00
post summary:
lowbound<eax<highbound == (eax-lowbound)<(highbound-lowbound)
due to the signed binary representation of integers.
30 Jan 2011, 18:00
mindcooler

Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler 30 Jan 2011, 21:14
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.
30 Jan 2011, 21:14
b1528932

Joined: 21 May 2010
Posts: 287
b1528932 30 Jan 2011, 21:30
bound instruction?
30 Jan 2011, 21:30
Picnic

Joined: 05 May 2007
Posts: 1385
Location: Piraeus, Greece
Picnic 30 Jan 2011, 23:34
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

30 Jan 2011, 23:34
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20062
revolution 31 Jan 2011, 00:18
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.
31 Jan 2011, 00:18
cod3b453

Joined: 25 Aug 2004
Posts: 618
cod3b453 31 Jan 2011, 00:21
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

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.
31 Jan 2011, 00:21
b1528932

Joined: 21 May 2010
Posts: 287
b1528932 31 Jan 2011, 01:32
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.
31 Jan 2011, 01:32
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum