How To Clear Register In Assembly
Assembly - Logical Instructions
The processor education set provides the instructions AND, OR, XOR, Test, and Non Boolean logic, which tests, sets, and clears the $.25 according to the need of the program.
The format for these instructions −
| Sr.No. | Didactics | Format |
|---|---|---|
| i | AND | AND operand1, operand2 |
| 2 | OR | OR operand1, operand2 |
| 3 | XOR | XOR operand1, operand2 |
| iv | TEST | TEST operand1, operand2 |
| 5 | Not | Not operand1 |
The kickoff operand in all the cases could exist either in annals or in memory. The second operand could be either in register/memory or an firsthand (constant) value. However, memory-to-retention operations are not possible. These instructions compare or lucifer bits of the operands and set the CF, OF, PF, SF and ZF flags.
The AND Didactics
The AND didactics is used for supporting logical expressions by performing bitwise AND performance. The bitwise AND operation returns 1, if the matching $.25 from both the operands are 1, otherwise it returns 0. For example −
Operand1: 0101 Operand2: 0011 ---------------------------- After AND -> Operand1: 0001
The AND operation can exist used for clearing one or more bits. For example, say the BL register contains 0011 1010. If you need to clear the high-society bits to zilch, you AND information technology with 0FH.
AND BL, 0FH ; This sets BL to 0000 1010
Permit's take up some other example. If you want to cheque whether a given number is odd or even, a simple exam would exist to check the to the lowest degree meaning bit of the number. If this is ane, the number is odd, else the number is even.
Assuming the number is in AL annals, we can write −
AND AL, 01H ; ANDing with 0000 0001 JZ EVEN_NUMBER
The following program illustrates this −
Example
section .text global _start ;must be declared for using gcc _start: ;tell linker entry betoken mov ax, 8h ;getting 8 in the ax and ax, 1 ;and ax with 1 jz evnn mov eax, 4 ;organisation phone call number (sys_write) mov ebx, 1 ;file descriptor (stdout) mov ecx, odd_msg ;bulletin to write mov edx, len2 ;length of message int 0x80 ;call kernel jmp outprog evnn: mov ah, 09h mov eax, 4 ;organisation call number (sys_write) mov ebx, 1 ;file descriptor (stdout) mov ecx, even_msg ;bulletin to write mov edx, len1 ;length of message int 0x80 ;call kernel outprog: mov eax,i ;arrangement phone call number (sys_exit) int 0x80 ;call kernel section .data even_msg db 'Even Number!' ;message showing fifty-fifty number len1 equ $ - even_msg odd_msg db 'Odd Number!' ;message showing odd number len2 equ $ - odd_msg
When the above code is compiled and executed, it produces the post-obit consequence −
Even Number!
Modify the value in the ax register with an odd digit, like −
mov ax, 9h ; getting 9 in the ax
The program would display:
Odd Number!
Similarly to articulate the entire register you lot can AND it with 00H.
The OR Instruction
The OR instruction is used for supporting logical expression by performing bitwise OR performance. The bitwise OR operator returns 1, if the matching bits from either or both operands are ane. Information technology returns 0, if both the $.25 are zero.
For example,
Operand1: 0101 Operand2: 0011 ---------------------------- Later on OR -> Operand1: 0111
The OR operation can exist used for setting one or more bits. For case, let us assume the AL register contains 0011 1010, you need to set the four low-lodge bits, you tin OR information technology with a value 0000 1111, i.e., FH.
OR BL, 0FH ; This sets BL to 0011 1111
Example
The following example demonstrates the OR instruction. Let us store the value 5 and 3 in the AL and the BL registers, respectively, then the educational activity,
OR AL, BL
should store seven in the AL register −
department .text global _start ;must be alleged for using gcc _start: ;tell linker entry point mov al, v ;getting 5 in the al mov bl, three ;getting 3 in the bl or al, bl ;or al and bl registers, result should be 7 add together al, byte '0' ;converting decimal to ascii mov [issue], al mov eax, 4 mov ebx, one mov ecx, event mov edx, 1 int 0x80 outprog: mov eax,1 ;system call number (sys_exit) int 0x80 ;call kernel section .bss result resb ane
When the above code is compiled and executed, information technology produces the following result −
7
The XOR Didactics
The XOR pedagogy implements the bitwise XOR operation. The XOR operation sets the resultant bit to 1, if and but if the bits from the operands are dissimilar. If the bits from the operands are same (both 0 or both 1), the resultant bit is cleared to 0.
For case,
Operand1: 0101 Operand2: 0011 ---------------------------- Subsequently XOR -> Operand1: 0110
XORing an operand with itself changes the operand to 0. This is used to clear a register.
XOR EAX, EAX
The Examination Teaching
The Examination instruction works aforementioned every bit the AND operation, but dissimilar AND instruction, information technology does not change the beginning operand. So, if we need to bank check whether a number in a register is fifty-fifty or odd, we tin can also practise this using the Exam instruction without changing the original number.
Examination AL, 01H JZ EVEN_NUMBER
The Non Instruction
The Non instruction implements the bitwise Non operation. Not operation reverses the bits in an operand. The operand could be either in a register or in the retention.
For example,
Operand1: 0101 0011 After Non -> Operand1: 1010 1100
Useful Video Courses
Video
Video
How To Clear Register In Assembly,
Source: https://www.tutorialspoint.com/assembly_programming/assembly_logical_instructions.htm
Posted by: mundywendone1953.blogspot.com

0 Response to "How To Clear Register In Assembly"
Post a Comment