Since shifting an unsigned integer value to the right one position is equivalent to dividing that value by two, you can also use the shift right instruction for division by powers of two:
shr ax, 1 ;Equivalent to AX/2
shr ax, 2 ;Equivalent to AX/4
shr ax, 3 ;Equivalent to AX/8
shr ax, 4 ;Equivalent to AX/16
shr ax, 5 ;Equivlaent to AX/32
shr ax, 6 ;Equivalent to AX/64
shr ax, 7 ;Equivalent to AX/128
shr ax, 8 ;Equivalent to AX/256
if ax contains 0000000000000010, shr ax, 1 produces 0000000000000001, which is 2/2 = 1, quite simple.
But what if we do shr ax, 3? the results is 0, which is not ax/3. How to explain???
Thanks
shr ax, 1 ;Equivalent to AX/2
shr ax, 2 ;Equivalent to AX/4
shr ax, 3 ;Equivalent to AX/8
shr ax, 4 ;Equivalent to AX/16
shr ax, 5 ;Equivlaent to AX/32
shr ax, 6 ;Equivalent to AX/64
shr ax, 7 ;Equivalent to AX/128
shr ax, 8 ;Equivalent to AX/256
if ax contains 0000000000000010, shr ax, 1 produces 0000000000000001, which is 2/2 = 1, quite simple.
But what if we do shr ax, 3? the results is 0, which is not ax/3. How to explain???
Thanks
If ax = 2h,
shr ax, 3 will result in 0 (2/8 = .25 or rounded down = 0). This is because the shift is just shifting of the bits. For accuracy, you would would not be using shifts, but fpu. But of course does the decimal value matters? Oh yes, one more thing the last bit shifted out is stored in the carry flag. That could be of some uses of course.
shr ax, 3 will result in 0 (2/8 = .25 or rounded down = 0). This is because the shift is just shifting of the bits. For accuracy, you would would not be using shifts, but fpu. But of course does the decimal value matters? Oh yes, one more thing the last bit shifted out is stored in the carry flag. That could be of some uses of course.