# INSTRUCCIONES LOGICAS

## Instrucciones Logicas

El procesador proporciona instrucciones `AND, OR, XOR, TEST, NOT` de la logica boleana., que prueba, establece y borra los bits segun las necesidades del programa. El formato:

| Sr.No. | Instruction | Format                  |
| ------ | ----------- | ----------------------- |
| 1      | AND         | AND operand1, operand2  |
| 2      | OR          | OR operand1, operand2   |
| 3      | XOR         | XOR operand1, operand2  |
| 4      | TEST        | TEST operand1, operand2 |
| 5      | NOT         | NOT operand1            |

El primer operando en todos los casos podria estar en el registro o en la memoria. El segundo operando podria estar en registro/memoria o un valor inmediato (constante). Sin embargo, las operaciones de memoria a memoria no son posibles. Estas instrucciones comparan o hacen coincidir los bits de los operandos o banderas `CF, OF, PF, SF y ZF` .

#### Instruccion AND:

Se utiliza para admitr expresiones logicas mediante la realizacion de una operacion AND bit a bit. La operacion bit a bit devuelve 1, si lod bits coincidentes de ambos operandos son 1; de lo contrario devuelve 0. Por ejemplo:

```
             Operand1: 0101
             Operand2: 0011
----------------------
After AND -> Operand1: 0001
```

La operacion AND se puede utilizar para borrar uno o mas bits. Por ejemplosupongamos que el registro BL contiene 0011 1010. Si necesita borrar los bits de orden superior a cero, haga AND con OFH.

```
AND bl, 0FH    ; Establece a bl a 0000
```

Si desea verificar si un numero dado es par o impar, una prueba simple seria verificar el bit menos significativo. Si esto es 1, el numero es impar de los contrario el numero es par.

```
and al, 01h
jz numero_par
```

```
section .text
   global _start            
	
_start:                     
   mov   ax, 8h            ; Le pasamos un 8 a ax           
   and   ax, 1             ; Le damos un 1 a ax       
   jz    evnn              ; La instruccion jz resalta a evnn donde si el resultado de 
                             AND es cero el numero es par. Si es impar la ejecucion sigue.
   
   mov   eax, 4              
   mov   ebx, 1             
   mov   ecx, odd_msg      
   mov   edx, len2         
   int   0x80  
   
   ; Se usa para hacer condicional                          
   jmp   outprog

; Esto se ejecuta si el nuemro es par.
evnn:   
   mov   ah,  09h
   
   mov   eax, 4            
   mov   ebx, 1            
   mov   ecx, even_msg     
   mov   edx, len1          
   int   0x80              

; Finalizamos el programa
outprog:
   mov   eax,1              
   int   0x80              



section   .data
   even_msg  db  'Even Number!'
   len1  equ  $ - even_msg 
   
   odd_msg db  'Odd Number!'    
   len2  equ  $ - odd_msg
```

#### Instruccion OR:

Se utiliza parar admitir expresiones logicas mediante la realizacion de una operacion OR bit a bit. EL OR devuelve 1, si los bits tienen un 1 en cualquier lado, y devuelve 0 solo cuando los dos lo son:

```
                Operand1: 0101
                Operand2: 0011
------------------------------
After OR ->     Operand1: 0111
```

La operacion se puede usar para establecer uno o mas bits.&#x20;

`EJEMPLO:`

Se demuestra la instuccion OR. ALmacenamos el valor 5 y 3 en los registros AL y BL:

```
or al, bl
```

Debe almacenar 7 en el registro AL:

```
section .text 
    global _start
    
_start:
    mov al, 5
    mov bl, 3
    or  al, bl
    add al, byte '0'
    
    mov [result], al
    mov eax, 4
    mov ebx, 1
    mov ecx, result
    mov edx, 1
    int 0x80

outprog:
    mov eax, 1
    int 0x80
    
section .bss
    result resb 1
```

#### Instruccion XOR:

La operacion XOR establece el bit resultante en 1, si y solo si los bits son diferentes. Si los bits son iguales (ambos 0 o ambos 1), el bit se borra a 0.

Por ejemplo:

```
                Operand1: 0101
                Operand2: 0011
------------------------------
After XOR ->    Operand1: 0110
```

Con XOR un operando con sigo mismo cambia a 0.

#### Instruccion TEST:

Esta funciona similar a la operacion AND, pero con la dferencia no cambia el primer operando. Entonces si necesitamos verificar un numero en un registro par o impar tambien podemos hacerlo usando la instruccion TEST, sin cambiar el numero original.

```
test al, 01h
jz NUMERO_par
```

#### Instruccion NOT:

Esta instruccion invierte los bits en un operando. el operando puede estar en un registro o en la mamoria.

```
                Operand1: 0101 0011
Afther NOT ->   Operand1: 1010 1100
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bla0x1s-organization.gitbook.io/untitled-1/reverse-engineering/assembly-x86/instrucciones-logicas.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
