flat assembler
Message board for the users of flat assembler.

Index > Main > Boolean Logic Instructions

Author
Thread Post new topic Reply to topic
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
Hello everyone. I'm stuck about logical instructions in assembly.. Exactly, AND and OR instructions.. I don't understand how they work normally..
This is my guess (false.)
1 or 1 = 1+1 (+ = plus)
1 and 1 = 1*1 (* = multiplication)
but when I'm trying something like this:
5 or 3 = 7 Sad Why ? It's like x+y-1
about AND logic, I really don't understand anything.. Can someone explain please ? Thank you.
Post 24 Jun 2011, 13:02
View user's profile Send private message Reply with quote
DJ Mauretto



Joined: 14 Mar 2007
Posts: 464
Location: Rome,Italy
DJ Mauretto
hello Very Happy
for each logic function, at least initially you have to write a table of truth '
Code:

0 and 0 = 0
0 and 1 = 0
1 and 0 = 0
1 and 1 = 1
    

Wink

_________________
Nil Volentibus Arduum Razz
Post 24 Jun 2011, 13:16
View user's profile Send private message Reply with quote
DJ Mauretto



Joined: 14 Mar 2007
Posts: 464
Location: Rome,Italy
DJ Mauretto
Quote:
5 or 3 = 7 Sad Why ? It's like x+y-1


Code:

0 or 0 = 0
0 or 1 = 1
1 or 0 = 1
1 or 1 = 1


5 = 101
3 = 011

5 or 3 = 101 or 011 = 111 ( 7 )      
Wink

_________________
Nil Volentibus Arduum Razz
Post 24 Jun 2011, 13:21
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
Ohh, I guess I should work with binary numbers only.. I asked this question because I saw many programs were using something like "and eax,ebx" where EAX and EBX were more than 0 or 1.. but I don't know what they wanted to do. Thanks.. Smile
Post 24 Jun 2011, 14:05
View user's profile Send private message Reply with quote
garystampa



Joined: 25 May 2011
Posts: 52
Location: Central FLorida
garystampa
You should be thinking about bits and not decimal numbers.

OR and AND aren't really for operating on numbers per se, although they can be...

If you want an index to wrap around a buffer you might make the buffer 7f+1 in length. Then you can increment the index and AND it with 7f to make sure it's always in range.

ORing is often used to turn bits on. ANDing is often used to turn them off. I have never found a use for ORing decimal numbers for the sake of creating a new decimal number. XOR is also useful.

You can test for an odd or even number by testing the lowest bit with AND:

if (x & 1) odd!
else even!

test ax,1
jnz odd
jz even
Post 25 Jun 2011, 03:42
View user's profile Send private message Reply with quote
idle



Joined: 06 Jan 2011
Posts: 359
Location: Ukraine
idle
i'd never guess what Agner meant:
Code:
[Find minimum of two unsigned values]                  [Assembler][/][8086]

This small gem show how you can find the minimum of two unsigned numbers:

        if (b < a)  a = b;

The approach used here does not use any branches which may mess up your BTB
(Branch Target Buffer)

;
; Find minimum of two unsigned values
;
; input:
;   eax = value a
;   ebx = value b
;
; output:
;   eax = smallest value
;
; destroys:
;   ebx, ecx
;   flags
;

        sub     ebx,eax
        sbb     ecx,ecx
        and     ecx,ebx
        add     eax,ecx

You can use this gem on 16 bit machines too, just use 16 bit registers.
This gem comes from Agner Fog's Pentium optimization manual. Be sure to get
this manual.
                                                      Gem writer: Agner Fog
                                                   last updated: 1998-03-16
    
Post 25 Jun 2011, 06:12
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2140
Location: Estonia
Madis731
If it was a question, I will post an answer:
Code:
; Variant A: EAX > EBX i.e. 7 and 5
        sub     ebx,eax ; 5-7=-2
        sbb     ecx,ecx ; carry flag was raised: ecx-ecx-1=-1 (0FFFFFFFFh)
        and     ecx,ebx ; ecx=ebx because 0FFFFFFFFh & -2 = -2 (0FFFFFFFEh)
        add     eax,ecx ; 7+(-2)=5
;EAX = 5 (5 < 7)

; Variant B: EAX < EBX i.e. 4 and 8
        sub     ebx,eax ; 8-4=4
        sbb     ecx,ecx ; carry flag not set: ecx-ecx-0=0
        and     ecx,ebx ; ecx=0 because 0 & 4 = 0
        add     eax,ecx ; 4+0=4
;EAX = 4 (4 < Cool
    


I provide a simpler example:
Code:
cmp eax,ebx
cmovg eax,ebx ; conditional mov is relatively old (P6). It acted slowly on some AMD64 CPUs. I think its okay to use it today.
;One can use cmova if unsigned operation is needed.
;EAX = MIN(EAX,EBX)
    
Post 27 Jun 2011, 11:40
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger 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 GitHub, YouTube, Twitter.

Website powered by rwasa.