What the heck is this beta doing?

Beta Testing discussion on mikroC PRO for PIC.
Post Reply
Author
Message
ira
Posts: 22
Joined: 14 Feb 2013 20:04

What the heck is this beta doing?

#1 Post by ira » 08 Feb 2019 06:36

This code within a larger piece of code generates assembly code that seems to make no sense. Note that the beginning and ending MOVLB 0 are not part of this command but exist right before and right after this code.

Code: Select all

unsigned char k, v;
unsigned long c;

c = (uint16_t)(k * v) ;
Generates this in my code.

Code: Select all

0x09F2	0x0100      	MOVLB       0
0x09F8	0xF000C101  	MOVFF       FARG_key2password_key, R0
0x09FC	0xF001C102  	MOVFF       FARG_key2password_key+1, R1
0x0A00	0xF004C0FF  	MOVFF       key2password_vid_L0, R4
0x0A04	0x0E00      	MOVLW       0
0x0A06	0x6E05      	MOVWF       R5 
0x0A08	0xDC74      	RCALL       _Mul_16X16_U
0x0A0A	0xF108C000  	MOVFF       R0, key2password_r11_L0
0x0A0E	0xF109C001  	MOVFF       R1, key2password_r11_L0+1
0x0A12	0x0E00      	MOVLW       0
0x0A14	0xBE01      	BTFSC       R1, 7 
0x0A16	0x0EFF      	MOVLW       255
0x0A18	0x0101      	MOVLB       1
0x0A1A	0x6F0A      	MOVWF       key2password_r11_L0+2, 1
0x0A1C	0x6F0B      	MOVWF       key2password_r11_L0+3, 1
0x0A1E	0x0E00      	MOVLW       0
0x0A20	0x6F0A      	MOVWF       key2password_r11_L0+2, 1
0x0A22	0x6F0B      	MOVWF       key2password_r11_L0+3, 1
0x09F2	0x0100      	MOVLB       0

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: What the heck is this beta doing?

#2 Post by janni » 09 Feb 2019 03:33

Aren't you by accident running with optimization level 0? And isn't your first multiplication argument of int type?

ira
Posts: 22
Joined: 14 Feb 2013 20:04

Re: What the heck is this beta doing?

#3 Post by ira » 09 Feb 2019 04:09

On purpose, when I tried other levels, the code doesn't work and I have no memory or speed issues. But even with that, it's still a bit wordy, isn't it?

Ira

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: What the heck is this beta doing?

#4 Post by janni » 09 Feb 2019 13:23

ira wrote:On purpose, when I tried other levels, the code doesn't work and I have no memory or speed issues. But even with that, it's still a bit wordy, isn't it?
Wordy - yes, but then it's hard to blame optimization when it's being blocked. As your code is in fact

Code: Select all

unsigned char char_var;
int int_var;
unsigned long ulong_var;

  ulong_var = (unsigned int)(int_var * char_var);
multiplication is performed on signed integers, then cast to unsigned long. The unoptimized assembly code reflects the way compiler operates - first works on signed variables and only then corrects the result to unsigned. With optimization restored, the final assembly looks reasonable

Code: Select all

0x005A	0xF000C016  	MOVFF       main_int_var_L0, R0
0x005E	0xF001C017  	MOVFF       main_int_var_L0+1, R1
0x0062	0xF004C015  	MOVFF       main_char_var_L0, R4
0x0066	0x0E00      	MOVLW       0
0x0068	0x6E05      	MOVWF       R5 
0x006A	0xDFD8      	RCALL       _Mul_16x16_U
0x006C	0xF018C000  	MOVFF       R0, main_ulong_var_L0
0x0070	0xF019C001  	MOVFF       R1, main_ulong_var_L0+1
0x0074	0x0E00      	MOVLW       0
0x0076	0x6E1A      	MOVWF       main_ulong_var_L0+2 
0x0078	0x6E1B      	MOVWF       main_ulong_var_L0+3
whether compiler version 7.20 or beta 7.30 is used.

ira
Posts: 22
Joined: 14 Feb 2013 20:04

Re: What the heck is this beta doing?

#5 Post by ira » 10 Feb 2019 22:57

Thanks for the explanation. The intricacies of C compiler rules are not something I'm familiar with so thanks for pointing out my misunderstanding. I think I'm currently at 4K used of 32K Flash and 200 bytes used of 2K of RAM. I don't need the processor, I just need the CAN engine so I get way more than I need.

Ira

Post Reply

Return to “mikroC PRO for PIC Beta Testing”