PIC If...then....else directive

Please can someone help. I'm quite new to PIC directives and I am having loads of trouble with the following "simple" code.

ADRESH holds the value of 2D

IF ADRESH > 0x2D GOTO LOOP ELSE GOTO LOOP1 ENDIF

The problem is that the if statement tests the address of ADRESH and not the value of ADRESH.

How do I test the value held in the register?

Many thanks.

Reply to
Gary Charlton
Loading thread data ...

If this is assembly language (which it looks to be), then you have the wrong idea about these directives. They are for conditional assembly. They are equivalent to this in C:

#if adresh > 0x2d compile this #else compile this #endif

The directives don't generate code to read registers and decide which way to go.

Reply to
Gary Kato

->IF ADRESH > 0x2D

-> GOTO LOOP

->ELSE

-> GOTO LOOP1

->ENDIF

->

-

-If this is assembly language (which it looks to be), then you have the wrong

-idea about these directives. They are for conditional assembly. They are

-equivalent to this in C:

-

-#if adresh > 0x2d

- compile this

-#else

- compile this

-#endif

-

-The directives don't generate code to read registers and decide which way to

-go.

Correct. BTW the C directives work in PIC assembly so the code you listed will compile fine.

The code you need, and the theory behind it, can be found here:

formatting link

BAJ

Reply to
Byron A Jeff

As Gary correctly pointed out, the code you posted implements "conditional assembly". I.E. the code is parsed and acted upon only at assembly time (never at run time). And, of course, at assembly time your register, ADRESH, exists only as an *address*; since the location doesn't actually exist at assembly time, its contents cannot be tested then.

Reply to
Michael

Is there perhaps a way to achieve the intended result using some form of macro?

Reply to
Hawkmoo

What is a macro? It's just a set of one or more instructions that the assembler inserts into your code. And those instructions get executed only at run time. No, I think a macro won't do what you want.

But I'm beginning to suspect that I don't understand what you want to do.

Maybe you want to define a CONSTANT and then, depending on that value, have either GOTO LOOP or GOTO LOOP1 assembled into your code??

Reply to
Michael

Please don't confuse me with the original poster.

I'm sure we all wrote a lot of code that looks something like (obviously depending on instruction set)

CJNE A, #255, l1; if A=10 then MOV B, #1 JMP l2 l1: ; else MOV B, #2 l2: ; endif

my question was is there some kind of efficient way to macro-ize this so as not to bother with two labels, as well as make the code more readable.

Reply to
Hawkmoo

It really would depend on the macro capabilities of the assembler. The problem is automatically generating the labels needed. You could have the macro use some assembler symbol (an EQU) and have your macro increment the symbol when called. One problem is that some assemblers only allow a symbol to be defined once, so: XX EQU 1

XX EQU XX + 1

will cause an error. If your assembler takes it, then you still have the problem of creating the symbol.

Ideally, you'd want to concatenate the symbol with a string:

LABEL+XX

I don't know how many assemblers are this sophisticated. I seem to recall that the old PDP-11 MACRO-11 assembler could do this.

Of course, one could always write a separate macro processor then feed its output to the assembler.

Reply to
Gary Kato

Hawkmoo wrote: : : my question was is there some kind of efficient way to macro-ize this : so as not to bother with two labels, as well as make the code more : readable. : :

Take a look at the 'structured assembly' stuff on this page:

formatting link
(get SASM2002.09.28). You'll also need MP_SASM if you don't have MPLAB installed.

An example of using it: IF_ .z. MOVLF 250,ALIVECNT bcf PORTA,RA4 ENDIF_

This generate runtime code that's what you are wanting.

ttyl,

--buddy

--
Remove '.spaminator' and '.invalid' from email address
when replying.
Reply to
buddy.spaminator.smith

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.