AVR assembler problems with GCC and MPLAB

Go To Last Post
39 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I created a pure single asm file project  with MPLAB ,gcc as compiler

ledon.S :

#include <avr/io.h>
.global main
.section text
.org 0
main:
    jmp main

I compile it and got these outputs:

CLEAN SUCCESSFUL (total time: 51ms)
make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
make[1]: Entering directory '/home/jack/work/avrasm/ledon.X'
make  -f nbproject/Makefile-default.mk dist/default/production/ledon.X.production.hex
make[2]: Entering directory '/home/jack/work/avrasm/ledon.X'
"/usr/local/avr/bin/avr-gcc"  -mmcu=atmega16 -I "/home/jack/.mchp_packs/Microchip/ATmega_DFP/3.0.158/include" -B "/home/jack/.mchp_packs/Microchip/ATmega_DFP/3.0.158/gcc/dev/atmega16"  -x assembler-with-cpp -c -D__ATmega16__  -MD -MP -MF "build/default/production/ledon.o.d" -MT "build/default/production/ledon.o.d" -MT build/default/production/ledon.o -o build/default/production/ledon.o ledon.S  -DXPRJ_default=default  -Wa,--defsym=__MPLAB_BUILD=1,-MD="build/default/production/ledon.o.asm.d"
"/usr/local/avr/bin/avr-gcc"  -mmcu=atmega16 -B "/home/jack/.mchp_packs/Microchip/ATmega_DFP/3.0.158/gcc/dev/atmega16"  -D__ATmega16__  -Wl,-Map="dist/default/production/ledon.X.production.map"    -o dist/default/production/ledon.X.production.elf build/default/production/ledon.o      -DXPRJ_default=default    -Wl,--defsym=__MPLAB_BUILD=1 -Wl,--gc-sections -Wl,--start-group  -Wl,-lm -Wl,--end-group 
"/usr/local/avr/bin"/avr-objcopy -O ihex "dist/default/production/ledon.X.production.elf" "dist/default/production/ledon.X.production.hex"
make[2]: Leaving directory '/home/jack/work/avrasm/ledon.X'
make[1]: Leaving directory '/home/jack/work/avrasm/ledon.X'

when I go to the IDE tab Window>Target Memery Views>Program Memery the disassembler codes does not as imagine

       Line    Address      Opcode     Label     DisAssy   
       1  0000         940C                JMP 0x2A      
       2  0001         0000                NOP           
       3  0002         940C                JMP 0x34      
       4  0003         0000                NOP           
       5  0004         940C                JMP 0x34      
       6  0005         0000                NOP           
       7  0006         940C                JMP 0x34      
       8  0007         0000                NOP           
       9  0008         940C                JMP 0x34      
      10  0009         0000                NOP           
      11  000A         940C                JMP 0x34      
      12  000B         0000                NOP           
      13  000C         940C                JMP 0x34      
      14  000D         0000                NOP           
      15  000E         940C                JMP 0x34      
      16  000F         0000                NOP           
      17  0010         940C                JMP 0x34      
      18  0011         0000                NOP           
      19  0012         940C                JMP 0x34      
      20  0013         0000                NOP           
      21  0014         940C                JMP 0x34      
      22  0015         0000                NOP           
      23  0016         940C                JMP 0x34      
      24  0017         0000                NOP           
      25  0018         940C                JMP 0x34      
      26  0019         0000                NOP           
      27  001A         940C                JMP 0x34      
      28  001B         0000                NOP           
      29  001C         940C                JMP 0x34      
      30  001D         0000                NOP           
      31  001E         940C                JMP 0x34      
      32  001F         0034                              
      33  0020         940C                JMP 0x34      
                                                         
      35  0022         940C                JMP 0x34      
                                                         
      37  0024         940C                JMP 0x34      
                                                         
      39  0026         940C                JMP 0x34      
                                                         
      41  0028         940C                JMP 0x34      
                                                         
      43  002A         2411                EOR R1, R1    
      44  002B         BE1F                OUT 0x3F, R1  
      45  002C         E5CF                LDI R28, 0x5F
      46  002D         E0D4                LDI R29, 0x04
      47  002E         BFDE                OUT 0x3E, R29
      48  002F         BFCD                OUT 0x3D, R28
      49  0030         940E                CALL 0x0      
                                                         
      51  0032         940C                JMP 0x36      
                                                         
      53  0034         940C                JMP 0x0       
                                                         
      55  0036         94F8                CLI           
      56  0037         CFFF                RJMP 0x37     
      57  0038         FFFF                              
      ...

it seems that the compiler added  many instructions automatically (colored red)

I know is the gcc auto init the device,but i do not need this.

 

I do not need compiler to do this for me,I want define vector routine or each line of machine codes to be exactly be controled ,what shoud I do?

Need some options to give gcc or gas?Please help.

This topic has a solution.
Last Edited: Thu. Jun 2, 2022 - 04:40 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why not use AVRASM?  It lets you do all the typing---exactly the way you want it.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:

