CHAPTER 7

MPLAB Number Formats


There are lots of little fiddly things about the way we WRITE the code itself. We notice that we can enter numbers in various formats, specifically in the beginning we are interested in Hexadecimal, Binary, and Decimal. Now MPLAB is very good in the sense that we can enter all three number formats providing we use the correct format designators. You know, give MPLAB a clue as to what you want to do.

The basic enter a number command is movlw
This is followed by the number. When I started, this caused me a lot of confusion, especially the decimal form. The problem is that MPLAB accepts different ways of writing the same thing

Lets look at the formats I have found, ( there may be more).

A simple test is write them as a code fragment and use the MPLAB compile to see if it is accepted.

Copy the program and compile it.

Note. The CODE lines that DON'T work, or do something different are preceded by ;
(Which I put in for practise, rather than eliminate it/ them.)
Try removing some ; compile, and see the warning/error message. But put the
Comment “;” back , recompile then move on.



;-----------------------------------------------------------------
; numsysts.asm version 1 March 02 written by Fred
;-----------------------------------------------------------------
;
; A LOOK AT NUMBER FORMATS THAT MPLAB WILL ACCEPT

; We test various ways of presenting numbers to MPLAB

    LIST P=16F84A ; 16F84A Runs at 4.000 MHz
    INCLUDE "p16f84A.inc"
    __CONFIG _CP_OFF & _WDT_OFF & _XT_OSC & _PWRTE_ON


; the error level comments are clear enough
        ERRORLEVEL -224 ; suppress annoying message because of tris
        ERRORLEVEL -302 ; suppress message because of page change

; The cblock is where the variables are defined. Note the starting address is given 0x0C
        CBLOCK 0x0C
        temp ; to hold a number for operations that can't be done on w.
        ENDC

; There is no interrupt routine ( jump to ORG 4) so not here but it is in the main prog.
        ORG 0 ; start at location 0
        goto Main ; jump over to main routine


; We use the constant entry command MOVLW to try out as many variants of
; the different number forms that we have found MPLAB accepts-
; Note the instruction movlw can be written with / without capital
; letters. But to give the code we write a coherent appearance we will
; always use SMALL letters

; open a watch window for temp and w

Main:
;Test entering number 0. MPLAB accepts all the combinations
;of UPPER and LOWER case letters.

        clrf temp

        movlw 0
        MOVLW 0
        Movlw 0
        movlW 0

; We also have offered 0 with no format, just plain 0
; Let's repeat with formats to see what happens.

        movlw 0x0 ; Hex
        MOVLW b'0' ; binary
        Movlw D'0' ; decimal
        movlW h'0' ; hex
        movlw 0H ; Hex
        movlw 0h ; hex
; just too many hex forms, be prepared. I use 0x as the hex identifier.

; Maybe another number would get different
; treatment, try with 9

; Note we will leave in place a format combination with
; a fault and write in the line below the "good" version
        movlw 9 ; none
        movlw 09 ; none
        movlw 0x9 ; Hex
;movlw b'9' bin nums are only 1's or 0's
        movlw b'1001' ; this is OK
        movlw D'9' ; decimal
        movlw h'9' ; hex

;Lets play a little more with the binary line (make it 5 to
; stay inside the 8 bit max)
    movlw b'0101' ; this is OK
;movlw 0101 ; 2
;movlw 0102 ; 3

; MPLAB accepts 2 and 3 but we start getting strange warnings
; about CBLOCK. 0101 and 0102 can also be decimal or hex.
; Note one 8bit byte can hold up to max
; B'11111111' or D '255' or H'FF'
; let's test what MPLAB THINKS THEY ARE.


    movlw b'0101' ; this is OK
    movwf temp
    incf temp,w ; if binary will be hex 5
; if decimal will be 0102,
; if hex will be 66 hex
; that has clarified, the number after adding 1 is 6,
;MPLAB treats as binary
; and if we take away the ' ' ? It will object to the b.
;If we also remove the b? well lets test


