First page Back Continue Last page Overview Text
The above code scales the contents of a byte-sized variable by .18 (18/100). The result is then offset by 8. Notice that the value that we save is only the lower byte of the sum calculated using the addd instruction. The B register contains the least significant byte of the D register. Be careful not to confuse the variable X, whose value is stored at the location , with the x register, which is an operand of the tfr instruction.
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.