Hex file dis-assembler

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

Can anyone tell me how to dis-assemble a hex file associated with an AVR Arduino Nano program with AtMega328P.

I have no problem parsing each line of the hex file to obtain byte count, record address, type, data, and check sum.

What I would like to do is convert the data codes into assembler nmemonics and obtain an assembler listing.

This topic has a solution.

Skier

Last Edited: Fri. Jan 31, 2020 - 10:10 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avr-objdump will do this easily.

 

(Its in the Arduino installation., you are going to need some parms like -m and -j, I'm not near a computer right now so cannot easily check but I've posted loads of examples here over the years)

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

AS7 will display a disassembled hex file.

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

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


ki0bk wrote:
AS7 will display a disassembled hex file.
Really? How?

 

If I File-Open a .hex file I see:

 

 

Sure it shows the bytes but not a disassembly. To disassemble I really need the debugger/simulator to start so one might consider File-Open File for debugging... but that only allows ELF and COF files to be opened - it won't load  HEX.

 

As I said above you can do this:

D:\test\test\Debug>avr-objdump -j .sec1 -d -m avr:5 test.hex

test.hex:     file format ihex


Disassembly of section .sec1:

00000000 <.sec1>:
   0:   33 c0           rjmp    .+102           ;  0x68
   2:   00 00           nop
   4:   39 c0           rjmp    .+114           ;  0x78
   6:   00 00           nop
   8:   37 c0           rjmp    .+110           ;  0x78
   a:   00 00           nop
   c:   35 c0           rjmp    .+106           ;  0x78
   e:   00 00           nop
  10:   33 c0           rjmp    .+102           ;  0x78
  12:   00 00           nop
  14:   31 c0           rjmp    .+98            ;  0x78
  16:   00 00           nop
  18:   2f c0           rjmp    .+94            ;  0x78
  1a:   00 00           nop
  1c:   2d c0           rjmp    .+90            ;  0x78
  1e:   00 00           nop
  20:   2b c0           rjmp    .+86            ;  0x78
  22:   00 00           nop
  24:   29 c0           rjmp    .+82            ;  0x78
  26:   00 00           nop
  28:   27 c0           rjmp    .+78            ;  0x78
  2a:   00 00           nop
  2c:   25 c0           rjmp    .+74            ;  0x78
  2e:   00 00           nop
  30:   23 c0           rjmp    .+70            ;  0x78
  32:   00 00           nop
  34:   21 c0           rjmp    .+66            ;  0x78
  36:   00 00           nop
  38:   1f c0           rjmp    .+62            ;  0x78
  3a:   00 00           nop
  3c:   1d c0           rjmp    .+58            ;  0x78
  3e:   00 00           nop
  40:   1b c0           rjmp    .+54            ;  0x78
  42:   00 00           nop
  44:   19 c0           rjmp    .+50            ;  0x78
  46:   00 00           nop
  48:   17 c0           rjmp    .+46            ;  0x78
  4a:   00 00           nop
  4c:   15 c0           rjmp    .+42            ;  0x78
  4e:   00 00           nop
  50:   13 c0           rjmp    .+38            ;  0x78
  52:   00 00           nop
  54:   11 c0           rjmp    .+34            ;  0x78
  56:   00 00           nop
  58:   0f c0           rjmp    .+30            ;  0x78
  5a:   00 00           nop
  5c:   0d c0           rjmp    .+26            ;  0x78
  5e:   00 00           nop
  60:   0b c0           rjmp    .+22            ;  0x78
  62:   00 00           nop
  64:   09 c0           rjmp    .+18            ;  0x78
  66:   00 00           nop
  68:   11 24           eor     r1, r1
  6a:   1f be           out     0x3f, r1        ; 63
  6c:   cf ef           ldi     r28, 0xFF       ; 255
  6e:   d8 e0           ldi     r29, 0x08       ; 8
  70:   de bf           out     0x3e, r29       ; 62
  72:   cd bf           out     0x3d, r28       ; 61
  74:   02 d0           rcall   .+4             ;  0x7a
  76:   04 c0           rjmp    .+8             ;  0x80
  78:   c3 cf           rjmp    .-122           ;  0x0
  7a:   80 e0           ldi     r24, 0x00       ; 0
  7c:   90 e0           ldi     r25, 0x00       ; 0
  7e:   08 95           ret
  80:   f8 94           cli
  82:   ff cf           rjmp    .-2             ;  0x82

but to do this I did need to know that this code was build for a mega328p and that it is of AVR architecture type 5 so I passed "-m avr:5". All the possible architectures are shown at the end of "avr-objdump -H":

      .....
      --dwarf-check          Make additional dwarf internal consistency checks.

avr-objdump: supported targets: elf32-avr elf32-little elf32-big plugin srec symbolsrec verilog tekhex binary ihex
avr-objdump: supported architectures: avr avr:1 avr:2 avr:25 avr:3 avr:31 avr:35 avr:4 avr:5 avr:51 avr:6 avr:100 avr:101 avr:102 avr:103 avr:104 avr:105 avr:106 avr:107 plugin
Report bugs to <http://www.sourceware.org/bugzilla/>.

The reason I know that a 328P is type 5 is from building a short 328P program and looking at the .map file:

Archive member included to satisfy reference by file (symbol)

c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr5\libgcc.a(_exit.o)
                              C:/Program Files (x86)/Atmel/Studio/7.0/Packs/atmel/ATmega_DFP/1.3.300/gcc/dev/atmega328p/avr5/crtatmega328p.o (exit)

Oh and the reason you need "-j .sec1" is that when looking at a .hex file (that does not really have "sections") it treats everything it finds as being in a dummy ".sec1" rather than the ".text" you might have guessed. So you have to direct it to disassmble the contents of the right "section".

 

The -d option is the thing that says "please disassemble this".

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

PS I forgot to say that the entire exercise is almost entirely pointless. It's close to impossible to read disassembled code that has no labels or structure. This is especially so if it was auto-generated code (that is a C compiler not a human Asm programmer) because C compilers do all kinds of weird and wonderful things like making a lot of use of "local" variables that get created on a stack and doing things like splitting the implementation of some high level logic into several not necessarily adjacent pieces of code (for example a for() loop might load the iterator variable 20 opcodes before the loop actually starts with some other stuff mixed in between (the compiler makes decisions to do this kind of thing if it suits it to reorder things). 

 

Is this one of these "the dog ate my homework" kind of things (or in this case "the dog ate might legally owned source code" kind of thing) ?

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

Just read "MDX AVR Assembly Simulator and Disassembler" by Milo_D on Forum EmbeddedRelated.com

 

MDX seems like the answer to my initial inquiry for AVR dis-assembler, however the instruction sequence, including getting NCurses from github.com, to get it up and running is very much over my head.

I would be much indebted to anyone who has followed the posted instructions and generated something like MDX.exe that I can run on my WIN10 laptop on a hex file.

Skier

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

I wrote an AVR disassembler/simulator years ago, but sorry, I don't feel inclined to let anyone use it.  I'd suggest you try to get MDX running, perhaps under Cygwin.  Try contacting Milo directly, probably would give you a hand.  More likely, you should just follow clawson's advice and use avr-objdump.

 

C: i = "told you so";