Compiler generates incorrect code for simplest program.
John Gulbrandsen
Can anyone figure out why the below program does not increment the
variable 'i'? The C-program shifts i to the left one bit per loop but
the generated code (asm program) doesn't change the 'i' variable
(constant 0)!
C51 COMPILER V8.02 MAIN 11/27/2006 17:21:15 PAGE 1
C51 COMPILER V8.02, COMPILATION OF MODULE MAIN
OBJECT MODULE PLACED IN main.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE main.c OPTIMIZE(0,SPEED) BROWSE DEBUG OBJECTEXTEND CODE
line level source
1
2 #include <REG7C646.H>
3
4 void main()
5 {
6 1 unsigned char i;
7 1
8 1 while(1)
9 1 {
10 2 for(i = 0; i <= 0x80; i <<= 1)
11 2 {
12 3 P1 = i;
13 3 }
14 2 }
15 1 }
C51 COMPILER V8.02 MAIN 11/27/2006 17:21:15 PAGE 2
ASSEMBLY LISTING OF GENERATED OBJECT CODE
; FUNCTION main (BEGIN)
; SOURCE LINE # 4
; SOURCE LINE # 5
0000 ?C0001:
; SOURCE LINE # 8
; SOURCE LINE # 9
; SOURCE LINE # 10
0000 750000 R MOV i,#00H
0003 ?C0003:
0003 E500 R MOV A,i
0005 D3 SETB C
0006 9480 SUBB A,#080H
0008 50F6 JNC ?C0001
; SOURCE LINE # 11
; SOURCE LINE # 12
000A 850090 R MOV P1,i
; SOURCE LINE # 13
000D ?C0005:
000D E500 R MOV A,i
000F 25E0 ADD A,ACC
0011 F500 R MOV i,A
0013 80EE SJMP ?C0003
0015 ?C0004:
; SOURCE LINE # 14
0015 80E9 SJMP ?C0001
0017 ?C0002:
; SOURCE LINE # 15
0017 ?C0006:
0017 22 RET
; FUNCTION main (END)
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 24 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 1 ----
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
Read-Only
Author John Gulbrandsen
Posted 28-Nov-2006 01:40 GMT
Toolset C51
RE: Compiler generates incorrect code for simplest program.
John Gulbrandsen
Never mind! Shifting a 0 doesn't do much :)
Read-Only
Author Christoph Franck
Posted 28-Nov-2006 08:50 GMT
Toolset C51
RE: Compiler generates incorrect code for simplest program.
Christoph Franck
Yes, but blame the compiler for it. :)
Read-Only
Author Martin Macher
Posted 28-Nov-2006 10:21 GMT
Toolset C51
RE: Compiler generates incorrect code for simplest program.
Martin Macher
My Suggestion: Let's examine your code by PC-Lint. Its output
could consist a warning: Variable 'i' not initialized with proper
value....
cheers and *lol*
Martin
Read-Only
Author Christoph Franck
Posted 28-Nov-2006 11:23 GMT
Toolset C51
RE: Compiler generates incorrect code for simplest program.
Christoph Franck
Its output could consist a warning: Variable 'i' not
initialized with proper value....
i is initialized to 0 in the for statement.
Read-Only
Author Martin Macher
Posted 28-Nov-2006 16:30 GMT
Toolset C51
RE: Compiler generates incorrect code for simplest program.
Martin Macher
Don't be serious, it was a joke...
John Gulbrandsen already explained the cause.
This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.
ARM websites use two types of cookie: (1) those that enable the site to function and perform as required; and (2) analytical cookies which anonymously track visitors only while using the site. If you are not happy with this use of these cookies please review our Privacy Policy to learn how they can be disabled. By disabling cookies some features of the site will not work.