Why not use AVRASM?  It lets you do all the typing---exactly the way you want it.

avrasm do not have Linux version and MPLAB only surpport it on windows. 

I think  it is not the problem of gcc,if you use them gas to compile the asm file ,everything is ok,but use gcc or gcc ,XC8 in MPLAB,it goes wrong.

There must be someway

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

smurfjack wrote:

avrasm do not have Linux version


https://www.avrfreaks.net/commen...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
.org 0

 

".org" does not work the same way in the gnu assembler that it does in AVRASM, because the gnu assembler ALWAYS plans to do relocation during linking, usually inserting startup code as well.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

smurfjack wrote:
Need some options to give gcc or gas?Please help.

 

You should brush up your internet search skills: I searched for avr-gcc no c runtime and found these amongst several other similar links:

 

https://quanttype.net/posts/2014...

https://avr-gcc-list.nongnu.nark...

 

Let us know what worked.

Last Edited: Wed. Jun 1, 2022 - 10:01 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

smurfjack wrote:

I know is the gcc auto init the device,but i do not need this.

 

I do not need compiler to do this for me,I want define vector routine or each line of machine codes to be exactly be controled ,what shoud I do?

Need some options to give gcc or gas?Please help.

Add  -Wl -nostartfiles  to the compiler options as a start.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

je_ruud wrote:

smurfjack wrote:

avrasm do not have Linux version


https://www.avrfreaks.net/commen...

Thank you very much!That's what I am searching for months!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ccrause wrote:
  -Wl -nostartfiles

I have add this options to gcc,then the compiler added the red initial codes again

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrasm is recognized by the MPLABX,and can successly compile on LInux,but can not debug nor flash

the out put of atmel-ICE:


*****************************************************

Currently loaded versions:
Application version...........1.42.161 (0x01.0x2a.0xa1)
Tool pack version .............1.4.490
Target voltage detected

Calculating memory ranges for operation...

Only config settings that have been explicitly declared in your code will be programmed.	

Erasing...

The following memory area(s) will be programmed:
program memory: start address = 0x0, end address = 0x3f

Programming complete

the output of  debugger:

Launching
An error occurred in the debugger.  Terminating the debug session.
com.microchip.mplab.mdbcore.debugger.DebugException
Programming target
User program running
INFO: Generic setting "Maintain active connection to hardware tool" is enabled and is not supported for the current device.
User program finished

 

Last Edited: Wed. Jun 1, 2022 - 04:08 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"Dare to be naïve." - Buckminster Fuller

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I see you ignored by links in #6 where you have learned that  -Wl,-nostartfiles on its own isn't enough.

 

You need -Wl,-nostartfiles,-nodefaultlibs

 

Also you have a bug in your .S

.section text   // NO
.section .text  // YES

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

N.Winterbottom wrote:

I see you ignored by links in #6 where you have learned that  -Wl,-nostartfiles on its own isn't enough.

 

You need -Wl,-nostartfiles,-nodefaultlibs

 

Also you have a bug in your .S

.section text   // NO
.section .text  // YES

 

yes,I corrected the bug

But ,gcc with -Wl,-nostartfiles,-nodefaultlibs also product much useless instructions like "nop" and initial instructions

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

westfw wrote:

.org 0

 

".org" does not work the same way in the gnu assembler that it does in AVRASM, because the gnu assembler ALWAYS plans to do relocation during linking, usually inserting startup code as well.

1.yes,but when dirctly use gnu as,it works,gnu as can convert every line you write in the asm file to instructions,without initial instructions

but gnc c compiler does not

2.then what shoud I do if I want to use .org to specific the code address

Last Edited: Wed. Jun 1, 2022 - 04:12 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

To org to an address put the thing you want to position in a named section then either using the linker script or - section-start to position the section to an absolute location. The default linker script effectively does this to guarantee .vectors is at the reset/IVT position. But apart from the IVT why do you need absolute positioning anyway? Write relative code and let the linker position everything for you (like it does for a C program).

 

BTW on Linux I bet your repo has avra. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

.org works, but is relative to the section you are in- .text in this case, which happens to start at address 0 as specified in the linker file (which can be changed). You are still dealing with the gcc toolchain including the linker, so will have to understand how the linker works.

 

MPLABX has project options in the Avr GCC global options category, where you can check some boxes in the avr-ld general options such as- no startup files, no default libs, or no std libs (covers both). You may also need to turn off the ld optimization of garbage collecting unused sections, as for instance main is no longer 'called' by anyone and the linker may remove it.

 

While you are in the options, under Avr GCC Global Options, in the option category pulldown, output files, check the option to generate an lss file so you can see the asm output in a better way.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

smurfjack wrote:
But ,gcc with -Wl,-nostartfiles,-nodefaultlibs also product much useless instructions like "nop" and initial instructions

Then your Makefile (if that's what you're using) doesn't pass those flags onto the LINK phase.

 

