More 1 variables to pass to asm commands ?

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
cpservicespb
Posts: 35
Joined: 18 Oct 2022 21:43

More 1 variables to pass to asm commands ?

#1 Post by cpservicespb » 13 Nov 2022 21:11

There was the topic about variable at asm lines.
But it was about 1 variable.

There are 2 variables, for example:

Code: Select all

register unsigned int *Address = 0x0f89;
unsigned int short BitNum = 3;
How is to pass these variables to asm commands block:
_asm { BSF Address +0, BitNum }
Or it is impossible.

hexreader
Posts: 1785
Joined: 27 Jun 2010 12:07
Location: England

Re: More 1 variables to pass to asm commands ?

#2 Post by hexreader » 13 Nov 2022 21:52

Code: Select all

BSF Address +0, BitNum
BitNum must be a literal that is fixed at compile time, since it becomes part of the instruction.

You cannot feed two variables to the BSF instruction

Why not just set bit BitNum using C code - let the compiler write the correct Assembly code for you.

Here is the closest I could come up with for supplying a variable and a literal to the BSF instruction:

Code: Select all

//    PIC18F2550  on StartUSB board with 8MHz 2-pin crystal fitted for 48MHz clock
// build the project and run the debugger and step-over to get cycles count (delta)
// you are using StartUSB board, so you will need to run software simulator debugger, not hardware debugger

// simple LED flash program for StartUSB for PIC
// PIC18F2550 with 8MHz 2-pin crystal configured to give 48MHz CPU clock
// use with HID bootloader

// LEDs on RA1 and RB1
// both LEDs should blink half second off, half second on

// Board defines

#define LED_A LATA1_bit
#define LED_B LATB1_bit

// global variables
register unsigned int *Address = 0x0f89;

// defines
#define BitNum 3

void main() {

    ADCON1 = 0x0f;                                                              // disable analogue
    LATA = 0;                                                                   // all outputs low
    LATB = 0;                                                                   // all outputs low
    LATC = 0;                                                                   // all outputs low
    TRISA = 0;                                                                  // all output
    TRISB = 0;                                                                  // all output
    TRISC = 0;                                                                  // all output

    CMCON = 0;                                                                  // disable comparators
    LED_A = 1;                                                                  // LED on
    LED_B = 1;                                                                  // both LEDs on indicates restart
    Delay_ms(1000);                                                             // both LEDs on for 1 second - only at restart
    LED_A = 0;                                                                  // LED off
    LED_B = 0;                                                                  // both LEDs off

    asm{
        BSF Address + 0, BitNum
    }

    while(1);
}
Start every day with a smile...... (get it over with) :)

cpservicespb
Posts: 35
Joined: 18 Oct 2022 21:43

Re: More 1 variables to pass to asm commands ?

#3 Post by cpservicespb » 13 Nov 2022 22:41

Because of BitNum is unknown during compile time.
It becomes known during run firmware, more precisely, after receiving USB report from Host OS utility.

Here is:

Code: Select all

typedef struct PortsStruct { 
        unsigned char PortNum;
        unsigned int* DLByte;
        unsigned int* PLByte;
        unsigned short int DLBit;
        unsigned short int PLBit;
} PortsStruct, *pPortsStruct;

const struct PortsStruct Ports[9] = {
         {1, &LATA, &LATA, 0, 1},   // Port1[0] - LATA RA0 & RA1
         {2, &LATA, &LATA, 2, 3},   // Port2[1] - LATA RA2 & RA3
         {3, &LATA, &LATA, 4, 5},   // Port3[2] - LATA RA4 & RA5
         {4, &LATB, &LATB, 0, 1},   // Port4[3] - LATB RB0 & RB1
         // ...
    };
............
portNum come flying from OS Utility at 64 byte packet (USB report)
for example

Code: Select all

portNum = 4
then appropriate byte (address) and bit is got from the structure (matrix) based on portNum offset:

Code: Select all

pPort = &Ports;
pPortTmp = pPort + portNum - 1;
Address = pPortTmp->DataLineByte; 
BitNum= pPortTmp->DLBit;

    asm{
        BSF Address + 0, BitNum
    }
At the time I can not imagine how combine structure and defined macros as member of it to use one variable and one predefined macros.

Post Reply

Return to “mikroC PRO for PIC General”