Skip to navigation

Revs on the BBC Micro

Maths (Arithmetic): Absolute8Bit

Name: Absolute8Bit [Show more] Type: Subroutine Category: Maths (Arithmetic) Summary: Calculate the absolute value (modulus) of an 8-bit number
This routine returns |A|. It can also return A * abs(n), where A is given the sign of n.
Arguments: A The number to make positive N flag Controls the sign to be applied: * If we want to calculate |A|, do an LDA or equivalent before calling the routine * If we want to calculate A * abs(n), do a BIT n before calling the routine * If we want to set the sign of A, then call with: * N flag clear to calculate A * 1 * N flag set to calculate A * -1
.Absolute8Bit BPL aval1 \ If A is positive then it already contains its absolute \ value, so jump to aval1 to return from the subroutine EOR #&FF \ Negate the value in A using two's complement, as the CLC \ following is true when A is negative: ADC #1 \ \ |A| = -A \ = ~A + 1 .aval1 RTS \ Return from the subroutine