I've just built your file and get this tiny listing: (This is the  .lss file curtvm mentioned)

 


freak.elf:     file format elf32-avr

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .data         00000000  00800100  00000002  00000076  2**0
                  CONTENTS, ALLOC, LOAD, DATA
  1 .text         00000002  00000000  00000000  00000074  2**1
                  CONTENTS, ALLOC, LOAD, READONLY, CODE

Disassembly of section .text:

00000000 <__ctors_end>:
   0:	ff cf       	rjmp	.-2      	; 0x0 <__ctors_end>

 

Last Edited: Wed. Jun 1, 2022 - 08:09 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

gcc with -Wl,-nostartfiles

"nostartfiles" seems to be a gcc command, rather than an ld command, so you want:

 

avr-gcc -mmcu=atmega328 -nostartfiles foo.S

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It does not make any difference.maybe they said right,gcc make some initial codes to the S,then the linker change the default address of your instructions.May be ,there is no way to get hex codes as you want except use gcc-as or avrasm avara directly.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

smurfjack wrote:
May be ,there is no way to get hex codes as you want

That must make me a magician then.

Then again I wrote/modified a Makefile so I have more control over the build than perhaps an IDE lets you have.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I just read this with incredulity. Using avr-as "bare" so there's no CRT and no requirement for a "main:" really is as simple as -nostartfiles. If the IDE is "getting in the way" and doing something you don't yet recognise to defeat the effect of -nostartfiles then put the IDE off to one side for a while and just run the build tools from the command line. You should be able to do the entire build (assemble/link) in one line.

 

The next step up would be an Mfile generated Makefile. When you are happy with that solution then switch back to the IDE and see if you can spot what it's doing differently. 

 

Or just "sudo apt-get install avra" and take it from there. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

now,I can debug on Linux with MPLABX and avrasm2 .the reason debugger can not run just because the development board I made is designed for usb virtualization with atmega16,so the usb sometimes corrupt with the atmel-ICE usb port.

Thank you all!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

I just read this with incredulity. Using avr-as "bare" so there's no CRT and no requirement for a "main:" really is as simple as -nostartfiles. If the IDE is "getting in the way" and doing something you don't yet recognise to defeat the effect of -nostartfiles then put the IDE off to one side for a while and just run the build tools from the command line. You should be able to do the entire build (assemble/link) in one line.

 

The next step up would be an Mfile generated Makefile. When you are happy with that solution then switch back to the IDE and see if you can spot what it's doing differently. 

 

Or just "sudo apt-get install avra" and take it from there. 

yes avr-as can do everything as espected,but avr-gcc can not,I think,'cause is a c compiler,and gcc have to convert your .S file to .s temply,may be in this way ,someting like "nop" and stack pointer init codes were added.Even you use gcc -c -nostartfiles ...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

You all are so proffesional,and help me understand much more for avr ,thanks!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

'cause I am simulating USB with 2 PINs of atmega16,something like VUSB,so,timing is very important,and even a nop cycle added automatically by gcc will damage the communication proc.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't follow your logic regarding avr-gcc versus avr-as? avr-gcc is not a compiler or an assembler or a linker or anything like that. It is simply a "compiler driver" it chooses to run a preprocessor or a compiler or an assembler or a linker depending on circumstances and invocation. For example if you pass it a .c file (without -c) it will run the preprocessor, then the compiler, then the assembler, then the linker. If you pass it a .s file it'll just run assembler then linker. So there's little difference between invoking the avr-as directly or having it run as a result of passing some assembler to the compiler driver. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes,the gcc act as a drive.

The IDE like MPLABX use makefile to build the project of a single-file asm.and the out put as follows:

CLEAN SUCCESSFUL (total time: 51ms)
make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
make[1]: Entering directory '/home/jack/work/avrasm/ledon.X'
make  -f nbproject/Makefile-default.mk dist/default/production/ledon.X.production.hex
make[2]: Entering directory '/home/jack/work/avrasm/ledon.X'
"/usr/local/avr/bin/avr-gcc"  -mmcu=atmega16 -I "/home/jack/.mchp_packs/Microchip/ATmega_DFP/3.0.158/include" -B "/home/jack/.mchp_packs/Microchip/ATmega_DFP/3.0.158/gcc/dev/atmega16"  -x assembler-with-cpp -c -D__ATmega16__  -MD -MP -MF "build/default/production/ledon.o.d" -MT "build/default/production/ledon.o.d" -MT build/default/production/ledon.o -o build/default/production/ledon.o ledon.S  -DXPRJ_default=default  -Wa,--defsym=__MPLAB_BUILD=1,-MD="build/default/production/ledon.o.asm.d"
"/usr/local/avr/bin/avr-gcc"  -mmcu=atmega16 -B "/home/jack/.mchp_packs/Microchip/ATmega_DFP/3.0.158/gcc/dev/atmega16"  -D__ATmega16__  -Wl,-Map="dist/default/production/ledon.X.production.map"    -o dist/default/production/ledon.X.production.elf build/default/production/ledon.o      -DXPRJ_default=default    -Wl,--defsym=__MPLAB_BUILD=1 -Wl,--gc-sections -Wl,--start-group  -Wl,-lm -Wl,--end-group
"/usr/local/avr/bin"/avr-objcopy -O ihex "dist/default/production/ledon.X.production.elf" "dist/default/production/ledon.X.production.hex"
make[2]: Leaving directory '/home/jack/work/avrasm/ledon.X'
make[1]: Leaving directory '/home/jack/work/avrasm/ledon.X'

