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.2 KB
Raw Blame

Fixed-point multiplication involves multiplying two fixed-point numbers and then adjusting the result to maintain the correct scaling. The key challenge in fixed-point multiplication is ensuring that the scaling factor (i.e., the number of fractional bits) is preserved correctly in the result.

In 16.16 fixed-point format, both operands are treated as 32-bit integers where the lower 16 bits represent the fractional part. After multiplying two 16.16 numbers, you get a 64-bit result, but the scaling factor needs to be corrected by shifting the result back by 16 bits.

Steps for Fixed-Point Multiplication

  1. Multiply as Integers: Multiply the two numbers as if they were normal integers.
  2. Shift to Adjust Scaling: After multiplication, the result is scaled by $( 2^{16} )$ twice (once for each operand), so you need to shift the result right by 16 bits to adjust the scaling.
  3. Handle Overflow: Ensure that the result fits in the appropriate number of bits (e.g., 32 bits for a 16.16 result).

Example: Fixed-Point Multiplication (16.16 Format)

Let’s multiply 1.5 and 2.25, represented as 16.16 fixed-point numbers:

  • 1.5 in 16.16 is 0x0001_8000
  • 2.25 in 16.16 is 0x0002_4000

The result of multiplying these two numbers in floating-point would be $( 1.5 \times 2.25 = 3.375 )$. In fixed-point, the multiplication needs to handle the scaling properly.

Assembly Code Example (x86)

Here’s how you can multiply two 16.16 fixed-point numbers in assembly:

section .data
    num1    dd  0x00018000      ; 1.5 in 16.16 fixed-point format
    num2    dd  0x00024000      ; 2.25 in 16.16 fixed-point format

section .text
    global _start

_start:
    ; Load the first fixed-point number into eax
    mov     eax, [num1]         ; load num1 (1.5)
    
    ; Perform multiplication
    mov     ebx, [num2]         ; load num2 (2.25)
    imul    ebx                 ; multiply eax (num1) by ebx (num2), result is 64-bit in edx:eax

    ; Correct the scaling: shift the result right by 16 bits
    ; We need to shift the combined 64-bit result edx:eax
    shrd    eax, edx, 16        ; shift the high 16 bits of edx into eax

    ; Now eax contains the result in fixed-point format

    ; Exit system call
    mov     eax, 60             ; exit system call number
    xor     edi, edi            ; exit code 0
    syscall

Explanation:

  1. IMUL Instruction: The imul instruction multiplies the two 32-bit fixed-point numbers, storing the 64-bit result in the edx:eax pair. This ensures that we capture both the integer and fractional parts of the result.
  2. SHRD Instruction: The shrd (shift right double) instruction shifts the combined 64-bit result in edx:eax by 16 bits to adjust for the scaling. This puts the correctly scaled result back into eax, which now contains the 16.16 fixed-point result.

Key Points:

  • Multiplying fixed-point numbers: Each fixed-point number is scaled by $( 2^{16} )$, so the result must be scaled back down by $( 2^{16} )$. This is done by shifting the result right by 16 bits.
  • 64-bit result: The multiplication of two 32-bit fixed-point numbers produces a 64-bit result, which must be properly shifted and truncated to fit back into a 32-bit register.

Example:

Let’s break down the multiplication of 1.5 and 2.25:

  • Fixed-point representation:
    • 1.5 is 0x0001_8000 (in 16.16 format).
    • 2.25 is 0x0002_4000 (in 16.16 format).

Multiplying them directly: $[ 0x0001_8000 \times 0x0002_4000 = 0x0002_D000_0000 ]$

The result before scaling is 0x0002_D000_0000, which represents $( 3.375 \times 2^{32} )$ (since it’s scaled by $( 2^{32} ))$. After shifting the result right by 16 bits: $[ 0x0002_D000_0000 \div 2^{16} = 0x0002_D000 = 3.375 ]$

Thus, the final result is $( 3.375 )$ in 16.16 fixed-point format.

Summary:

  • Multiply as integers: Use imul to multiply two 32-bit fixed-point numbers.
  • Shift right: Use shrd to shift the result right by 16 bits to correct the scaling.
  • Handle overflow: If necessary, handle overflow by ensuring the result fits within the desired number of bits.