In assembly language, programmers can make use of the following five logical instructions:
AND
– Returns 1 only when both bits are 1; otherwise, it returns 0.NOT
– Reverses each bit.OR
– Returns 1 if one or one of the two bits or both bits are 1. Returns 0 if both bits are 0.XOR
– Returns 1 only if one of the two bits is 1; returns 0 in all other cases.TEST
– Same as the AND
operator, but does not change the value of the first operand.Logical operators are executed bit-wise, which means that each bit in the first operand is compared to the corresponding bit (in accordance with the position) in the second operand. The new bit for that position in the resulting number depends on the semantics of the logical operator.
The
NOT
operator only takes one operand and reverses each bit one by one. Except for theTEST
operator, all logical operators save the resulting number in the first operand.
The syntax for AND
, TEST
, XOR
, and OR
instructions is as follows:
operator_name operand_1 operand_2 ;
On the other hand, the NOT
instruction only takes in one operand:
not operand_1;
The program below performs several logical operations on an arbitrary number stored in the eax
general purpose register.
Upon successful execution of all the operations, the program checks whether or not the resulting number is even or odd. A display message is printed onto the console before the program ends, as we can see here:
section .textglobal _start_start: ;denotes the entry point of the programmov eax, 4h ;stores 0100 in eaxand eax, 1h ;performs 0100 AND 0001 and saves 0000 in eaxor eax, 1h ;performs 0000 OR 0001 and saves 0001 in eaxand eax, 2h ;performs 00001 AND 0010 and saves 0000 in eaxxor eax, 1h ;performs 0000 XOR 0001 and saves 0001 in eaxnot eax ;saves 1110 in eaxand eax, 1h ;performs 1110 AND 0001 and saves 0000 in eax, which is zero and evenjz if_even ;jz stands for 'jump if zero'. if value in eax is 0, program execution jumps to the if_even section, skipping all the lines in between. If not, it executes the next instruction without altering the program flow.mov eax, 4 ;system call number for the write system call saved in eaxmov ebx, 1 ;file descriptor of the stdout stream saved in ebxmov ecx, odd ;display message for odd number saved in ecxmov edx, len_odd ;length of display message saved in edxint 0x80 ;calls kernel to call the write system calljmp exit_prog ;jumps to the exit_prog section, skipping all code in the if_even sectionif_even:mov eax, 4 ;system call number for the write system call saved in eaxmov ebx, 1 ;file descriptor of the stdout stream saved in ebxmov ecx, even ;display message for odd number saved in ecxmov edx, len_even ;length of display message saved in edxint 0x80 ;calls kernel to call the write system callexit_prog:mov eax,1 ;system call number (sys_exit)int 0x80 ;call kernelsection .dataeven db 'The final number was an even number!' ;display message for even numberlen_even equ $-even ;length of the display message 'even'odd db 'The final number was an odd number!' ;display message for odd numberlen_odd equ $-odd ;length of the display message 'odd'
Free Resources