it use gcc twice

first,it use gcc to make object file from asm file
 

avr-gcc" -mmcu=atmega16 -I "/home/jack/.mchp_packs/Microchip/ATmega_DFP/3.0.158/include" -B "/home/jack/.mchp_packs/Microchip/ATmega_DFP/3.0.158/gcc/dev/atmega16" -x assembler-with-cpp -c -D__ATmega16__ -MD -MP -MF "build/default/production/ledon.o.d" -MT "build/default/production/ledon.o.d" -MT build/default/production/ledon.o -o build/default/production/ledon.o ledon.S -DXPRJ_default=default -Wa,--defsym=__MPLAB_BUILD=1,-MD="build/default/production/ledon.o.asm.d"

 

second,it use gcc to make elf file from object file

"/usr/local/avr/bin/avr-gcc" -mmcu=atmega16 -B "/home/jack/.mchp_packs/Microchip/ATmega_DFP/3.0.158/gcc/dev/atmega16" -D__ATmega16__ -Wl,-Map="dist/default/production/ledon.X.production.map" -o dist/default/production/ledon.X.production.elf build/default/production/ledon.o -DXPRJ_default=default -Wl,--defsym=__MPLAB_BUILD=1 -Wl,--gc-sections -Wl,--start-group -Wl,-lm -Wl,--end-group 

 

finaly,it use objcopy to make the hex file

"/usr/local/avr/bin"/avr-objcopy -O ihex "dist/default/production/ledon.X.production.elf" "dist/default/production/ledon.X.production.hex"

 

even I add -nostartfile (to 1st step 2nd step or 1st and 2nd step)by modifying the makefiles auto generated by the IDE under folder of "nbproject",the final result is polluted by nop or stack init instructions.

 

 

I wonder in which way I can get the pure final hex file without nop instructions etc. by modifying the command of MPLABX makefiles.

 

the makefiles structure as follow:

           --projectname.X----

                          |                       

           ---projectname.X---                    ledon.S                  Makefile

                           |

                 ---nbproject----

                            |

configurations.xml    Makefile-default,mk     Makefile-genesis.properties    Makefile-impl.mk     Makefile-local-default.mk     Makefile-variables.mk   project.xml                      

Last Edited: Thu. Jun 2, 2022 - 01:19 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

smurfjack wrote:

second,it use gcc to make elf file from object file

"/usr/local/avr/bin/avr-gcc" -mmcu=atmega16 -B "/home/jack/.mchp_packs/Microchip/ATmega_DFP/3.0.158/gcc/dev/atmega16" -D__ATmega16__ -Wl,-Map="dist/default/production/ledon.X.production.map" -o dist/default/production/ledon.X.production.elf build/default/production/ledon.o -DXPRJ_default=default -Wl,--defsym=__MPLAB_BUILD=1 -Wl,--gc-sections -Wl,--start-group -Wl,-lm -Wl,--end-group 

 

In #17 I suspected that -Wl,-nostartfiles,-nodefaultlibs wasn't being passed onto the LINK phase. This indeed seems to be the case.

 

My MPLABX enabled computer is unavailable at the moment (Platinum Jubilee Bank Holiday) so can't fix this for you just yet.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

N.Winterbottom wrote:

smurfjack wrote:

second,it use gcc to make elf file from object file

"/usr/local/avr/bin/avr-gcc" -mmcu=atmega16 -B "/home/jack/.mchp_packs/Microchip/ATmega_DFP/3.0.158/gcc/dev/atmega16" -D__ATmega16__ -Wl,-Map="dist/default/production/ledon.X.production.map" -o dist/default/production/ledon.X.production.elf build/default/production/ledon.o -DXPRJ_default=default -Wl,--defsym=__MPLAB_BUILD=1 -Wl,--gc-sections -Wl,--start-group -Wl,-lm -Wl,--end-group 

 

In #17 I suspected that -Wl,-nostartfiles,-nodefaultlibs wasn't being passed onto the LINK phase. This indeed seems to be the case.

 

 

 

 

if add -nostartfiles -nodefaultlibs to 2nd step

