Multiple Precision Arithmetic
Multiple Precision Arithmetic
In this section
Page 1 of 8
John J. McDonough, WB8RCR
Page 2 of 8
John J. McDonough, WB8RCR
Notice that this is not any different from what we would do if we were adding decimal numbers by hand.
1
In previous examples, these blocks have always started at H20. There is no magic to the location, though. File register addresses in the 16F84A run from H0c to H4f. As long as we start after H0b and end before H50 we can choose whatever we want. One minor annoyance, though. The p16f84a include file defines _CP_ON as H0f. Since this definition is encountered before ours, if we start our use of the memory at H0c, the file register display shows H0f as _CP_ON rather than whatever we have assigned. This does not affect how the program runs at all, but it is an annoyance when we are debugging.
Page 3 of 8
John J. McDonough, WB8RCR
Subtraction Routine
This time we do the subtraction when the carry has been cleared because of the borrow, rather than, as in addition, when set because of a carry. Although these examples show only two bytes, the same logic can be carried on for any arbitrary size number.
Page 4 of 8
John J. McDonough, WB8RCR
Add16
; Do a subtraction call
Sub16
After you have stepped through each of the routines to satisfy yourself that you understand what is going on, try the following; Step down to the call instruction, then click on the step over button. The subroutine will be executed, but you will not be taken into it step by step. Instead, you will see just the results. This can be handy when you are confident with your subroutine logic, but you want to test a variety of different cases. One of the differences between the 6.30 and 6.40 versions of MPLAB is that the 6.40 version includes a Step Out button. This allows you to step partway through a subroutine then skip over the rest of the subroutine and return to the calling program. This isnt nearly as handy as the step over button, but it can be useful in some cases. Now, try testing the routine with values that require a carry and a borrow. 258 and 255 will work here. When you step through the subroutines, notice that in this case we take the path where we add in the carry (or subtract the borrow). Continued on next page
Page 5 of 8
John J. McDonough, WB8RCR
Add16
Do a subtraction call
Sub16
;--------------------------------------------------------------------------------; Set up arguments with carry/borrow movlw H'02' ; First value H'0102' movwf v1_lo ; = 258 decimal movlw H'01' movwf v1_hi
Page 6 of 8
John J. McDonough, WB8RCR
Do a subtraction call
Sub16
Homework Assignment 1
Homework Assignment 2
Page 7 of 8
John J. McDonough, WB8RCR
Wrap Up
Summary In this lesson, we looked at how to represent numbers that are too large to fit in a single byte. We also developed routines for adding and subtracting these larger numbers. Up until now, all of our programs have been totally self-contained within the PIC. We have had no way to interact with the user or with other circuitry. In the next lesson, we will look at how the file register memory in the PIC is banked, and how we interact with the outside world.
Coming Up
Page 8 of 8
John J. McDonough, WB8RCR