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.

3.9 KB
Raw Blame

Fixed-point addition and subtraction in assembly language are similar to performing integer operations but with a critical detail: both numbers must have the same scaling factor (i.e., the same number of fractional bits) to ensure the correct result. If the two fixed-point numbers have the same format (e.g., both are in 16.16 fixed-point format), you can perform addition and subtraction as you would with integers. The result remains in the same fixed-point format.

Steps for Fixed-Point Addition and Subtraction

  1. Ensure Consistent Scaling: Both numbers must have the same scaling factor (i.e., the same number of fractional bits).
  2. Perform the Operation: Add or subtract the numbers as if they were integers.
  3. Result Interpretation: The result will still be in the fixed-point format.

Example: Fixed-Point Addition and Subtraction (16.16 Format)

For example, in 16.16 fixed-point format, two numbers like 0x0001_8000 (1.5 in decimal) and 0x0002_4000 (2.25 in decimal) can be added directly.

16.16 Format Breakdown:

  • 0x0001_8000 = $(1 + \frac{32768}{65536} = 1.5)$
  • 0x0002_4000 = $(2 + \frac{16384}{65536} = 2.25)$

Assembly Code Example (x86)

Here’s how you can add and subtract fixed-point numbers in 16.16 format:

section .data
    num1    dd  0x00018000      ; fixed-point number 1 (1.5 in 16.16 format)
    num2    dd  0x00024000      ; fixed-point number 2 (2.25 in 16.16 format)

section .text
    global _start

_start:
    ; Fixed-point addition
    mov     eax, [num1]         ; load first fixed-point number
    add     eax, [num2]         ; add second fixed-point number
    ; Now eax contains the result of 1.5 + 2.25 = 3.75 in fixed-point (0x0003C000)
    
    ; To verify, we'll now perform subtraction of the same two numbers
    mov     ebx, eax            ; store addition result for reference

    ; Fixed-point subtraction
    mov     eax, [num1]         ; load first fixed-point number again
    sub     eax, [num2]         ; subtract second fixed-point number
    ; Now eax contains the result of 1.5 - 2.25 = -0.75 in fixed-point (0xFFFFC000)
    
    ; Exit system call
    mov     eax, 60             ; exit system call number
    xor     edi, edi            ; exit code 0
    syscall

Explanation:

  1. Addition:

    • 0x0001_8000 (1.5 in decimal) and 0x0002_4000 (2.25 in decimal) are added.
    • The result is 0x0003_C000, which represents $( 3 + \frac{49152}{65536} = 3.75 )$.
  2. Subtraction:

    • 0x0001_8000 (1.5 in decimal) minus 0x0002_4000 (2.25 in decimal) results in 0xFFFF_C000, which is negative. This is because in two’s complement, the number 0xFFFF_C000 represents -0.75 in 16.16 fixed-point format.

Key Operations:

  • ADD and SUB: Both of these operations work as expected with fixed-point numbers, as long as both numbers use the same format.
  • Two’s Complement: When subtracting, remember that the result may be negative, and it will be represented in two's complement form.

Interpreting Results:

  • The result of addition or subtraction is still in the fixed-point format. If you want to convert this back to a human-readable format (like a floating-point decimal), you can extract the integer and fractional parts as explained earlier.

Example Results:

  • Addition Result: $(1.5 + 2.25 = 3.75)$, stored as 0x0003_C000 in 16.16 format.
  • Subtraction Result: $(1.5 - 2.25 = -0.75)$, stored as 0xFFFF_C000 in 16.16 format (in two's complement).

Handling Different Fixed-Point Formats:

  • If the two fixed-point numbers have different scaling factors (e.g., 16.16 and 8.24), you must first align the formats by shifting one of the numbers appropriately. For instance, to add a 16.16 fixed-point number to a 32.32 fixed-point number, you'd shift the 16.16 number left by 16 bits to convert it into 32.32 format.