Skip to content

This Repository is a collection of different ethical hacking tools and malware's for penetration testing and research purpose written in python, ruby, rust, c++, go and c.

4.5 KB
Raw Blame

Performing bitwise logical operations with immediate data in ARM assembly language allows you to manipulate values directly with constants (immediate values) rather than needing to load them from memory first. Immediate values are often used in bit masking and setting or clearing specific bits within registers.

Example: Bitwise Logical Operations with Immediate Data

In this example, we'll perform several bitwise operations using immediate values. We will demonstrate the following operations:

  1. AND: Using an immediate value to mask specific bits.
  2. OR: Setting specific bits using an immediate value.
  3. XOR: Toggling specific bits using an immediate value.
  4. NOT: Inverting bits (although NOT cannot take immediate values directly, we will simulate its effect).
  5. Shift Operations: Shifting an immediate value left or right.

ARM Assembly Code

.section .data
    value: .word 0b11001100           ; A sample 8-bit value (204 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_shift: .space 4             ; Space to store the result of left shift

.section .text
.global _start

_start:
    // Load the value from memory
    LDR     r0, =value                ; Load address of value into r0
    LDR     r1, [r0]                  ; Load the value into r1

    // Bitwise AND operation with immediate data
    AND     r2, r1, #0b00001111        ; r2 = r1 AND 0b00001111 (mask lower nibble)
    STR     r2, =result_and           ; Store the result of AND

    // Bitwise OR operation with immediate data
    ORR     r3, r1, #0b00010000        ; r3 = r1 OR 0b00010000 (set the 5th bit)
    STR     r3, =result_or            ; Store the result of OR

    // Bitwise XOR operation with immediate data
    EOR     r4, r1, #0b11110000        ; r4 = r1 XOR 0b11110000 (toggle upper nibble)
    STR     r4, =result_xor           ; Store the result of XOR

    // Simulating NOT operation with immediate (inversion)
    MVN     r5, r1                     ; r5 = NOT r1 (inverts bits of r1)
    STR     r5, =result_shift          ; Store the result of NOT

    // Left shift operation with immediate data
    LSL     r6, r1, #2                 ; r6 = r1 << 2 (shift left by 2 bits)
    STR     r6, =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:

    • value: A sample 32-bit value (204 in decimal, 0b11001100).
    • result_and, result_or, result_xor, result_shift: Reserved spaces to store results of the operations.
  2. Loading the Value:

    • LDR r0, =value: Loads the address of value into r0.
    • LDR r1, [r0]: Loads the value from memory into r1.
  3. Bitwise Operations with Immediate Values:

    • AND:
      • AND r2, r1, #0b00001111: Masks the lower nibble of r1 and stores the result in r2.
    • OR:
      • ORR r3, r1, #0b00010000: Sets the 5th bit of r1 and stores the result in r3.
    • XOR:
      • EOR r4, r1, #0b11110000: Toggles the upper nibble of r1 and stores the result in r4.
    • NOT:
      • MVN r5, r1: Inverts all bits of r1 and stores the result in r5. This simulates the NOT operation.
    • Left Shift:
      • LSL r6, r1, #2: Shifts the bits of r1 to the left by 2 positions, effectively multiplying by 4, and stores the result in r6.
  4. Storing Results:

    • Each result is stored in the respective memory location using STR.
  5. Exiting the Program:

    • The program ends with a syscall to exit.

Summary

  • Bitwise Operations with Immediate Data: This example illustrates how to use immediate values for AND, OR, and XOR operations in ARM assembly.
  • Immediate Values: You can perform these operations directly without needing to load the immediate values from memory.
  • Shifts and Inversion: Shifting can also be done directly with immediate values, and while NOT doesn't directly take an immediate, it can be simulated using MVN.

These operations are essential for tasks involving low-level data manipulation, including flag settings, masking, and bit-level arithmetic. If you have further questions or need more examples, feel free to ask!