flat assembler
Message board for the users of flat assembler.

 Index > Main > Boolean Logic Instructions
Author
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 Why ? It's like x+y-1
about AND logic, I really don't understand anything.. Can someone explain please ? Thank you.
24 Jun 2011, 13:02
DJ Mauretto

Joined: 14 Mar 2007
Posts: 464
Location: Rome,Italy
DJ Mauretto
hello
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
```

_________________
Nil Volentibus Arduum
24 Jun 2011, 13:16
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 )      ```

_________________
Nil Volentibus Arduum
24 Jun 2011, 13:21
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..
24 Jun 2011, 14:05
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
25 Jun 2011, 03:42
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

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
```
25 Jun 2011, 06:12

Joined: 25 Sep 2003
Posts: 2140
Location: Estonia
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)
;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
;EAX = 4 (4 <
```

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)
```
27 Jun 2011, 11:40
 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