• AVR Freaks

Hot!Stepping through code, how to analyze assembly from library (.a file)

Author
PhreakShow
Starting Member
  • Total Posts : 43
  • Reward points : 0
  • Joined: 2011/05/13 01:35:01
  • Location: 0
  • Status: offline
2020/06/02 09:37:11 (permalink)
0

Stepping through code, how to analyze assembly from library (.a file)

Hi guys.
 
I have a project which uses an external library; it is included using properties - libraries - file.a
The (former) colleague who originally wrote the code for this library is gone, and so are the sources. A classic...
 
This library writes stuff to the SPI bus, but disables to clock line before driving the MOSI line. It is a major pain in the ass to count bits without a clock line present. I suppose at some point, the lib sets my tristate bits for the clock line to output.
So now I want to step through the assembly code until I find that position, patch it with a hex editor and flash it to my PIC18.
 
I can see the calls that lead to the lib, and I can step through the asm instructions using the "program memory" window. I cannot see any live values of registers, through, and I cannot see an access to a tris register. 
 
If I clear the tris before entering the lib function, the tris is set afterwards. Imho, the lib function sets the bit again.
 
So basically I have two questions: How can I see register (or variable) contents, and why is there no access to the tris?
#1

15 Replies Related Threads

    RISC
    Super Member
    • Total Posts : 5732
    • Reward points : 0
    • Status: online
    Re: Stepping through code, how to analyze assembly from library (.a file) 2020/06/02 14:07:28 (permalink)
    0
    Hi,
    Which compiler do you use ?
    With the memory disassembly you should be able to see ASM instructions of the library and step by step into it.
    It will show you any register access : 
    https://microchipdevelope...ory#:~:text=In%20MPLAB®%20X%2C%20all,File%20Registers&text=Other%20Memory
    Regards
    #2
    PhreakShow
    Starting Member
    • Total Posts : 43
    • Reward points : 0
    • Joined: 2011/05/13 01:35:01
    • Location: 0
    • Status: offline
    Re: Stepping through code, how to analyze assembly from library (.a file) 2020/06/02 14:30:07 (permalink)
    0
    I use the XC8 compiler. I only noticed the disassembly, will have a closer look at the hardware registers tomorrow. Maybe I just missed it.
     
    Is there an assembler opcode for the TRIS registers? In order to search for it in the binary.
    #3
    ric
    Super Member
    • Total Posts : 27595
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Stepping through code, how to analyze assembly from library (.a file) 2020/06/02 14:33:02 (permalink)
    0
    If you're using the PLIB libraries that used to come with XC8, they provide source code for all those functions.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    RISC
    Super Member
    • Total Posts : 5732
    • Reward points : 0
    • Status: online
    Re: Stepping through code, how to analyze assembly from library (.a file) 2020/06/02 14:38:38 (permalink)
    0
    Hi,
    Which device do you use ?
    Every PIC16/PIC18 datasheet contains all instructions and their opcodes.
    In case you use an "old" PIC16 device (only 2 or 3 digits after F like PIC16Fxx or PIc16Fxxx), you can use the following detailed manual : http://ww1.microchip.com/downloads/en/devicedoc/33023a.pdf
    Regards
    #5
    PhreakShow
    Starting Member
    • Total Posts : 43
    • Reward points : 0
    • Joined: 2011/05/13 01:35:01
    • Location: 0
    • Status: offline
    Re: Stepping through code, how to analyze assembly from library (.a file) 2020/06/02 14:44:24 (permalink)
    0
    @ric: No it's not an plib library. An ex-colleague wrote a 3rd party lib and I am trying to figure out how to change the behaviour.
     
    @RISC, it's a PIC18F25K83. I looked through your pdf, and I found asm instructrions and their opcodes, but no opcode for a register, for example TRISB.
    #6
    crennolet
    Super Member
    • Total Posts : 197
    • Reward points : 0
    • Joined: 2012/03/15 09:51:58
    • Location: 0
    • Status: offline
    Re: Stepping through code, how to analyze assembly from library (.a file) 2020/06/02 15:21:02 (permalink)
    0
    TRISB is a register, which is a memory address. So no "opcode" will be found. You need to look at the datasheet and locate the address for that register and then you will have to look in the program memory listing to see where that register address is being modified.
     
    #7
    ric
    Super Member
    • Total Posts : 27595
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Stepping through code, how to analyze assembly from library (.a file) 2020/06/02 15:36:26 (permalink)
    0
    crennolet
    TRISB is a register, which is a memory address. So no "opcode" will be found. You need to look at the datasheet and locate the address for that register and then you will have to look in the program memory listing to see where that register address is being modified.

    Which might be a MOVWF instruction, or maybe a BCF instruction.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #8
    NorthGuy
    Super Member
    • Total Posts : 6161
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: Stepping through code, how to analyze assembly from library (.a file) 2020/06/02 16:10:56 (permalink)
    +1 (1)
    There's a TRIS instruction on PIC16s which can be used to set TRISx registers, but I it is used very rarely.
    #9
    ric
    Super Member
    • Total Posts : 27595
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Stepping through code, how to analyze assembly from library (.a file) 2020/06/02 16:39:34 (permalink)
    0
    OP has already stated it is a PIC18 device.

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #10
    crennolet
    Super Member
    • Total Posts : 197
    • Reward points : 0
    • Joined: 2012/03/15 09:51:58
    • Location: 0
    • Status: offline
    Re: Stepping through code, how to analyze assembly from library (.a file) 2020/06/02 16:39:57 (permalink)
    0
    And since this is a PIC18, it is possible that the code may use indirect accesses, which means you may not even see the TRISB address on the instruction, but rather somewhere above where one of the indirect address registers is loaded with the address.
     
    #11
    NorthGuy
    Super Member
    • Total Posts : 6161
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: Stepping through code, how to analyze assembly from library (.a file) 2020/06/02 18:43:58 (permalink)
    +1 (1)
    ric
    OP has already stated it is a PIC18 device.



    PIC18 makes looking for the write instruction much easier. Since it's in ACCESS, the bank selection won't affect the address. Thus it is only necessary to find the memory access instruction which has the ACCESS bit set and the address byte equal to the address of the TRISx register.
     
    crennolet
    And since this is a PIC18, it is possible that the code may use indirect accesses, which means you may not even see the TRISB address on the instruction, but rather somewhere above where one of the indirect address registers is loaded with the address.

     
    That's possible, but unlikely.
     
    #12
    1and0
    Access is Denied
    • Total Posts : 10902
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Stepping through code, how to analyze assembly from library (.a file) 2020/06/02 20:14:36 (permalink)
    0
    PhreakShow
    it's a PIC18F25K83. I looked through your pdf, and I found asm instructrions and their opcodes, but no opcode for a register, for example TRISB.

    TRISB is located at address 0x3FC3 in the Access RAM, so search for opcodes with the lower 7 bits as 0b011000011;
    i.e. any opcode with pattern xxxx xxxx xxx0 1100 0011 where x is don't care. The most likely instructions will be MOVWF, BCF, and BSF.
     
    #13
    PhreakShow
    Starting Member
    • Total Posts : 43
    • Reward points : 0
    • Joined: 2011/05/13 01:35:01
    • Location: 0
    • Status: offline
    Re: Stepping through code, how to analyze assembly from library (.a file) 2020/06/03 04:00:53 (permalink)
    0
    Thanks for your answers, I will try and have a look at the hints you gave me.
     
    Another question: The compiler creates an elf and a hex file. Is there a way to convert this to a binary, which I can edit with a hex editor?
     
    Edit: I used the TRISB register under Window - Debugging - IO View, and had a look at TRISB1... but it stayed 0.
     
    Is there another way to drive the SPI1 on port B, but without clock? I thought that guy just set the tris to input, thus disabling the actual clock output.
    post edited by PhreakShow - 2020/06/03 08:31:04
    #14
    NorthGuy
    Super Member
    • Total Posts : 6161
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: Stepping through code, how to analyze assembly from library (.a file) 2020/06/03 10:01:38 (permalink)
    0
    SCK signal can be routed to anywhere with PPS, so it might be on a different pin. However, the abandoned pin will not tristate with TRIS set to zero.
    #15
    PhreakShow
    Starting Member
    • Total Posts : 43
    • Reward points : 0
    • Joined: 2011/05/13 01:35:01
    • Location: 0
    • Status: offline
    Re: Stepping through code, how to analyze assembly from library (.a file) 2020/06/03 10:06:54 (permalink)
    0
    During startup, there is a clock visible on this pin. After some SPI packets, the clock suddenly disappears. No external SPI slave is connected, for simplicity.
     
    In order to achieve this, the former programmer could either set TRIS to 1 or re-set the pin using PPS. Any other possibilities to disable the SPI clock during runtime, with an actual clock present during the first few seconds after startup?
    #16
    Jump to:
    © 2020 APG vNext Commercial Version 4.5