[59480] | 1 | ; $Id: ASMBitLastSetU16.asm 98103 2023-01-17 14:15:46Z vboxsync $
|
---|
| 2 | ;; @file
|
---|
[60484] | 3 | ; BiosCommonCode - ASMBitLastSetU16() - borrowed from IPRT.
|
---|
[59480] | 4 | ;
|
---|
| 5 |
|
---|
| 6 | ;
|
---|
[98103] | 7 | ; Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[59480] | 8 | ;
|
---|
[96407] | 9 | ; This file is part of VirtualBox base platform packages, as
|
---|
| 10 | ; available from https://www.virtualbox.org.
|
---|
[59480] | 11 | ;
|
---|
[96407] | 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 | ;
|
---|
[59480] | 27 |
|
---|
| 28 |
|
---|
| 29 | ;*******************************************************************************
|
---|
| 30 | ;* Header Files *
|
---|
| 31 | ;*******************************************************************************
|
---|
[60484] | 32 | public _ASMBitLastSetU16
|
---|
[59480] | 33 |
|
---|
[60484] | 34 | .8086
|
---|
[59480] | 35 |
|
---|
[60484] | 36 | _TEXT segment public 'CODE' use16
|
---|
| 37 | assume cs:_TEXT
|
---|
| 38 |
|
---|
| 39 |
|
---|
[59480] | 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 | ;
|
---|
[60484] | 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.
|
---|
[59480] | 48 | ;
|
---|
| 49 | ; @cproto DECLASM(unsigned) ASMBitLastSetU16(uint32_t u16);
|
---|
| 50 | ;
|
---|
[60484] | 51 | _ASMBitLastSetU16 proc
|
---|
| 52 | .8086
|
---|
[59480] | 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)
|
---|
[60484] | 58 | jz return_zero
|
---|
[59480] | 59 |
|
---|
| 60 | mov ax, 16
|
---|
[60484] | 61 | next_bit:
|
---|
[59480] | 62 | shl cx, 1
|
---|
[60484] | 63 | jc return
|
---|
[59480] | 64 | dec ax
|
---|
[60484] | 65 | jmp next_bit
|
---|
[59480] | 66 |
|
---|
[60484] | 67 | return_zero:
|
---|
[59480] | 68 | xor ax, ax
|
---|
[60484] | 69 | return:
|
---|
[59861] | 70 | pop bp
|
---|
[59480] | 71 | ret
|
---|
[60484] | 72 | _ASMBitLastSetU16 endp
|
---|
[59480] | 73 |
|
---|
[60484] | 74 | _TEXT ends
|
---|
| 75 | end
|
---|
[59480] | 76 |
|
---|