"/usr/local/avr/bin/avr-gcc"  -mmcu=atmega16 -B "/home/jack/.mchp_packs/Microchip/ATmega_DFP/3.0.158/gcc/dev/atmega16"  -D__ATmega16__  -Wl,-Map="dist/default/production/gccledon.X.production.map"    -o dist/default/production/gccledon.X.production.elf build/default/production/gccledon.o      -DXPRJ_default=default    -Wl,--defsym=__MPLAB_BUILD=1 -Wl,--gc-sections -Wl,--start-group  -Wl,-lm -Wl,--end-group -nostartfiles -nodefaultlibs 

 

My MPLABX enabled computer is unavailable at the moment (Platinum Jubilee Bank Holiday) so can't fix this for you just yet.

 

I modified as you told,when add -nostartfiles -nodefaultlibs to 2nd step as follow,no error

"/usr/local/avr/bin/avr-gcc"  -mmcu=atmega16 -B "/home/jack/.mchp_packs/Microchip/ATmega_DFP/3.0.158/gcc/dev/atmega16"  -D__ATmega16__  -Wl,-Map="dist/default/production/gccledon.X.production.map"    -o dist/default/production/gccledon.X.production.elf build/default/production/gccledon.o      -DXPRJ_default=default    -Wl,--defsym=__MPLAB_BUILD=1 -Wl,--gc-sections -Wl,--start-group  -Wl,-lm -Wl,--end-group -nostartfiles -nodefaultlibs 

but when upload hex to the device,errors


*****************************************************

Currently loaded versions:
Application version...........1.42.161 (0x01.0x2a.0xa1)
Tool pack version .............1.4.490
Target voltage detected

Calculating memory ranges for operation...
Programming did not complete.
MPLAB's memory is blank so no programming operation was attempted

 

 

 

if add to the 1st step,no errors,but the final hex instructions has nop and stack init instructions.

"/usr/local/avr/bin/avr-gcc"  -mmcu=atmega16 -I "/home/jack/.mchp_packs/Microchip/ATmega_DFP/3.0.158/include" -B "/home/jack/.mchp_packs/Microchip/ATmega_DFP/3.0.158/gcc/dev/atmega16"  -x assembler-with-cpp -c -D__ATmega16__  -MD -MP -MF "build/default/production/gccledon.o.d" -MT "build/default/production/gccledon.o.d" -MT build/default/production/gccledon.o -o build/default/production/gccledon.o gccledon.S  -DXPRJ_default=default  -Wa,--defsym=__MPLAB_BUILD=1,-MD="build/default/production/gccledon.o.asm.d"  -Wl,-nostartfiles,-nodefaultlibs

 

 

if add to the 1st and 2nd step at the same time

"/usr/local/avr/bin/avr-gcc"  -mmcu=atmega16 -I "/home/jack/.mchp_packs/Microchip/ATmega_DFP/3.0.158/include" -B "/home/jack/.mchp_packs/Microchip/ATmega_DFP/3.0.158/gcc/dev/atmega16"  -x assembler-with-cpp -c -D__ATmega16__  -MD -MP -MF "build/default/production/gccledon.o.d" -MT "build/default/production/gccledon.o.d" -MT build/default/production/gccledon.o -o build/default/production/gccledon.o gccledon.S  -DXPRJ_default=default  -Wa,--defsym=__MPLAB_BUILD=1,-MD="build/default/production/gccledon.o.asm.d"  -Wl,-nostartfiles,-nodefaultlibs
"/usr/local/avr/bin/avr-gcc"  -mmcu=atmega16 -B "/home/jack/.mchp_packs/Microchip/ATmega_DFP/3.0.158/gcc/dev/atmega16"  -D__ATmega16__  -Wl,-Map="dist/default/production/gccledon.X.production.map"    -o dist/default/production/gccledon.X.production.elf build/default/production/gccledon.o      -DXPRJ_default=default    -Wl,--defsym=__MPLAB_BUILD=1 -Wl,--gc-sections -Wl,--start-group  -Wl,-lm -Wl,--end-group -nostartfiles -nodefaultlibs
"/usr/local/avr/bin"/avr-objcopy -O ihex "dist/default/production/gccledon.X.production.elf" "dist/default/production/gccledon.X.production.hex"

no errors build,but errors when flashing to device


Calculating memory ranges for operation...
Programming did not complete.
MPLAB's memory is blank so no programming operation was attempted

 

 

 

-----------------------------------------------------------------------

I do this by editing the Makefile-default,mk file

the original content of Makefile-default,mk as follows:

#
# Generated Makefile - do not edit!
#
# Edit the Makefile in the project folder instead (../Makefile). Each target
# has a -pre and a -post target defined where you can add customized code.
#
# This makefile implements configuration specific macros and targets.


# Include project Makefile
ifeq "${IGNORE_LOCAL}" "TRUE"
# do not include local makefile. User is passing all local related variables already
else
include Makefile
# Include makefile containing local settings
ifeq "$(wildcard nbproject/Makefile-local-default.mk)" "nbproject/Makefile-local-default.mk"
include nbproject/Makefile-local-default.mk
endif
endif

# Environment
MKDIR=mkdir -p
RM=rm -f 
MV=mv 
CP=cp 

