Page 1 of 1

Macro correctness

PostPosted: Sat May 26, 2018 7:09 am
by include12f675
Hi forum, I'm new here. I'd like to ask a question about these macro.
Code: Select all
; 16 bit compare by Y with X
; subtraction X to Y, X should be the greatest
; if X=Y then now Z=1
; if Y<X then now C=0
; if X<=Y then now C=1

cmp16       macro   Y,X,FAR
            local       NEAR
            clrc
            movf        high X,W
            subwf       high Y,W
            bnc         FAR
            bnz         NEAR
            movf        X,W
            subwf       Y,W
            bnc         FAR
NEAR
            endm
; Same as above but compare a file with a constant
cmpi16      macro   Y,CONST,FAR
            local       NEAR
            clrc
            movf        high Y,W
            sublw       high (CONST)
            bnc         FAR
            bnz         NEAR
            movf        Y,W
            sublw       low (CONST)
            bc         FAR
NEAR
            endm

; Same as above but compare a constant with a file
cmpc16      macro   CONST,Y,FAR
            local       NEAR
            clrc
            movlw       high (CONST)
            subwf       high Y,W
            bnc         FAR
            bnz         NEAR
            movlw       low (CONST)
            subwf       Y,W
            bc         FAR
NEAR
            endm

Honestly I haven't yet tested on the hardware :mrgreen: , but during a test with Proteus VSM, they don't behave as expected.
I'd expect that if the second term is less than the first it will branch to FAR.

Re: Macro correctness

PostPosted: Sat May 26, 2018 11:38 am
by ric
What family PIC (or even better, which precise PIC) are you testing this on?

Re: Macro correctness

PostPosted: Sat May 26, 2018 12:38 pm
by include12f675
Well, is a 12F675. But it won't matter as much as it's one of those that need to test the carry flag in between two sustractions, which isn't propagated.
Likewise 12F18xx they might not need such a stress, then the check it would be just at the macro end. But that's a different macro.

Re: Macro correctness

PostPosted: Sun May 27, 2018 12:02 pm
by ric
Just a quick sanity check here.
Y is the first parameter
X is the second parameter.
After a subtraction, the Carry flag is the opposite of what you get in Intel style processors.

Re: Macro correctness

PostPosted: Sun May 27, 2018 4:04 pm
by include12f675
Well, I haven't started to write program on PC. I'd like to set a number of macros to semplify the programming task in assembly. Even that I'm less keen to use assembly, rather I opt for some higher programming level, like basic or C. Only for optimization case I fold the sleeves to do the hard way.

Re: Macro correctness

PostPosted: Sun May 27, 2018 11:04 pm
by ric
A grounding in assembly is a good way to become familiar with the strengths and weaknesses of the hardware.
If you are able to learn C, then it is an excellent way to become more productive.
It's easy to insert small sequences of assembly language into a C program if you have very tight timing on any particular processes.