Skip to content

Latest commit

 

History

History
91 lines (67 loc) · 4.32 KB

File metadata and controls

91 lines (67 loc) · 4.32 KB

Bitwise logical operations are fundamental operations in assembly language that allow you to manipulate individual bits of data. In ARM assembly language, you can perform various bitwise logical operations such as AND, OR, XOR, NOT, and shifting operations. These operations are crucial for tasks such as setting or clearing specific bits in a register, masking bits, or implementing bit flags.

Common Bitwise Logical Operations

  1. AND: This operation sets each bit to 1 if both bits are 1.
  2. OR: This operation sets each bit to 1 if at least one of the bits is 1.
  3. XOR: This operation sets each bit to 1 if the bits are different.
  4. NOT: This operation inverts all bits (0 becomes 1, and 1 becomes 0).
  5. Shift Left/Right: These operations shift bits to the left or right, effectively multiplying or dividing by powers of two.

Example: Bitwise Logical Operations in ARM Assembly

Here's an example that demonstrates how to perform various bitwise logical operations in ARM assembly language.

ARM Assembly Code

.section .data
    value1: .word 0b11001100      ; A sample 8-bit value (204 in decimal)
    value2: .word 0b10101010      ; Another sample 8-bit value (170 in decimal)
    result_and: .space 4           ; Space to store the result of AND operation
    result_or: .space 4            ; Space to store the result of OR operation
    result_xor: .space 4           ; Space to store the result of XOR operation
    result_not: .space 4           ; Space to store the result of NOT operation
    result_shift: .space 4         ; Space to store the result of left shift

.section .text
.global _start

_start:
    // Load the values from memory
    LDR     r0, =value1            ; Load address of value1 into r0
    LDR     r1, [r0]               ; Load value1 into r1
    LDR     r2, =value2            ; Load address of value2 into r2
    LDR     r3, [r2]               ; Load value2 into r3

    // Bitwise AND operation
    AND     r4, r1, r3             ; r4 = r1 AND r3
    STR     r4, =result_and        ; Store the result of AND

    // Bitwise OR operation
    ORR     r5, r1, r3             ; r5 = r1 OR r3
    STR     r5, =result_or         ; Store the result of OR

    // Bitwise XOR operation
    EOR     r6, r1, r3             ; r6 = r1 XOR r3
    STR     r6, =result_xor        ; Store the result of XOR

    // Bitwise NOT operation (inverts bits of value1)
    MVN     r7, r1                 ; r7 = NOT r1 (inverts bits)
    STR     r7, =result_not        ; Store the result of NOT

    // Left shift operation (shifting value1 left by 2 bits)
    LSL     r8, r1, #2             ; r8 = r1 << 2 (multiply by 4)
    STR     r8, =result_shift      ; Store the result of left shift

    // Exit the program
    MOV     r7, #1                 ; syscall number for exit
    SWI     0                      ; software interrupt to exit

.section .bss

Explanation of the Example:

  1. Data Section:

    • value1 and value2: Two 32-bit values (204 and 170 in decimal).
    • result_and, result_or, result_xor, result_not, result_shift: Reserved spaces to store the results of the respective operations.
  2. Loading Values:

    • LDR: Loads the addresses of value1 and value2 into registers and then loads their values into r1 and r3.
  3. Bitwise Operations:

    • AND: AND r4, r1, r3 performs a bitwise AND between r1 and r3, storing the result in r4.
    • OR: ORR r5, r1, r3 performs a bitwise OR and stores the result in r5.
    • XOR: EOR r6, r1, r3 performs a bitwise XOR and stores the result in r6.
    • NOT: MVN r7, r1 inverts the bits of r1 and stores the result in r7.
    • Shift Left: LSL r8, r1, #2 shifts the bits of r1 to the left by 2 positions (equivalent to multiplying by 4).
  4. Storing Results:

    • STR: Each result is stored in the corresponding space in memory.
  5. Exiting the Program:

    • The program ends with a syscall.

Summary

  • Bitwise Operations: This example demonstrates how to perform AND, OR, XOR, NOT, and left shift operations using ARM assembly.
  • Loading and Storing: Use LDR to load values and STR to store results.

These operations are essential for tasks that involve low-level data manipulation, such as handling flags, bit masks, and performing arithmetic operations more efficiently.