# Macros
CND_CONF=default
ifeq ($(TYPE_IMAGE), DEBUG_RUN)
IMAGE_TYPE=debug
OUTPUT_SUFFIX=elf
DEBUGGABLE_SUFFIX=elf
FINAL_IMAGE=${DISTDIR}/gccledon.X.${IMAGE_TYPE}.${OUTPUT_SUFFIX}
else
IMAGE_TYPE=production
OUTPUT_SUFFIX=hex
DEBUGGABLE_SUFFIX=elf
FINAL_IMAGE=${DISTDIR}/gccledon.X.${IMAGE_TYPE}.${OUTPUT_SUFFIX}
endif

ifeq ($(COMPARE_BUILD), true)
COMPARISON_BUILD=
else
COMPARISON_BUILD=
endif

ifdef SUB_IMAGE_ADDRESS

else
SUB_IMAGE_ADDRESS_COMMAND=
endif

# Object Directory
OBJECTDIR=build/${CND_CONF}/${IMAGE_TYPE}

# Distribution Directory
DISTDIR=dist/${CND_CONF}/${IMAGE_TYPE}

# Source Files Quoted if spaced
SOURCEFILES_QUOTED_IF_SPACED=gccledon.S

# Object Files Quoted if spaced
OBJECTFILES_QUOTED_IF_SPACED=${OBJECTDIR}/gccledon.o
POSSIBLE_DEPFILES=${OBJECTDIR}/gccledon.o.d

# Object Files
OBJECTFILES=${OBJECTDIR}/gccledon.o

# Source Files
SOURCEFILES=gccledon.S

# Pack Options 
PACK_COMPILER_OPTIONS=-I "${DFP_DIR}/include"
PACK_COMMON_OPTIONS=-B "${DFP_DIR}/gcc/dev/atmega16"



CFLAGS=
ASFLAGS=
LDLIBSOPTIONS=

############# Tool locations ##########################################
# If you copy a project from one host to another, the path where the  #
# compiler is installed may be different.                             #
# If you open this project with MPLAB X in the new host, this         #
# makefile will be regenerated and the paths will be corrected.       #
#######################################################################
# fixDeps replaces a bunch of sed/cat/printf statements that slow down the build
FIXDEPS=fixDeps

.build-conf:  ${BUILD_SUBPROJECTS}
ifneq ($(INFORMATION_MESSAGE), )
	@echo $(INFORMATION_MESSAGE)
endif
	${MAKE}  -f nbproject/Makefile-default.mk ${DISTDIR}/gccledon.X.${IMAGE_TYPE}.${OUTPUT_SUFFIX}

MP_PROCESSOR_OPTION=ATmega16
# ------------------------------------------------------------------------------------
# Rules for buildStep: assemble
ifeq ($(TYPE_IMAGE), DEBUG_RUN)
else
endif

# ------------------------------------------------------------------------------------
# Rules for buildStep: assembleWithPreprocess
ifeq ($(TYPE_IMAGE), DEBUG_RUN)
${OBJECTDIR}/gccledon.o: gccledon.S  .generated_files/flags/default/5964d2fd6a1995b039622cc491c93908d35680fd .generated_files/flags/default/da42defc1fe2108d5cab4e04f81e29bf2e01992d
	@${MKDIR} "${OBJECTDIR}" 
	@${RM} ${OBJECTDIR}/gccledon.o.d 
	@${RM} ${OBJECTDIR}/gccledon.o 
	@${RM} ${OBJECTDIR}/gccledon.o.ok ${OBJECTDIR}/gccledon.o.err 
	 ${MP_CC} $(MP_EXTRA_AS_PRE) -mmcu=atmega16 ${PACK_COMPILER_OPTIONS} ${PACK_COMMON_OPTIONS}  -DDEBUG  -x assembler-with-cpp -c -D__$(MP_PROCESSOR_OPTION)__  -MD -MP -MF "${OBJECTDIR}/gccledon.o.d" -MT "${OBJECTDIR}/gccledon.o.d" -MT ${OBJECTDIR}/gccledon.o -o ${OBJECTDIR}/gccledon.o gccledon.S  -DXPRJ_default=$(CND_CONF)  -Wa,--defsym=__MPLAB_BUILD=1$(MP_EXTRA_AS_POST),-MD="${OBJECTDIR}/gccledon.o.asm.d",--defsym=__ICD2RAM=1,--defsym=__MPLAB_DEBUG=1,--gdwarf-2,--defsym=__DEBUG=1
	
