Q: Compiler generates wrong Assembler Code
Next Thread | Thread List | Previous Thread
Start a Thread | Settings
| Details |
Message |
|
Read-Only
Author Jacek Wisniowski
Posted 24-Jul-2003 19:09 GMT
Toolset C166
|
 Q: Compiler generates wrong Assembler Code
Jacek Wisniowski
I am involved within the development of some control hardware using the Infeneon XC161 processor with a "self made" evaluation board.
I think something is wrong with the compiler.
First I thought that there is a bug on the board, but most program parts are running.
Measurments on the board end up in the result that everything works fine.
I used the internal debuger to check out what asm code really has been generated.
I was upset! Some Variables are lost after a function reentry. My code works fine with constants, but if I put some evalation expressions between the lines everything goes wrong.
Here is the Code example:
This short code should write integer numbers thoug the serial port. "putchar( uint )" works!
void NumToHex(unsigned int Handle)
{
const unsigned char numbers[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
//write the number into RS232
putchar('0');
putchar('x');
putchar( (unsigned char) numbers[ ((Handle & 0xf000) >> 12) ] );
putchar( (unsigned char) numbers[ ((Handle & 0x0f00) >> 8) ] );
putchar( (unsigned char) numbers[ ((Handle & 0x00f0) >> 4) ] );
putchar( (unsigned char) numbers[ (Handle & 0x000f) ] );
//and goto next line
putchar(10);
putchar(13);
}
*****
this code always returns "0x ". But there should be something like "0x0a25".
If I try
putchar( (unsigned char) numbers[0] );
putchar( (unsigned char) numbers[13] );
putchar( (unsigned char) numbers[2] );
putchar( (unsigned char) numbers[1] );
everything works fine.
I checked the range of my shifting-results.
All Numbers stay between 0 and 15.
PLEASE HELP ME. I nearly go mad with this.
Is there anything wrong with the compiler?
Have I forgotten a bracket?
Could there be a problem with my memory-configuration?
thanks for your answers
Jacek Wisniowski
|
|
|
Read-Only
Author Jon Ward
Posted 24-Jul-2003 23:11 GMT
Toolset C166
|
 RE: Q: Compiler generates wrong Assembler Code
Jon Ward
Does the following code work?
putchar(numbers[(Handle>>12) & 0x0f]);
putchar(numbers[(Handle>>8) & 0x0f]);
putchar(numbers[(Handle>>4) & 0x0f]);
putchar(numbers[Handle & 0x0f]);
Jon
|
|
|
Read-Only
Author Jacek Wisniowski
Posted 25-Jul-2003 17:23 GMT
Toolset C166
|
 RE: Q: Compiler generates wrong Assembler Code
Jacek Wisniowski
Hi Jon,
The following line is mostly simple, but
it's not working on my system.
putchar(numbers[Handle & 0x0f]);
If I Try
unsigned int dummy;
dummy = 5;
putchar(numbers[dummy]);
it's working.
There is a problem with math-expressions.
dummy = handle & 0x0f
isn't working, too.
It's like voodoo. I think the expression is lost inside the generated assembler-block.
But my debugger say's it's all right.
|
|
|
Read-Only
Author Jon Ward
Posted 26-Jul-2003 15:58 GMT
Toolset C166
|
 RE: Q: Compiler generates wrong Assembler Code
Jon Ward
I just tried your example and it works just fine (whether I use your original or my suggested change).
The code generated is as follows:
; FUNCTION NumToHex (BEGIN RMASK = @0x7FFF)
; SOURCE LINE # 6
0000 ECFD PUSH R13
0002 F0D8 MOV R13,R8
;---- Variable 'Handle' assigned to Register 'R13' ----
.
.
.
;putchar( (unsigned char) numbers[ ((Handle & 0xf000) >> 12) ] );
002A F04D MOV R4,R13
002C 66F400F0 AND R4,#0F000H
0030 7CC4 SHR R4,#0CH
0032 F010 MOV R1,R0
0034 0014 ADD R1,R4
0036 A981 MOVB RL4,[R1]
0038 C088 MOVBZ R8,RL4
003A CA000000 E CALLA cc_UC,putchar
;
;putchar( (unsigned char) numbers[ ((Handle & 0x0f00) >> 8) ] );
003E F04D MOV R4,R13
0040 66F4000F AND R4,#0F00H
0044 7C84 SHR R4,#08H
0046 F010 MOV R1,R0
0048 0014 ADD R1,R4
004A A981 MOVB RL4,[R1]
004C C088 MOVBZ R8,RL4
004E CA000000 E CALLA cc_UC,putchar
;
;putchar( (unsigned char) numbers[ ((Handle & 0x00f0) >> 4) ] );
0052 F04D MOV R4,R13
0054 66F4F000 AND R4,#0F0H
0058 7C44 SHR R4,#04H
005A F010 MOV R1,R0
005C 0014 ADD R1,R4
005E A981 MOVB RL4,[R1]
0060 C088 MOVBZ R8,RL4
0062 CA000000 E CALLA cc_UC,putchar
;
;putchar( (unsigned char) numbers[ (Handle & 0x000f) ] );
0066 F04D MOV R4,R13
0068 66F40F00 AND R4,#0FH
006C F010 MOV R1,R0
006E 0014 ADD R1,R4
0070 A981 MOVB RL4,[R1]
0072 C088 MOVBZ R8,RL4
0074 CA000000 E CALLA cc_UC,putchar
.
.
.
This code looks correct to me (R0 is the user stack pointer and numbers is located on the user stack) and furthemore it runs correctly.
Maybe there is another problem? How are you testing the code? Have you used the correct startup code for the XC devices? Where is the user stack located? Is that memory area valid? Do you get any compiler warnings or linker warnings?
Can you show the assembler code generated for your C function? That is probably the first place to start looking if you think you've found a compiler bug.
Jon
|
|
|
Read-Only
Author Jacek Wisniowski
Posted 29-Jul-2003 20:22 GMT
Toolset C166
|
 RE: Q: Compiler generates wrong Assembler Code
Jacek Wisniowski
Maybe there is another problem? How are you testing the code?
I use de Debugger and upload to my hardware-device.
Have you used the correct startup code for the XC devices?
I think so. Most components work, just this one is not working.
Where is the user stack located? Is that memory area valid?
Yes. There was a problem with the user stack in a previous code-version. A bigger User stck solved problems with procedure calls. But a bigger Stack Size results in "out of memory" behaviour.
Do you get any compiler warnings or linker warnings?
No, no errors and warnings.
I think that anything is different between the debugger settings and my device settings. But I dont't know where to look for.
I've had problems with calling procedures. we made some changes in the memory setup and after that the calls worked. I will try to call the HexNum()-code in main(). If this is working it might be the startup-code (stack).
Do you know any problems with startup-code, memory setup and function-calls?
My assembler code looks similar to yours.
What errors can result from wrong startup-code (I took the default-startup-code for XC161)? I work with the "medium" Memory modell.
I can show you the assembler code in my next posting after I am at work again, tomorrow.
Jacek
|
|
|
Read-Only
Author Jon Ward
Posted 30-Jul-2003 00:11 GMT
Toolset C166
|
 RE: Q: Compiler generates wrong Assembler Code
Jon Ward
My assembler code looks similar to yours.
Well, mine works so the generated code isn't the problem.
What errors can result from wrong startup-code (I took the default-startup-code for XC161)?
Just about any kind of error you could imagine. For example, the chip selects are configured in the startup code. If this is not configured correctly, your program would appear to have no RAM or ROM memory.
Your problem really sounds like a configuration issue. Have you checked to see that the startup code matches your hardware configuration?
Jon
|
|
|
Read-Only
Author Jacek Wisniowski
Posted 30-Jul-2003 12:15 GMT
Toolset C166
|
 RE: Q: Compiler generates wrong Assembler Code
Jacek Wisniowski
Hi Jon, this is my Assembler-Code:
545: void NumToHex(unsigned int Handle)
00010246 ECFD PUSH R13
00010248 F0D8 MOV R13,R8
0001024A 06F0F0FF ADD R0,#0xFFF0
546: {
548: const unsigned char numbers[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
0001024E F060 MOV R6,R0
00010250 E6F40040 MOV R4,#0x4000
00010254 E6F71000 MOV R7,#0x0010
00010258 CA00E609 CALLA+ CC_UC,?C_BCPYNN(0x109E6)
550: putchar('0');
0001025C E6F83000 MOV R8,#0x0030
00010260 BBDF CALLR putchar(0x10220)
551: putchar('x');
00010262 E6F87800 MOV R8,#0x0078
00010266 BBDC CALLR putchar(0x10220)
563: putchar(numbers[(Handle>>12) & 0x0f]);
00010268 F04D MOV R4,R13
0001026A 7CC4 SHR R4,#0x0C
0001026C 66F40F00 AND R4,#0x000F
00010270 F010 MOV R1,R0
00010272 0014 ADD R1,R4
00010274 A981 MOVB RL4,[R1]
00010276 C088 MOVBZ R8,RL4
00010278 BBD3 CALLR putchar(0x10220)
564: putchar(numbers[(Handle>>8) & 0x0f]);
0001027A F04D MOV R4,R13
0001027C 7C84 SHR R4,#0x08
0001027E 66F40F00 AND R4,#0x000F
00010282 F010 MOV R1,R0
00010284 0014 ADD R1,R4
00010286 A981 MOVB RL4,[R1]
00010288 C088 MOVBZ R8,RL4
0001028A BBCA CALLR putchar(0x10220)
565: putchar(numbers[(Handle>>4) & 0x0f]);
0001028C F04D MOV R4,R13
0001028E 7C44 SHR R4,#0x04
00010290 66F40F00 AND R4,#0x000F
00010294 F010 MOV R1,R0
00010296 0014 ADD R1,R4
00010298 A981 MOVB RL4,[R1]
0001029A C088 MOVBZ R8,RL4
0001029C BBC1 CALLR putchar(0x10220)
566: putchar(numbers[Handle & 0x0f]);
567:
568: //and goto next line
0001029E F04D MOV R4,R13
000102A0 66F40F00 AND R4,#0x000F
000102A4 F010 MOV R1,R0
000102A6 0014 ADD R1,R4
000102A8 A981 MOVB RL4,[R1]
000102AA C088 MOVBZ R8,RL4
000102AC BBB9 CALLR putchar(0x10220)
569: putchar(10);
000102AE E0A8 MOV R8,#0x0A
000102B0 BBB7 CALLR putchar(0x10220)
570: putchar(13);
000102B2 E0D8 MOV R8,#0x0D
000102B4 BBB5 CALLR putchar(0x10220)
571: }
It's not woking with my device.
I will check stack and memory settings.
Thank you for your help.
Jacek
|
|
Next Thread | Thread List | Previous Thread
Start a Thread | Settings
|