## Find Y = X * 0.18 + 8 for a given X

## Code:

- ldaa variable
- Ldab #18
- mul
- ldx #100
- idiv ; quotient is in x register
- tfr x,d ; move it to d register
- addd #8 ; add offset
- stab variable ; store l.s. byte of sum

A crucial step in writing code that performs mathematical operations is making sure that the result of each operation will fit in the location designated for the result. First, we verify that the expected result will fit in one byte. Then, we check each step along the way. Here, we need to verify that the sum of the quotient and 8 fits in a byte.

We proceed as follows:

X <256 because it was stored as a single byte.

Y = X*0.18+8 < 256*.18+8 < 300*.2+8=68<256 so the result Y will fit in one byte.

Next we check each step:

18*255 <20*260 =5200 = $1450, which can be stored in the 16-bit D register is ok.

5200/100=52 also fits in 16 bits, so no problem here.

52+8 = 60 < 255 so the result fits in the 8-bit B register portion of the D register and we need not worry about the A register portion of the result.