else
${OBJECTDIR}/gccledon.o: gccledon.S  .generated_files/flags/default/502e8ce5f8dbad568775aa2547b4f8ae45f2a9da .generated_files/flags/default/da42defc1fe2108d5cab4e04f81e29bf2e01992d
	@${MKDIR} "${OBJECTDIR}" 
	@${RM} ${OBJECTDIR}/gccledon.o.d 
	@${RM} ${OBJECTDIR}/gccledon.o 
	@${RM} ${OBJECTDIR}/gccledon.o.ok ${OBJECTDIR}/gccledon.o.err 
	 ${MP_CC} $(MP_EXTRA_AS_PRE) -mmcu=atmega16 ${PACK_COMPILER_OPTIONS} ${PACK_COMMON_OPTIONS}  -x assembler-with-cpp -c -D__$(MP_PROCESSOR_OPTION)__  -MD -MP -MF "${OBJECTDIR}/gccledon.o.d" -MT "${OBJECTDIR}/gccledon.o.d" -MT ${OBJECTDIR}/gccledon.o -o ${OBJECTDIR}/gccledon.o gccledon.S  -DXPRJ_default=$(CND_CONF)  -Wa,--defsym=__MPLAB_BUILD=1$(MP_EXTRA_AS_POST),-MD="${OBJECTDIR}/gccledon.o.asm.d"
	
endif

# ------------------------------------------------------------------------------------
# Rules for buildStep: compile
ifeq ($(TYPE_IMAGE), DEBUG_RUN)
else
endif

# ------------------------------------------------------------------------------------
# Rules for buildStep: compileCPP
ifeq ($(TYPE_IMAGE), DEBUG_RUN)
else
endif

# ------------------------------------------------------------------------------------
# Rules for buildStep: link
ifeq ($(TYPE_IMAGE), DEBUG_RUN)
${DISTDIR}/gccledon.X.${IMAGE_TYPE}.${OUTPUT_SUFFIX}: ${OBJECTFILES}  nbproject/Makefile-${CND_CONF}.mk    
	@${MKDIR} ${DISTDIR} 
	${MP_CC} $(MP_EXTRA_LD_PRE) -mmcu=atmega16 ${PACK_COMMON_OPTIONS}   -gdwarf-2 -D__$(MP_PROCESSOR_OPTION)__  -Wl,-Map="${DISTDIR}/gccledon.X.${IMAGE_TYPE}.map"    -o ${DISTDIR}/gccledon.X.${IMAGE_TYPE}.${OUTPUT_SUFFIX} ${OBJECTFILES_QUOTED_IF_SPACED}      -DXPRJ_default=$(CND_CONF)  $(COMPARISON_BUILD)  -Wl,--defsym=__MPLAB_BUILD=1$(MP_EXTRA_LD_POST)$(MP_LINKER_FILE_OPTION),--defsym=__ICD2RAM=1,--defsym=__MPLAB_DEBUG=1,--defsym=__DEBUG=1 -Wl,--gc-sections -Wl,--start-group  -Wl,-lm -Wl,--end-group 
	
	
	
	
	
	
else
${DISTDIR}/gccledon.X.${IMAGE_TYPE}.${OUTPUT_SUFFIX}: ${OBJECTFILES}  nbproject/Makefile-${CND_CONF}.mk   
	@${MKDIR} ${DISTDIR} 
	${MP_CC} $(MP_EXTRA_LD_PRE) -mmcu=atmega16 ${PACK_COMMON_OPTIONS}  -D__$(MP_PROCESSOR_OPTION)__  -Wl,-Map="${DISTDIR}/gccledon.X.${IMAGE_TYPE}.map"    -o ${DISTDIR}/gccledon.X.${IMAGE_TYPE}.${DEBUGGABLE_SUFFIX} ${OBJECTFILES_QUOTED_IF_SPACED}      -DXPRJ_default=$(CND_CONF)  $(COMPARISON_BUILD)  -Wl,--defsym=__MPLAB_BUILD=1$(MP_EXTRA_LD_POST)$(MP_LINKER_FILE_OPTION) -Wl,--gc-sections -Wl,--start-group  -Wl,-lm -Wl,--end-group -nostartfiles -nodefaultlibs 
	${MP_CC_DIR}/avr-objcopy -O ihex "${DISTDIR}/gccledon.X.${IMAGE_TYPE}.${DEBUGGABLE_SUFFIX}" "${DISTDIR}/gccledon.X.${IMAGE_TYPE}.hex"
	
	
	
	
	
	@echo Normalizing hex file
	@"/opt/microchip/mplabx/v6.00/mplab_platform/platform/../mplab_ide/modules/../../bin/hexmate" --edf="/opt/microchip/mplabx/v6.00/mplab_platform/platform/../mplab_ide/modules/../../dat/en_msgs.txt" ${DISTDIR}/gccledon.X.${IMAGE_TYPE}.hex -o${DISTDIR}/gccledon.X.${IMAGE_TYPE}.hex

endif


# Subprojects
.build-subprojects:


# Subprojects
.clean-subprojects:

# Clean Targets
.clean-conf: ${CLEAN_SUBPROJECTS}
	${RM} -r ${OBJECTDIR}
	${RM} -r ${DISTDIR}

# Enable dependency checking
.dep.inc: .depcheck-impl

