Converting C to assembly code

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

I'm trying to convert a c file to assembly language using "gcc -g FileName" however when I write that command in the terminal on linux it says "gcc: error: FileName: No such file or directory. I saved the file in the desktop

PacMan

Last Edited: Sun. Dec 10, 2017 - 12:23 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

what has this got to do with Arduino? This is a Linux question.

If your file is on the desktop and your terminal is in the default user directory, then you need to specify where the file is
Eg: gcc -g ~/Desktop/filename

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

I was going to ask a different question but forgot to change it, sorry.

When I write that command it gives me the a fatal error: avr/io.h: No such file or directory. Any ideas whats wrong?

The '#include <avr/io.h>' in the file allows me to get the symbol definitions for ATmega328p

PacMan

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

gcc isn’t going to work for avr is it? It generates code for the machine you’re running on.
You want avrgcc
Do you have it installed?

Somehow i don’t think -g is what you want. -S is more likely. I think you’ll need to have a bit more magic on the command line to specify the avr chip, paths, includes etc. There’s plenty of examples on avrfreaks.

Last Edited: Sat. Dec 9, 2017 - 10:45 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, however when I write the command I get lots of errors.

Are you good with C and assembly language?

PacMan

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

Brumack wrote:
Are you good with C and assembly language?

In case Kartman decides to be humble and not answer that, I can vouch for him. Yes, he certainly is.

 

But that's not the problem here. The problem is that you need to be better at asking a specific and well structured question.

 

Brumack wrote:
When I write that command it gives me the a fatal error: avr/io.h

You've already got an explanation for that, but in case it wasnt clear enough, here's a longer version:

 

The command gcc runs a compiler that reads C code and turns it into executable code for the computer where the command was issued. I.e. if you run the gcc command on a Windows macgine you're activating a compiler that makes assembler (and eventually binary machine code) to be run on that windows machine. That compiler knows nothing at all about AVRs, and specifically it knows nothing at all about a file avr/io.h.

 

You want to compile C code into assembler for an AVR microcontroller. For that you need to give the command avr-gcc rather than just gcc. For this to work the specific avr-gcc compiler needs to be installed on the machine where you issue that command. The gcc compiler and the avr-gcc compiler are separate compilers. Just having the gcc compiler installed will not give you the opportunity to use the avr-gcc compiler. That avr-gcc compiler needs to be installed separately.

 

Here are some initial questions that we need to have answers to in order to help you along:

  • Do you have avr-gcc installed?
  • What operating system are you running on the computer where you want to do the compilation? (Windows? Linux?)

 

And I also have this very specific question: Is this related to school work? Have you got an assignment where you need to hand in assembler code?

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

Yes, this is for an assignment.

I had to create a Morse code generator in AVR assembler language  however I created it in C since I know it better and thought I could just convert it after however I'm having a lot of difficulties doing so. My current operating system is Linux.

PacMan

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

Oh, the Morse code assignment.. (We see the Traffic Lights assignment more often than the Morse code assignment so that's kind'of "fresh"..)

 

With the sarcasm away ;-) , here's some serious advice:

 

 

  1. Your teacher will, if (s)he is only the least competent, spot that the assembler is generated by a compiler within a few seconds. If you decide to try to have the avr-gcc compiler generate the assembler for you I strongly recommend asking the teacher first if handing in such code is acceptable or if you'll "flunk".
  2. There are at least two AVR assembler dialects that are extensively used. One is "Atmel Assembler" ("Assembler2", to be exact). The other is the "GNU Assembler" (GAS) for AVRs. The avr-gcc compiler generates the latter dialect. If your assignment says to hand in Atmel Assembler, I strongly recommend asking your teacher on beforehand if GAS dialect is acceptable (if it isn't then you can port GAS code to Atmel Assembler dialect by hand, but given your stated experience with assembler this might not be trivial for you).
  3. Your teacher probably gave you the assignment for you to learn some AVR assembler, so why skip trying to cut corners and take that learning challenge on? After all, that is what school is for..

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Sat. Dec 9, 2017 - 11:54 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That's a fair point but I had many deadlines coming up and so I went for the easier path to ensure I can finish all my work.

My assignment says to hand in Atmel Assembler, could you possible walk me through porting GAS code to Atmel assembler? 

Thanks for being responsive!

PacMan

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

Brumack wrote:
My assignment says to hand in Atmel Assembler, could you possible walk me through porting GAS code to Atmel assembler? 

Sorry, but no. That is not something that I can do with an "easy walk-through".

 

A bigger complication is this: If "assignment says to hand in Atmel Assembler", means "hand in Atmel Assembler2 dialect" (not entirely clear) then as far as I know, the Atmel Assembler is not available for Linux. It's Windows only if my memory serves me.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

Linux may have avra which is a clone of the Atmel assembler. 

 

But if you want to start with C get avr-gcc and invoke that with -save-temps then study the .s file which is in GNU avr-as format. You can use the opcode but the directives need to be converted from avr-as to avra/Atmel format

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

clawson wrote:
the directives need to be converted from avr-as to avra/Atmel format

..and macros, like the ones for getting hi/lo bytes of words?

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

It seems an excellent strategy to start with a language you are familiar with.

Test and debug until you have it working 100%.

 

Then translate into the Assembler syntax required by your Teacher.

Test the new ASM version and compare with the working C program.

 

By all means ask specific questions about the high level program or the translation.

 

As Johan has pointed out in #8,   your teacher will spot the "build method" that you have used.

If your program is working 100% correctly,   she will not be able to complain.

 

If you get 90% success in C and succeed in the syntax translation,   you have still failed.

If you get 90% success in C and fail in the syntax translation,   you have failed miserably.

If you attempt in ASM and get 50% success,  your teacher will still love you.

 

Personally,   I think that the C to ASM exercise is far more worthwhile than straight ASM projects.

If you do the C to ASM exercise and optimise some critical sequences (successfully),   employers will really love you.

 

Seriously,  neither Morse code nor Traffic Lights are critical for speed or size.   So there is little point in hand-tweaking the C generated code.

 

David.

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

JohanEkdahl wrote:
Oh, the Morse code assignment.. 

only a week ago: https://www.avrfreaks.net/forum/a... - from the same class?

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...