(question) Uploading and running ARM machine code?

Status
Not open for further replies.

uptide

Member
I'm looking through the upload instructions (https://www.pjrc.com/teensy/loader_mac.html) and through the example files. There are three kinds of files I'm seeing, .hex, .elf, .ino.

Where can I go or what can I read that tells me what each of these files do?

My goal is to upload ARM machine code into non-volatile memory and have it be executed. (This is what I'm doing for fun, uploading and executing machine code; I'm not trying to make the machine do anything else but execute instructions as close to the processor as possible.)

I have not bought a board yet, I'm trying to find something that can do this. So if Teensy can't do this it would be cool if someone knew of a board that might let me do this.

EDIT: It would also be nice to know where to get an ARM assembler at, Google is failing me :[
 
Last edited:
You really need a Teensy board to do anything meaningful with these files.

My goal is to upload ARM machine code into non-volatile memory and have it be executed.

That's *exactly* what Teensy Loader does when you use it with a real Teensy board.

I have not bought a board yet, I'm trying to find something that can do this.

You need to buy a Teensy. There is no simulator or non-hardware approach supported.

Where can I go or what can I read that tells me what each of these files do?

The .ino files are the source code used with Arduino. You can open these with the Arduino software.

The .elf is the final result from the compiler which includes lots of extra info. Must documentation is available about the ELF format. Teensy (and most embedded programming) uses only a subset of the ELF features.

http://www.linuxjournal.com/article/1059
https://en.wikipedia.org/wiki/Executable_and_Linkable_Format

ELF is a very complex format with tons of info. To look inside ELF files, use the "readelf" program, which is standard on Linux when you have the gcc build tools installed. In a terminal, run "readelf -H" for a summary of the many options.

The .hex file is a format called "intel hex" (use those words with Google for documentation) which contains only the bytes actually programmed into the flash memory. The encoding is plain ascii, so you can inspect these files by opening with any text editor.

It would also be nice to know where to get an ARM assembler at

There is an assembler built into the toolchain. There are 2 ways to use it.

1: You can create .S assembler files. Arduino will compile and use them in your project. For example, here's one in the core library:

https://github.com/PaulStoffregen/cores/blob/master/teensy3/memset.S

Of course, functions called from other code need to follow the ARM ABI, which specifies details like R0 to R3 are used for inputs, R0 for output, and which other registers must be saved versus which you can overwrite without saving on the stack.

In principle, you could create an entire project using only .S files, including re-implementing the tremendous amount of code which already exists for USB, serial, I2C, etc. In practice, such a project could be expected to take many man-years of work.

2: You can use inline assembly within C and C++ files. This approach integrates assembly directly into surrounding C/C++ code, so it is more complex and difficult. Here's an example from the audio library.

https://github.com/PaulStoffregen/Audio/blob/master/utility/dspinst.h

With inline asm, generally you use macros for the registers and the compiler's register allocator will choose the best registers based on all the surrounding C/C++ code. While there are many ways to do this, the most common involves inline functions to package up all the assembly complexity.
 
Here are some prior threads about coding in assembly.

https://forum.pjrc.com/threads/25317-Assembly-coding-for-Teensy3-1

https://forum.pjrc.com/threads/27938-Using-Assembler-with-Arduino-and-Teensy3-1-or-Teensy2

You might also consider whether assembly is even worthwhile. For example, in this thread a tremendous amount of effort was spent optimizing the base-10 printing code on Teensy LC.

https://forum.pjrc.com/threads/28932-LC-is-10-9-times-slower-than-T3-1?p=76072&viewfull=1#post76072

The "Stimmer" algorithm came from a lot of previous work on 8 bit AVR, where assembly did greatly outperform C code. But on Teensy LC, it turns out with the algorithm expressed the best way in C, the compiler beat both me and mlu by about 10%... and I personally have about 30 years of experience with assembly language programming, though mostly on 8 bit processors, starting with the 6502 in 1987, and many large projects on 8051, PIC & AVR through the 1990 and early 2000s.
 
@PaulStoffregen
My 'project' is to just code in assembly/opcodes. I did not know if I could upload this assembled code to the Teensy and was basing my buy decision off if I was able to. I have ordered one now! Thank you very much! You didn't mention which toolchain you are using, but I found this: https://developer.arm.com/open-source/gnu-toolchain/gnu-rm/downloads

It's impressive that you have 30 years of assembly experience, but I am going to ignore your suggestion for using C. I have no desire to know C, I have already learned and used C#, python, perl, and other languages that are far from the actual iron. (If I'm going to abstract I might as well go all the way)
 
You didn't mention which toolchain you are using

The toolchain is gcc 5.4, which is automatically installed when you use Arduino 1.8.5 with Teensyduino 1.40.

https://www.pjrc.com/teensy/td_download.html

Even if you never intend to use C/C++ and Arduino, I highly recommend at least using it once to upload the LED blink example or some other simple program, so you know the Teensy hardware is fully functional and able to upload. When things don't work (which will certainly be the case if learning assembly), having a working system as a fallback to verify the hardware is essential.

When using Arduino, click File > Preferences and click the option to turn on verbose info while compiling. Then when you click Verify or Upload, Arduino will show you all the exact toolchain commands it uses. These can help you to get started.

There is also a sample Makefile in Arduino's hardware/teensy/avr/cores/teensy3 folder. If you choose to run the toolchain using make, that might help.

Remember, if you believe something has gone wrong, the only "supported" path is using the Arduino IDE, or using the stand-alone Teensy Loader to program known-good HEX files to the board. Test your board with the known-good software if your ASM does not work.
 
Last edited:
Status
Not open for further replies.
Back
Top