ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

ARM ASM: Bitwise Set Highest Set Bit

ASM ARM

Goal -- WPM

Ready
Exercise Algorithm Area
1.global find_highest_set_bit
2
3@ find_highest_set_bit: Finds the position of the most significant bit (MSB).
4@ R0: input value
5@ R0: position of MSB (0-31), or -1 if input is 0.
6find_highest_set_bit:
7PUSH {R4, R5, LR} @ Save registers and LR
8MOV R4, R0 @ R4 = input value
9MOV R5, #-1 @ R5 = default return value (-1 for zero input)
10
11CMP R4, #0 @ Check if input is zero
12BEQ .done @ If zero, return -1
13
14@ Use a binary search-like approach to find the MSB position
15@ Check upper 16 bits
16TST R4, #0xFFFF0000 @ Test if any bits in upper half are set
17ADDNE R5, R5, #16 @ If yes, add 16 to position
18MOVNE R4, R4, LSR #16 @ Shift R4 right by 16 to work with upper half
19
20@ Check upper 8 bits of the remaining 16
21TST R4, #0xFF00 @ Test if any bits in upper half are set
22ADDNE R5, R5, #8 @ If yes, add 8 to position
23MOVNE R4, R4, LSR #8 @ Shift R4 right by 8
24
25@ Check upper 4 bits of the remaining 8
26TST R4, #0xF0 @ Test if any bits in upper half are set
27ADDNE R5, R5, #4 @ If yes, add 4 to position
28MOVNE R4, R4, LSR #4 @ Shift R4 right by 4
29
30@ Check upper 2 bits of the remaining 4
31TST R4, #0xC @ Test if any bits in upper half are set
32ADDNE R5, R5, #2 @ If yes, add 2 to position
33MOVNE R4, R4, LSR #2 @ Shift R4 right by 2
34
35@ Check upper 1 bit of the remaining 2
36TST R4, #0x2 @ Test if the second bit is set
37ADDNE R5, R5, #1 @ If yes, add 1 to position
38@ No need to shift R4 further, the last bit's position is determined
39
40.done:
41MOV R0, R5 @ Move final position to R0 for return
42POP {R4, R5, LR} @ Restore registers and LR
43BX LR @ Return
Algorithm description viewbox

ARM ASM: Bitwise Set Highest Set Bit

Algorithm description:

This ARM assembly function `find_highest_set_bit` determines the index of the most significant bit (MSB) that is set to 1 in a given 32-bit integer. For example, if the input is 0b00001000 (decimal 8), the MSB is at position 3 (0-indexed). If the input is 0, it returns -1. This operation is useful in algorithms that require bit manipulation, such as calculating logarithms, determining the size of a number, or optimizing certain bitwise operations. It's a common utility in low-level programming and performance-critical code.

Algorithm explanation:

The `find_highest_set_bit` function takes the input value in R0. It first checks if the input is zero; if so, it returns -1. Otherwise, it uses a series of conditional shifts and tests to efficiently locate the MSB. It checks the upper 16 bits; if any are set, it adds 16 to the result position and shifts the value right by 16. It then repeats this process for the remaining 8 bits, then 4, then 2, and finally 1 bit. Each step effectively halves the search space. The `TST` instruction checks if any bits in a given mask are set, and `ADDNE`/`MOVNE` conditionally update the result and the working value. The time complexity is O(log N), where N is the number of bits (32 in this case), because it performs a fixed number of steps (log2(32) = 5 steps). The space complexity is O(1) for register usage and stack. This approach is significantly faster than a linear scan of bits.

Pseudocode:

FUNCTION find_highest_set_bit(value):
  IF value IS 0 THEN
    RETURN -1
  END IF
  position = 0
  IF upper 16 bits of value are set THEN
    position = position + 16
    value = value >> 16
  END IF
  IF upper 8 bits of value are set THEN
    position = position + 8
    value = value >> 8
  END IF
  IF upper 4 bits of value are set THEN
    position = position + 4
    value = value >> 4
  END IF
  IF upper 2 bits of value are set THEN
    position = position + 2
    value = value >> 2
  END IF
  IF second bit of value is set THEN
    position = position + 1
  END IF
  RETURN position
END FUNCTION