Re: Best way to emulate BSR?



Assuming you are using 16 bit (you did say the processor didn't support BSR), the following is the simplest I could think of.

Lookup tables might be faster but with an old processor you probably are trying to conserve memory as well.

This is equivalent to BSR dx, ax

test ax, ax
jz ____Finish

push ax
push cx
push bx

clear dx
mov cx, 8
mov bx, 0FF00h
jmp ____Beg

LoopTop:
shr bx, cl
____Beg:
test ax, bx
jz ____Next
shr ax, cl
add dx, cx
____Next:
shr cl, 1
jnz LoopTop

test bx, bx ; just to clear the z flag

pop bx
pop cx
pop ax

____Finish:

Cassandra

.