MPLAB X IDE doesn't seem to assemble my PIC16F877 assembly language program correctly

134 views Asked by At

I have a very simple PIC16F877 Assembly language program as follows:

PORTD EQU 0x08
TRISD EQU 0x88
STATUS EQU 0x03
PCLATH EQU 0x0A

    ORG 0
; start at address 0
    BCF  PCLATH, 4
    BCF  PCLATH, 3
    GOTO main

    ORG 6
main:
    CLRF STATUS
    BSF STATUS, 0x5
    BCF STATUS, 0x6
    CLRF TRISD
loop:
    BCF STATUS, 0x5
    BCF STATUS, 0x6
    MOVLW 0xFF
    MOVWF PORTD
    MOVLW 0x00
    MOVWF PORTD
    bcf  PCLATH, 4
    bcf  PCLATH, 3
    GOTO loop
    
end

I am using the MPLAB X IDE v6.15 and selected the appropriate chip and assembler. The output of the build is as follows:

"C:\Program Files\Microchip\xc8\v2.45\pic-as\bin\pic-as.exe" -mcpu=PIC16F877A -c \
-o build/default/debug/BlinkS.o \
BlinkS.S \
 -D__DEBUG=1   -mdfp="C:/Program Files/Microchip/MPLABX/v6.15/packs/Microchip/PIC16Fxxx_DFP/1.4.149/xc8"  -msummary=+mem,-psect,-class,-hex,-file,-sha1,-sha256,-xml,-xmlfull -fmax-errors=20 -mwarn=0 -xassembler-with-cpp
::: advisory: (2100) using the C99 standard library with the selected device may result in larger code and data usage
"C:\Program Files\Microchip\xc8\v2.45\pic-as\bin\pic-as.exe" -mcpu=PIC16F877A build/default/debug/BlinkS.o \
-o dist/default/debug/BlinkS.X.debug.hex \
 -D__DEBUG=1   -mdfp="C:/Program Files/Microchip/MPLABX/v6.15/packs/Microchip/PIC16Fxxx_DFP/1.4.149/xc8"  -msummary=+mem,-psect,-class,-hex,-file,-sha1,-sha256,-xml,-xmlfull -mcallgraph=std -Wl,-Map=dist/default/debug/BlinkS.X.debug.map -mno-download-hex
::: advisory: (2100) using the C99 standard library with the selected device may result in larger code and data usage
::: warning: (528) no start record; entry point defaults to zero

Memory Summary:
    Program space        used     0h (     0) of  2000h words   (  0.0%)
    Data space           used     0h (     0) of   170h bytes   (  0.0%)
    EEPROM space         used     0h (     0) of   100h bytes   (  0.0%)
    Configuration bits   used     0h (     0) of     1h word    (  0.0%)
    ID Location space    used     0h (     0) of     4h bytes   (  0.0%)
    Extra sections       used    14h (    20)

The first thing I noticed that is strange is that "Extra sections" is showing memory usage but nothing in the program space... The amount of data used (20 bytes) sounds about right but not sure why it's not being used in the Program space.

The second thing is that when I inspect the program memory (disassembled file), I see the following:

Disassembly Listing for BlinkS
                                                  1:     ; Program
                                                  2:     
                                                  3:     ;    processor PIC16F877A
                                                  4:     
                                                  5:         
                                                  6:     PORTD EQU 0x08
                                                  7:     TRISD EQU 0x88
                                                  8:     STATUS EQU 0x03
                                                  9:     PCLATH EQU 0x0A
                                                  10:    
                                                  11:    
                                                  12:        ORG 0
                                                  13:    ; start at address 0
0000  0A0A     INCF PCLATH, W                     14:        BCF  PCLATH, 4
0001  2806     GOTO 0x6                           15:        BCF  PCLATH, 3
0002  3FFF     ADDLW 0xFF                         16:        GOTO main
                                                  17:    
                                                  18:        ORG 6
                                                  19:    main:
0006  08FF     MOVF 0x7F, F                       20:        CLRF STATUS
0007  0800     MOVF INDF, W                       21:        BSF STATUS, 0x5
0008  0A0A     INCF PCLATH, W                     22:        BCF STATUS, 0x6
0009  280A     GOTO 0xA                           23:        CLRF TRISD
                                                  24:    loop:
000A  3FFF     ADDLW 0xFF                         25:        BCF STATUS, 0x5
000B  3FFF     ADDLW 0xFF                         26:        BCF STATUS, 0x6
000C  3FFF     ADDLW 0xFF                         27:        MOVLW 0xFF
000D  3FFF     ADDLW 0xFF                         28:        MOVWF PORTD
000E  3FFF     ADDLW 0xFF                         29:        MOVLW 0x00
000F  3FFF     ADDLW 0xFF                         30:        MOVWF PORTD
0010  3FFF     ADDLW 0xFF                         31:        bcf  PCLATH, 4
0011  3FFF     ADDLW 0xFF                         32:        bcf  PCLATH, 3
0012  3FFF     ADDLW 0xFF                         33:        GOTO loop
                                                  34:        
                                                  35:    end

It doesn't look quite right. I can see the GOTO statement is correct but all of the other statements doesn't match the program.

I wrote a similar program in C and it compiles and works correctly but not when I use assembly language. I need strict control of the timing of the ports, so I need to use assembly language. I have experience with other microcontroller assembly language but not the PIC and am completely new to the MPLAB IDE .... I imagine there is something wrong with my project settings somewhere but I can't seem to figure out where I went wrong. Any help would be appreciated.

As a side note, I did try adding #include <xc.inc> to my program and strangely, this resulted in the misassembled code to be in the EEPROM space (and 1 word less!).

Memory Summary:
    Program space        used     0h (     0) of  2000h words   (  0.0%)
    Data space           used     0h (     0) of   170h bytes   (  0.0%)
    EEPROM space         used    13h (    19) of   100h bytes   (  7.4%)
    Configuration bits   used     0h (     0) of     1h word    (  0.0%)
    ID Location space    used     0h (     0) of     4h bytes   (  0.0%)
 
0

There are 0 answers