DEPFILES=$(shell "${PATH_TO_IDE_BIN}"mplabwildcard ${POSSIBLE_DEPFILES})
ifneq (${DEPFILES},)
include ${DEPFILES}
endif

 

Last Edited: Thu. Jun 2, 2022 - 02:41 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

currently,I can write pure avr asm codes with MPLAB IDE & avrasm2 linux version,flashing,debugging,everything is ok.(copy avrasm2 to /usr/local/avr/bin   and go to MPLABX build options tab,click "scan" button,the IDE will find avrasm2 automatically.)

If avr-gcc and MPLAB can not do these thing,I think I should give up gcc & MPLAB asm solution and focus on MPLAB & avrasm2 solution.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


I've only ever used MPLABX for about 2 minutes on another machine but I figured it can't be that difficult t create a bare metal Asm project so I just installed it on this Win11 machine. I use:

 

 

then create a standalone project..

 

 

then make it a GCC project...

 

 

 

I then need a new source file so I use "New" on this context menu..

 

 

From the following list I selected "other" and then..

 

 

(are they sure about .s not .S ??)

 

I then wrote a quick test program...

 

 

If I try to build "as is" then it fails because the CRT wants to link to a non-existent "main:"...

 

 

I hate (I mean absolutely detest!) the MPLABX user interface but I finally found the option for no start files hiding here...

 

 

when I build now...

 

It builds successfully without error. It can't be linking the CRT because if it did that would be looking for a non-existent "main:" global. So this must have succeeded but to verify..

 

First I found the .hex file to be completely empty and that is because of:

 

 

So, as you see here, I turned it off (again how on earth are people supposed to find where they buried these options??).

 

Now I can't work out how to switch on LSS in this truly dire excuse for an IDE but I am able to run a disassembler at the command line:

 

 

QUOD ERAT DEMONSTRANDUM!!

 

It really IS that simple. You seem to be making pretty heavy weather of something so simple. I will grant you that MPLABX really is the biggest heap of steaming shite and it simply gets in the way of operating the build tools so that does make things more challenging but "-nostartfiles" really was the answer everyone has been saying all along!

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

PS the reason I switched off garbage collection is that code that doesn't seem to be called is garbage collected. In a normal program that doesn't matter because there's a call hierarchy starting at main() but if you turn off the CRT there's no call to main then the other functions. So garbage collect discards EVERYTHING. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Now I can't work out how to switch on LSS

You spelled out post #16 in detail, and in that post you will also find where to turn on lss listings although with a little poking around you would find it.

 

 

I do this by editing the Makefile-default,mk file

If you are editing a makefile in an ide that auto generates them, and it says 'do not edit!', its probably a good idea to heed the warning. For one thing, your edits most likely do not survive if any changes take place elsewhere as a new makefile will be generated.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks clawson,I performed the settings as you told:

 

build succeed:

 

 

program device :

 

read program memery: many nops and the address of label "reset" is not 0x38

 

but when use cmdline avr-objdump,the ihex codes seems right:

 

the development board's LED is on correctly.

 

This is a bug of MPLAB IDE's Program memory ? I think this function is reading memory from device ,why the result is not correct?

Last Edited: Fri. Jun 3, 2022 - 01:28 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I do further test with avrasm2:

first I created a new  project named "ledon" with avrasm2  (without programing device),and read the current program on the pcb board:

it certainly is the previous gcc project(gccledon) program on the board.

 

then build and program device,I read the program on PCB board again:

every thing is ok in the IDE.but....but.....the cmdline avr-objdump give worng output of the ihex file(the main address changed to 0x70) ...!!why

so ,it is not the bug of MPLAB,both led on the PCB board turn on correctly.

Last Edited: Fri. Jun 3, 2022 - 02:18 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You are seeing word addresses in the case of the program memory view (notice the address contains a word value, 2 bytes), where all gcc tools deals with byte addresses.

 

Go back to post #16 where is spells out how to turn on lss listings, which will give you byte addresses. Use the lss listing to see what you have, not the program memory view or the debugging output disassembly listing.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

smurfjack wrote:
'cause I am simulating USB with 2 PINs of atmega16,something like VUSB,so,timing is very important,and even a nop cycle added automatically by gcc will damage the communication proc.

I believe we've all been wasting our time here.

 

OP has written one line of ASM and noticed the addition of a vector table and c_startup code. He has wrongly extrapolated that to a project the size of V-USB (itself a C project with ASM sections for cycle perfect timing control) and decided his timing will be affected.

 

Of course his cycle timing if written perfect will remain perfect and we should have corrected him earlier before this got a bit out-of-hand.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

To be honest I think having the CRT is an advantage not a disadvantage. Unless you really can't afford the space taken by the IVT then it's nice that this is provided in an easily hookable way (see user manual example). Similarly if you want "global variables" and especially if you want initialised global variables then the system providing _do_copy_data delivers a ready made solution on a plate. In fact you can just add a .c to the project just with the global variables defined and initialized.