1 | ; $Id: ASMBitLastSetU16.asm 96407 2022-08-22 17:43:14Z vboxsync $
|
---|
2 | ;; @file
|
---|
3 | ; BiosCommonCode - ASMBitLastSetU16() - borrowed from IPRT.
|
---|
4 | ;
|
---|
5 |
|
---|
6 | ;
|
---|
7 | ; Copyright (C) 2006-2022 Oracle and/or its affiliates.
|
---|
8 | ;
|
---|
9 | ; This file is part of VirtualBox base platform packages, as
|
---|
10 | ; available from https://www.virtualbox.org.
|
---|
11 | ;
|
---|
12 | ; This program is free software; you can redistribute it and/or
|
---|
13 | ; modify it under the terms of the GNU General Public License
|
---|
14 | ; as published by the Free Software Foundation, in version 3 of the
|
---|
15 | ; License.
|
---|
16 | ;
|
---|
17 | ; This program is distributed in the hope that it will be useful, but
|
---|
18 | ; WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | ; General Public License for more details.
|
---|
21 | ;
|
---|
22 | ; You should have received a copy of the GNU General Public License
|
---|
23 | ; along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | ;
|
---|
25 | ; SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | ;
|
---|
27 |
|
---|
28 |
|
---|
29 | ;*******************************************************************************
|
---|
30 | ;* Header Files *
|
---|
31 | ;*******************************************************************************
|
---|
32 | public _ASMBitLastSetU16
|
---|
33 |
|
---|
34 | .8086
|
---|
35 |
|
---|
36 | _TEXT segment public 'CODE' use16
|
---|
37 | assume cs:_TEXT
|
---|
38 |
|
---|
39 |
|
---|
40 | ;;
|
---|
41 | ; Finds the last bit which is set in the given 16-bit integer.
|
---|
42 | ;
|
---|
43 | ; Bits are numbered from 1 (least significant) to 16.
|
---|
44 | ;
|
---|
45 | ; @returns (ax) index [1..16] of the last set bit.
|
---|
46 | ; @returns (ax) 0 if all bits are cleared.
|
---|
47 | ; @param u16 Integer to search for set bits.
|
---|
48 | ;
|
---|
49 | ; @cproto DECLASM(unsigned) ASMBitLastSetU16(uint32_t u16);
|
---|
50 | ;
|
---|
51 | _ASMBitLastSetU16 proc
|
---|
52 | .8086
|
---|
53 | push bp
|
---|
54 | mov bp, sp
|
---|
55 |
|
---|
56 | mov cx, [bp + 2 + 2]
|
---|
57 | test cx, cx ; check if zero (eliminates checking dec ax result)
|
---|
58 | jz return_zero
|
---|
59 |
|
---|
60 | mov ax, 16
|
---|
61 | next_bit:
|
---|
62 | shl cx, 1
|
---|
63 | jc return
|
---|
64 | dec ax
|
---|
65 | jmp next_bit
|
---|
66 |
|
---|
67 | return_zero:
|
---|
68 | xor ax, ax
|
---|
69 | return:
|
---|
70 | pop bp
|
---|
71 | ret
|
---|
72 | _ASMBitLastSetU16 endp
|
---|
73 |
|
---|
74 | _TEXT ends
|
---|
75 | end
|
---|
76 |
|
---|