Arduino Uno R3 and TL866A - Page 1
Forum section: electronics / beginners

author: Electro Fan
author_rank: Super Contributor
timestamp: October 13, 2013, 05:27:56 am
content: Ok, a brief report and then a question:I made a Arduino Uno Hello World breadboard project and programmed it with some (slightly more creative) text using the Arduino IDE (on my PC).Next I bought a spare ATMEGA328P and found I could read it on my new TL866A (not a lot to read but I was able to look around at what comes with a fresh ATMEGA328P using the MiniPro v5.91 software).I then moved the new spare AMEGA328P from the TL866A to the Arduino Uno socket (as a replacement for the original ATMEGA). I then uploaded the sketch text from the PC to the spare ATMEGA328P with the file I had created with the Arduino IDE.I then put the spare ATMEGA328P back into the TL866A and found that using the MiniPro software I could read the text I had created using the Arduino IDE.Next I found that I could overwrite the text I had written onto the ATMEGA by overwriting the corresponding hex characters using the MiniPro software. I then moved the ATMEGA from the TL866A back to the Arduino board and it displayed the text on the LCD showing the character changes made from within the MiniPro software while it was on the TL866A.So far, so good.The question is, what is the process for reading the newly written text on the ATMEGA back into a revised sketch using the PC IDE? I can connect the Arduino to the PC just fine with the USB cable and it seems like the IDE on the PC should allow me to download the new text (created in hex using the MiniPro software and the TL866A) into a sketch file using the IDE. Kind of "reverse compiled". I just haven't figured out how, yet...Thanks for any guidance/suggestions!

author: ataradov
author_rank: Super Contributor
timestamp: October 13, 2013, 05:56:30 am
content: What you want is positively impossible. After program compiled, it can not be easily reversed back to its source code.

author: Rick Law
author_rank: Super Contributor
timestamp: October 13, 2013, 06:06:43 am
content: You should be able to convert the machine codes back to assembly code (disassembly), but converting back to high level source (such as C/C++ which is what the Arduino IDE uses) is impossible - not the exact souirce anyway; I suppose one can guess what the source could be given the assembly code, and then try to find matching source that generate the given sequence.

author: Electro Fan
author_rank: Super Contributor
timestamp: October 13, 2013, 06:38:34 am
content: OK, thanks - guess it doesn't go in that direction.Just curious though, is there some fundamental problem that can't be solved or is it that there is no market (no need) for software to perform such a function and therefore it's just that no one has bothered to make such software?

author: MrAureliusR
author_rank: Unknown
timestamp: October 13, 2013, 06:45:13 am
content: It would be very useful - especially for software pirates. But its mostly that it's essentially impossible.Typed using Hacker's Keyboard for Android

author: pickle9000
author_rank: Super Contributor
timestamp: October 13, 2013, 06:48:56 am
content: Your looking for a disassembler, while you can find such things for older machine code (processors), a great deal is lost in the process. The assembler will optimize you original code for a particular device, it won't use remarks/comments or even variable names in some cases. Basically it can look very ugly when reverse engineered.

author: Unknown
author_rank: Unknown
timestamp: October 13, 2013, 06:52:59 am
content: Assembly to machine code is a one-to-one mapping, every assembly directive maps to one machine code opcode (with operands). So translating the other way around is also possible. Disassembling does not give you macros, labels or comments since these are not encoded in machine code.There are multiple ways to match a C statement like a for loop to assembly / machine code, however. Compiling the same machine code with different compilers will generate different machine code, and multiple C statements might result in the same machine code. It gets even more interesting if optimizations are turned on in the compiler (very likely for production code). Then the compiler may rearrange, merge and even eliminate instructions to create faster and/or smaller machine code. For example something like:Code:#define N 42int main (int argc, char *argv[]) {int i;int total=0;int input;for (i=0; i<3; i++) {total += N+i;j = N+i;}input = 2 * (PINB & (1 << 3));my_func(total, input);}Might be optimized to the assembly equivalent of:Code:a = (inb(PINB) << 1) & 16;b = 129;c(b, a);Some of the assembly directives (eg. inb) may not even have a direct C equivalent.

author: Rick Law
author_rank: Super Contributor
timestamp: October 13, 2013, 06:44:06 pm
content: Quote from: Electro Fan on October 13, 2013, 06:38:34 amThink of it this way: you view a cooking show and you took perfect notes. Following your notes, you can make that special cake perfect every time. But however perfect, one can never convert that step-by-step instruction back to a TV cooking show. You can follow it to script and film a new show, but not convert your notes back to the original video.In the process of making the notes, information is lost that can never be retrieved.A compiler converts your high level language description of the symbolic work you want the computer to do, and converted it to the actual steps that the CPU needs to carry out. Once converted (compiled), a lot of information are lost. The compiler may allocate address 0x1000 as a place to store variable "loopCount" for your statementint loopCount=1;.... somewhere down the roadut once converted, the information that it was a variable to count loops is lost. To the CPU, it is a memory location that was loaded as 1, and later on, it gets incremented. It doesn't know why. That you may use it inside a FOR is gone.Also for example in implementing a for (loopCount=0;loopCount<10;loopCount++), different compilers, or different version of the same compiler, or the same version with different option flags, all may generate very different op-codes. Even discerning that it is a FOR loop from disassembly would be a hard tasks. You can see that a bit down is a point where it was "JUMP" to from a point lower still, but no clue that it is a FOR, or a DO, or a WHILE. Even to discern that it is some kind of looping, you will have to analyze the disassembled code one op-code at a time to figure that out.