Chapter 9
How to use Logical instructions
-------------------------------

To predict the results of the logical instructions, you must convert the Hexadecimal
number into binary numbers and then preform the logical function on each bit.
Logical instructions are for bit manipulation of the registers specified. This is the
only way we can set or clear the bits inside any of the registers.

The AND instruction is used to clear individual bits.

The OR instruction is used to set individual bits.

The XOR instruction is used to clear a register to zero.

USE the following chart to convert between Hexadecimal, decimal and binary.

________1_____________1_____________0001
________2_____________2_____________0010
________3_____________3_____________0011
________4_____________4_____________0100
________5_____________5_____________0101
________6_____________6_____________0110
________7_____________7_____________0111
________8_____________8_____________1000
________9_____________9_____________1001
________10_____________A____________1010
________11_____________B____________1011
________12_____________C____________1100
________13_____________D____________1101
________14_____________E____________1110
________15_____________F____________1111
----------------------------------------------------------------------

Converting from Hex to Binary and Binary to Hex.
------------------------------------------------

To convert HEX to binary, You just find the four bit binary number that represents
each HEX digit.

A5---------(hex number)
----/---\
---/-----\
1010---0101 (binary number)

To convert BINARY to HEX, You just find the hex digit value for each four bit group
of binary digits.

10001101 (binary number)
-\ /--\ /
-- 8-----D (hex number)

If the AX register has the HEX number 45A1 in it. Then,

AX = 4 5-------- A 1 -------------(hex number)
---------/--|--------- |---\
--------/---|--------- |----\
-------/----|--------- |-----\
---0100--0101 1010 0001-----(binary number)

How to predict the LOGICAL instruction
-----------------------------------
To predict the result of any logical instruction, You must convert all values into

BINARY, then preform the logical function on each pair of bits separately.
The truth tables for the logical functions are shown below.

AND OR XOR
-----------

1 x 1 = 1 1 + 1 = 1 1 xor 1 = 0
1 x 0 = 0 1 + 0 = 1 1 xor 0 = 1
0 x 1 = 0 0 + 1 = 1 0 xor 1 = 1
0 x 0 = 0 0 + 0 = 0 0 xor 0 = 0

Let's find the logical result of the AX register in the program listed.

MOV AX,3545
AND AX,1722

1. Convert both numbers into binary and arrange them one over the other. (write
them down in four bit groups)

2. Preform the logical function on each pair of bits, starting with the LSB.

---------B15------------------------B0
3 5 4 5=...0011..0101..0100..0101. (LSB)
1 7 2 2=x..0001..0111..0010..0010.
-----------------------------------------------------
-----------0001..0101..0000..0000. (result)

--------------1-------5-------0--------0 (hex)

3. Convert the result back into hex.

-------------------0001 0101 0000 0000 = 1500

Then AX = 1500.

NOT AX
NOT AL
NOT BYTE 

The not instruction will invert each bit of the specified register or memory
location.(This will produce the one's complement)

AND AX,
AND AX,F000
AND AX,BX

Produces the logical result of and-ing the two values specified. This is useful for
resetting or clearing an individual bit in the program. (1x0=0)

OR AX,
OR AX,1556
OR AX,BX

Produces the logical result of or-ing the two values specified. This is useful for setting an
individual bit in the program. (1+0=1)

XOR AX,
XOR AX,BX
XOR AX,AX

Produces the logical result of xor-ing the two values specified. This is useful for
comparing two values.

SHL AX,CL
SHL AL,01
SHL AX,01

Shifts the bits of the value specified by the number of times in the CL register. SHL will
shift to the left. SHR will shift to the right. The bits that are pushed out of the register
are dropped and zero's are pushed into the other side.

ROL AX,CL
ROL AL,01
ROR AX,CL
ROR AL,01

Rotates the bits of the value specified by the number of times in the CL register. ROR
rotates to the right. ROL rotates to the left. In the rotate, all the bits that are pushed
out of the register, are rotated around to the other side and pushed back in. Suppose
AX=834F and we have an instruction like,

ROR AX,1
Then
The rotate is toward the right.

AX = 1000 0011 0100 1111 <--the LSB rotates around to the other side. ---------------->

MSB-->1 1000 0011 0100 111 (the bit pushed out will become the MSB)

If we regroup and convert to hex,

1100 0001 1010 0111

C 1 A 7

AX = C1A7

How to use Transfer of control instructions
-------------------------------------------

CALL 240
CALL 
CALL [BX]

Calls a sub-routine in another part of the program. The sub-routine must end with RET.
When the sub-routine is finished, the program Will return to the instruction
immediately following the call instruction.

RET

Must be the last instruction of a sub-routine.Causes the program to return to the calling
program.
JMP 100
JMP 
JMP [BX]

location is executed and the program continues from that point.

JZ 150

This is a conditional jump to the specified address if the zero flag is set. (JZ=jump if zero)
The address must be within 80 hex bytes of the JZ instruction. (short jump) This
instruction must immediately follow an arithmetic, logical or INT instruction.

JNZ 300

This is a conditional jump to the specified address if the zero flag is not set. (JNZ= jump
if not zero) The address must be within 80 hex bytes of the JNZ instruction.
(short Jump) This instruction must immediately follow an arithmetic, logical or INT
instruction.

LOOP 250

The loop instruction is used to execute an instruction or a group of instructions more
than one time. Like the FOR/NEXT instruction in basic programming.The loop count is
put into the CX register before the loop instruction. Each time the loop instruction is
executed, the CX register will count down by one. When CX counts down to zero
the program will go the next instruction after LOOP.
Example:

MOV CX,07D0 ;The count for the loop

MOV AH,9 ;
MOV BH,50 ; This group of instructions
MOV BL,DL ; will be repeated 7D0 times.
MOV AL,DL ;
INT 10 ;
INC DL ;
LOOP 103 ; The program will jump back to
; offset 103 until CX=0
INT 20 ; After 7D0 times the program
; will end

Predict what this program does and than use the
A command to assemble it. After the program is
assembled, use the G command to run and verify
the results.

INT XX

The INT instruction is used to access the operating system (IBMBIO and IBMDOS)
as well as the ROM bios chip on the mother board. XX in this instruction is one of 256
possible INT calls.

• Chapter 10