; movlw 0101 ; getting dodgy, MPLAB sees as 1
; movwf temp
; incf temp,w ; w is now 2

;Last tests on the decimal format

        clrf temp
        clrw

        ;addlw D255 non starter MPLAB SEES A NEW VARIABLE D255
        movlw d'255'
        movlw D'255'
        movlw .255

; These three are equivalent but the last is almost dangerous
; often I have missed or misinterpreted the point.


oot:
        goto oot

        end

; to be consistent I use the small letter forms
; b' 'and h ' ' and d' '
; though personally I prefer the 0x for hex

;------------------------------------------------------------
; End of Numsystems
;------------------------------------------------------------

Letter Case conventions

You can write the code in many ways, MPLAB is quite tolerant compared to other compiling programs. However it has some predefined words for variables/registers.

Where are they? Well after any compile when you have a watch window opened, the variables in your program become available for watching.

But you will also see a lot more that you didn't add… those below are often used ones,
with two forms like:-

  1. STATUS status
  2. INTCON intcon
  3. PORTA porta
  4. PORTB portb

Looking at the top one, the STATUS register, you need to add THE BIT NUMBER you want to observe , for example after doing an operation on a number, the result logically can be zero or not zero.

So we can bit test the status register to see if the zero flag is set with
the command 1 or z . also the Z is bit 2, so 2 is a valid entry as well

btfss status ,1 BAD btfss STATUS, 2 GOOD
btfss STATUS, 1 GOOD btfss STATUS, Z GOOD
btfss status, z BAD btfss STATUS, z BAD
btfss status, Z BAD btfss STATUS Z GOOD



Why only capitals? I don't know, but MPLAB only will accept the upper case version for these registers. The important thing is to know what can happen and use the correct form of the word.

In the code I have written there are two groups. All MPLAB predefined names like those for STATUS, are in upper case.

My variables, my labels and all instructions are in lower case. Only the first letter of a label is in upper case. These helps me to see what is what in the code that is written.

But I insist, you can write the code anyway you want, providing you use the UPPER CASE where MPLAB seems to need it.

More on generating code

Most of us will have written code in other languages and have generated a few pet routines.
The practical difficulty with assembler is that it is so elemental that a lot of routines cannot be reused as the commands don't exist.

Multiply, divide, subtract, add all need routines to be invented when we move away from simple bit operations.

Fortunately, many people have already sat down and sweated away forging these fundamental routines. Many of these routines and many of their inventors are to be found at PICLIST. COM on the web.

This is a huge very complete site on almost every aspect of code. To be more exact SOURCE CODE. There must be hundreds of examples classified under headings like math, lcd, … you name it.

We will lean very heavily on the site and refer to specific code routines already written
to get a second and probably better interpretation of how to do something.


The hex2dec Time problem with BIG numbers

In the Debug Break settings paragraphs, we mentioned that the hex2dec module could have problems. We looked at the h3d routine, myh3dec.asm. It was fine for small numbers, with the conversions taking about 100usec…. But the big numbers like distance max, 99999m would on this basis take 9.999 seconds, too long.

There we were asked for a solution, a simple solution.

Well now for the answer.

Consider 99950 metres. Starting at zero and advancing one by one till we arrive at 99950, indeed would take a long time . But we can use the same routines for hex decimal conversions in a subtly different way. The variables instead of being recalculated can be INCREASED. So in the DISTANCE module we only have to e update every second the variables to show the INCREASE IN DISTANCE.

        So the code instructions do not have to convert the whole number from scratch, but increase the numbers already held by ( 2 or 4 or 53 m), a small quickly executed increment.

This is a very different kettle of fish.

How long? We only ask the DISTANCE module to UPDATE the already calculated numbers by 2. The time required, once more drops to usecs.

We will see this soon in practice in the BIKE COMPUTER program

If you don't see the difference immediately in the time involved, don't worry, I didn't see it either, until a lot of different ways were tested. But for the moment we accept that INCREASING the already calculated variables by 2m will take very little time,…. we have measured typical low number times of 60us and high number times of about 350usec.