• AVR Freaks

Helpful ReplyHot!PIC32Mx understanding disassembly

Author
Robin66
New Member
  • Total Posts : 26
  • Reward points : 0
  • Joined: 2016/05/24 12:04:24
  • Location: 0
  • Status: online
2021/01/26 12:59:16 (permalink)
0

PIC32Mx understanding disassembly

Hi.  I've been using PICs for years but this is the first time I've used a 32bit device.  I'm using a PIC32MX270F256B and have running an LCD 320*240 but I'm trying to fix SPI slow thru-put but the disassembly looks like garbage.
 
details:
MPLAB X IDE v5.4
xc32 v2.5  (optimization level 2)
 
eg. here's a wrapper function for an spi function defined in macros.  
 
void lcd_send(bool dc, uint8_t value)
{
LCD_CS = 0;

if(dc)
LCD_DC = 1;
else
LCD_DC = 0;

LCD_FUNC_1B(value);
LCD_CS = 1;
}
 
I look at the disassembly and I expect to see a few instructions relating to LCD_CS = 0 (it would have been 1 line when I was using pic24, a BCLR instruction, but anyway...).  However I see 300 lines of assembly, most of it repetitions
 
142: void lcd_send(bool dc, uint8_t value)
143: {
9D000000 27BDFFE8 ADDIU SP, SP, -24
9D000004 AFBF0014 SW RA, 20(SP)
9D000008 AFB00010 SW S0, 16(SP)
144: LCD_CS = 0;
9D00000C 3C10BF88 LUI S0, -16504
9D000010 96026130 LHU V0, 24880(S0)
9D000014 7C022104 INS V0, ZERO, 4, 1
9D000018 A6026130 SH V0, 24880(S0)
9D000344 3C10BF88 LUI S0, -16504
9D000348 96026130 LHU V0, 24880(S0)
9D000354 7C022104 INS V0, ZERO, 4, 1
9D000358 A6026130 SH V0, 24880(S0)
9D000384 96026130 LHU V0, 24880(S0)
9D000388 7C022104 INS V0, ZERO, 4, 1
9D00038C A6026130 SH V0, 24880(S0)
9D0003D4 3C10BF88 LUI S0, -16504
9D0003D8 96026130 LHU V0, 24880(S0)
9D0003E0 7C022104 INS V0, ZERO, 4, 1
9D0003E4 A6026130 SH V0, 24880(S0)
9D00042C 3C10BF88 LUI S0, -16504
9D000430 96026130 LHU V0, 24880(S0)
9D000438 7C022104 INS V0, ZERO, 4, 1
9D00043C A6026130 SH V0, 24880(S0)
9D000498 3C10BF88 LUI S0, -16504
9D00049C 96026130 LHU V0, 24880(S0)
9D0004A8 7C022104 INS V0, ZERO, 4, 1
9D0004AC A6026130 SH V0, 24880(S0)
9D0004FC 96026130 LHU V0, 24880(S0)
 
now I know these lines can't be executing bc my 'scope shows a delay of ~1us from CS going low to the start of SPI clocking (inside LCD_FUNC_1B) and I'm running at 48MHz so i'm losing ~50 instructions.
 
Should I give up on understanding the assembly code now I'm working with 32bit PICs?  Or is there a setting which would make it more accessible?  Any general advice on minimising overheads on PIC32s would be appreciated.  I'm used to working with PIC24s where basic things like setting a bit really only takes 1 line of assembly (BSET).  My PIC32 is way faster for float maths, but seems less good for the most basic stuff.
 
If I turn off optimizations (level = 0) I get something much more realistic, but it does run slower on the 'scope.
 
142: void lcd_send(bool dc, uint8_t value)
143: {
9D000000 27BDFFE8 ADDIU SP, SP, -24
9D000004 AFBF0014 SW RA, 20(SP)
9D000008 AFBE0010 SW FP, 16(SP)
9D00000C 03A0F021 ADDU FP, SP, ZERO
9D000010 00801821 ADDU V1, A0, ZERO
9D000014 00A01021 ADDU V0, A1, ZERO
9D000018 A3C30018 SB V1, 24(FP)
9D00001C A3C2001C SB V0, 28(FP)
144: LCD_CS = 0;
9D000020 3C03BF88 LUI V1, -16504
9D000024 94626130 LHU V0, 24880(V1)
9D000028 7C022104 INS V0, ZERO, 4, 1
9D00002C A4626130 SH V0, 24880(V1)
145:
146: if(dc)
9D000030 93C20018 LBU V0, 24(FP)
9D000034 10400008 BEQ V0, ZERO, 0x9D000058
9D000038 00000000 NOP
147: LCD_DC = 1;
 
 
#1
NorthGuy
Super Member
  • Total Posts : 6518
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: offline
Re: PIC32Mx understanding disassembly 2021/01/26 13:44:04 (permalink) ☄ Helpfulby Robin66 2021/01/26 13:54:59
0
Looks like the compiler inlined your function during optimization. Therefore, there are many copies of the same code at different places - you can see that it repeats many time. The commands are the same, but addresses are different. The listing gathered all these together so it appears that it repeats the same command many times, but in fact these are extracts for different places of the code.
#2
Robin66
New Member
  • Total Posts : 26
  • Reward points : 0
  • Joined: 2016/05/24 12:04:24
  • Location: 0
  • Status: online
Re: PIC32Mx understanding disassembly 2021/01/26 13:54:52 (permalink)
0
Ok, that makes complete sense.  I won't balk when I see this in future.  Bearing this in mind I can still look at the disassembly to get a jist of what's going on. thanks for the explanation.  
#3
toms
Senior Member
  • Total Posts : 131
  • Reward points : 0
  • Joined: 2006/03/07 18:06:24
  • Location: London, UK
  • Status: offline
Re: PIC32Mx understanding disassembly 2021/01/28 04:41:19 (permalink) ☄ Helpfulby Robin66 2021/01/28 04:42:28
4 (1)
I find it easier to read the assembly with optimisations set to 0, because the compiler wont do any fancy tricks and throw you off the trail. You'll typically get a group of assembly instructions per line of C code that does what it says on the tin. Its much easier to follow, just more verbose and more repetitive.
 
Once I have working code with optimisation level 0, I then turn on optimisation and see how much space I save and speed I gain.
#4
Robin66
New Member
  • Total Posts : 26
  • Reward points : 0
  • Joined: 2016/05/24 12:04:24
  • Location: 0
  • Status: online
Re: PIC32Mx understanding disassembly 2021/01/28 04:43:43 (permalink)
0
Yeah I've started doing that.  I still find it much harder to follow than pic24 disassembly but I guess there was a step change in the architecture necessitating a new approach
#5
toms
Senior Member
  • Total Posts : 131
  • Reward points : 0
  • Joined: 2006/03/07 18:06:24
  • Location: London, UK
  • Status: offline
Re: PIC32Mx understanding disassembly 2021/01/28 04:50:18 (permalink)
4 (1)
PIC32 is based on MIPS CPU cores, so it would be worth getting a hold of a MIPS instruction set reference and keep it handy.
 
https://www.mips.com/?do-download=the-mips32-instruction-set-v6-06
 
I also find the output from objdump on the command line a bit nicer to read than the assembly listing in MPLABX. You dont get the fancy colours, and you'll have to search for the function you want to look at though.
#6
Jump to:
© 2021 APG vNext Commercial Version 4.5