flat assembler
Message board for the users of flat assembler.

Index > Windows > best way to implment locical 'and' as in &&

Author
Thread Post new topic Reply to topic
ishkabible



Joined: 13 Sep 2010
Posts: 54
ishkabible
i was looking into conditionals and find that the && and || operators are more unwieldy than i previously thought. i figured there would be a flag for and a conditional jump to go along with it, but i have found no such thing. i thought about it and realized it could be implemented via a series of jumps and conditional tests to see if both items are not equal to zero.

here is what i came up with (i do not have an assembler available right now because i'm on a school computer, so there may be errors)

a = b && c;
Code:
cmp b,0
je .@false
cmp c,0
je .@false
mov [a],1
jmp .@end
.@false:
mov [a],0
.@end
    


a = b || c;

Code:
cmp b,0
je .@false
jmp .@true
cmp c,0
je .@false
.@true:
mov [a],1
jmp .@end
.@false:
mov [a],0
.@end:
    


to me this seems excessive so i'm asking if there is a better way.
Post 03 Nov 2010, 17:17
View user's profile Send private message Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler
edx = (((eax>='a') and (eax<='z')) or ((eax>='A') and (eax=<'Z'))) or (eax='_')

Code:
 .checkalpha:
        xor     ecx,ecx
        xor     edx,edx
        cmp     al,'_'
        je      .alpha

        cmp     al,'A'
        setae   cl
        cmp     al,'Z'
        setbe   dl
        and     dl,cl
        jnz     .alpha

        xor     ecx,ecx
        cmp     al,'a'
        setae   cl
        cmp     al,'z'
        setbe   dl
        test    dl,cl
        jnz     .alpha

 .nonalpha:
        xor     edx,edx
        retn
 .alpha:
        retn    

_________________
This is a block of text that can be added to posts you make.
Post 03 Nov 2010, 17:42
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
ishkabible,

You may look at compilers' output. They actually use de Morgan's rules to simplify conditionals.

Mr. Hyde's comment: there ain't such thing as best way: you must supply criteria to decide.

----8<----
mindcooler,

Isn't cmp al,'_' / je .alpha misleading?


Last edited by baldr on 03 Nov 2010, 17:50; edited 1 time in total
Post 03 Nov 2010, 17:46
View user's profile Send private message Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler
In my implementation, _ was included in the definition of alphanumeric.
Post 03 Nov 2010, 17:49
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
mindcooler,

Oh, I see. Wink
Post 03 Nov 2010, 17:51
View user's profile Send private message Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler
To answer the question in the title, I would implement && as and.
Post 03 Nov 2010, 17:52
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
mindcooler,

Bitwise and? Heh, maybe I've found another FORTH follower. Wink

P.S. 0= OVER 0= OR NOT . Wink
Post 03 Nov 2010, 17:55
View user's profile Send private message Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler
There is no other and. Or and and is implemented in hardware and no branches are necessary.

And I'm not FORTH, I'm afraid. (Not afraid of FORTH)
Post 03 Nov 2010, 19:12
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
mindcooler,

Boolean and (&& in C-speak) is nowhere near bitwise and (&), as you may see in simple 2&1==0.

FORTH is nothing to be afraid about, you may like it. Wink
Post 03 Nov 2010, 19:22
View user's profile Send private message Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler
Solution is easy: stop using C booleans!

Nah, I gave up on HLAs when I met FASM.
Post 03 Nov 2010, 20:14
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
mindcooler,

Those who consider even the slight possibility of FORTH being HLA should be considered infidels. Wink
Post 03 Nov 2010, 20:21
View user's profile Send private message Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler
Well, it's _almost_ a stack machine language, but only almost Smile
Post 03 Nov 2010, 20:30
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
mindcooler,

FORTH features unique synergy of being interpretive (in a way) and compilative (in a way, too), like fasm. Stack handling comes naturally when you're at expressions' evaluation, so why not? But CREATE … DOES> can't be beat, AFAIK.

As always, we've been driven off-topic. Wink
Post 03 Nov 2010, 20:47
View user's profile Send private message Reply with quote
ishkabible



Joined: 13 Sep 2010
Posts: 54
ishkabible
bit-wise and can be used as the logical and operator on other Boolean values. take this
a <= b && b >= c is the same as a <= b & b >= c. just my little 2 cents.
1) thanks balder, i looked into de morgens rules, that should help
2) for criteria lets say that there is no context in witch the code will be used, just the fastest set of operations to set 'a' equal to the result of "b && c" i know it sounds strange but i think it will give me a good base line on how to implement it with out any special thought. i also am more specifically asking because i want to make an intermediate representation language so that i can do things like a = b + c or a+=c or even a[b]=c. as i was thinking how to implement each statement i came across things like "if a && b goto c" and "a = b && c" and was wondering how to best implement these in a 'raw' unoptimized form. that is why i am making the only criteria that there is no context and it needs to accomplish the task requested. i understand that is is almost impossible to find the 'defector' best way to do this, so i will state that i would like to see a reasonably fast solution.
Post 03 Nov 2010, 20:52
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
ishkabible wrote:
2) for criteria lets say that there is no context in witch the code will be used, just the fastest set of operations to set 'a' equal to the result of "b && c"
8088-fastest? 8086-fastest? 80286-fastest, etc.? You really should read Michael Abrash's "Zen of the Assembly Language"; while it's old, good insight of the bottlenecks implied on code execution will be apparent.
ishkabible wrote:
i know it sounds strange but i think it will give me a good base line on how to implement it with out any special thought. i also am more specifically asking because i want to make an intermediate representation language so that i can do things like a = b + c or a+=c or even a[b]=c. as i was thinking how to implement each statement i came across things like "if a && b goto c" and "a = b && c" and was wondering how to best implement these in a 'raw' unoptimized form. that is why i am making the only criteria that there is no context and it needs to accomplish the task requested. i understand that is is almost impossible to find the 'defector' best way to do this, so i will state that i would like to see a reasonably fast solution.
Implement it as you can. Test the implementation. Analyse results (this part is the most difficult, but there are tools; look at CPU makers' sites). Improve. Try again.
Post 03 Nov 2010, 21:16
View user's profile Send private message Reply with quote
ishkabible



Joined: 13 Sep 2010
Posts: 54
ishkabible
thanks good advice, if i got no awnsers i was going to resort to this method, but from experience i have learned that if im not sure how to implement something i should see if someone else has useful insight on the subject.

Quote:

...this part is the most difficult, but there are tools; look at CPU makers' sites...


so i should look on the AMD website and see if there are any profiling tools?

Quote:

8088-fastest? 8086-fastest? 80286-fastest, etc.? You really should read Michael Abrash's "Zen of the Assembly Language"; while it's old, good insight of the bottlenecks implied on code execution will be apparent.


im going to be making x86 applications so 8086. not completely sure what you mean by that but that is my answer, weather it's coherent and relevant or not i will let you be the judge.
Post 03 Nov 2010, 23:59
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17287
Location: In your JS exploiting you and your system
revolution
x86 means more than just 8086. x86 covers an entire class of CPUs that includes AMD, Via, Harris, Intel etc. as the makers and from the original 8086 up to the latest Athlon-X or i7 (or whatever is the latest). So when you say the 'fastest', you must specify precisely for which CPU it will be the fastest. And if you want it to be the fastest across all the CPUs then your task is impossible since they all behave differently and are optimised in different ways. Note that the speed is affected by the mobo and other external factors, so yo may need to specify these things also.
Post 04 Nov 2010, 00:24
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:  


< 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 YouTube, Twitter.

Website powered by rwasa.