Solved

Assembly logic and/or...

Posted on 2000-03-20
3
369 Views
Last Modified: 2010-05-18
Would you please tell me are there instructions in Assembly I can use to logically and/or/xor two number?

I only know and or ...but they are bitwise.

Waiting your help
Good Luck
0
Comment
Question by:ASH
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
nils pipenbrinck earned 25 total points
ID: 2635323
There are no logic binary operations in assembler.. you have to evaluate every single expression of your code... in most cases you can combine several expressions into one using binary logic.

AND-instructions must be evaluated for each expression while OR and XOR can be combined.. if you take a look at the examples below you'll get the point why..

here is one example where AND binary doesn't work:


  int a = 16;
  int b = 32;

  if (a&&b)
  {
    // will be called because a && b is true... note: a&b would evaluate to zero!
    blah();
  }


that would translate into: (x86 code assumed) (remember: this is wrong code!)

  mov eax, 16
  mov ebx, 32
  and eax, ebx
  jnz .label
  call blah
..label:


however, if you rewrite it this way it works:

  if (a) if (b)
  {
    blah();
  }

  translated:

  mov eax, 16
  mov ebx, 32
  test eax, eax
  jz   .label1
  test ebx, ebx
  jz   .label1
  call blah
..label:

for the logical or operation you can simply use the binary or since or preserves bits:

  int a = 16;
  int b = 32;

  if (a||b)
  {
    blah();
  }

  translates nicely into:

  mov eax, 16
  mov ebx, 32
  or  eax, ebx
  jz  .label1
  call blah
..label1:

Logical or operations can always be evaluated using the binary or instruction (unless you do more arithmetics with the result.. see below)...


xor will always be evaluated binary.. there is no logical version of xor.


there are still some things to keep in mind: the compiler use a internal representation of the "true" state..

for example:

  int a = 2;
  int b = 2;
  printf ("%d\n", (a&&b)|1);

this will print a 1, not a 3 because (a&&b) evaluates to true which is 0x01.. (I'm not 100% sure if it depends on the compiler).

if you have code that does such bad things you have to use the 1 for logical arithmetics in your assembly code.

  mov ecx, 0
  mov eax, 2
  mov ebx, 2
  test eax, eax
  jz   .label1
  mov  ecx, 1
..label1:
  test ebx, ebx
  jz   .label2
  mov  ecx, ecx
..label2:

  ; now the or-part:
  or   ecx, 1
  push ecx
  call print_integer
 


I hope this helped,
  Nils
0
 
LVL 4

Expert Comment

by:nils pipenbrinck
ID: 2635330
ehm. the last example was incorrect..

try this one..

  ; ecx will hold the result:
  mov ecx, 0
  mov eax, 2
  mov ebx, 2
  test eax, eax
  jz   .label1
  mov  ecx, 1
..label1:
  test ebx, ebx
  jz   .label2
  mov  ecx, 1
..label2:

  ; now the or-part:
  or   ecx, 1
  push ecx
  call print_integer
   
0
 
LVL 22

Expert Comment

by:nietod
ID: 2635709
Assembly is CPU-specific.  What CPU?

>> logically and/or/xor two number?
   *   *   *
>> I only know and or ...but they are
>> bitwise.

logical and/or/xor doesn't apply to numbers, only to boolean (true/false) values.   Bitwise and/or/xor applies to numbers.  If you represent your boolean values with true as 1 and false as 0, then you use the bitwise operators to perform logical (boolean) evaluations.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
why "." vs "->" 23 116
Grammars for C C++ and java 1 108
computer science syllabus 3 52
I could not build boost code, 10 55
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now