home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 19139 < prev    next >
Encoding:
Text File  |  1993-01-02  |  2.6 KB  |  87 lines

  1. Xref: sparky comp.lang.c:19139 comp.sys.ibm.pc.programmer:726 alt.msdos.programmer:3070 comp.sys.ibm.pc.misc:16286
  2. Newsgroups: comp.lang.c,comp.sys.ibm.pc.programmer,alt.msdos.programmer,comp.sys.ibm.pc.misc
  3. Path: sparky!uunet!munnari.oz.au!bunyip.cc.uq.oz.au!marlin.jcu.edu.au!coral.cs.jcu.edu.au!stuart
  4. From: stuart@coral.cs.jcu.edu.au (Stuart Kemp)
  5. Subject: BEWARE! Borland's BCC produces bad code (with example!)
  6. Message-ID: <stuart.726023333@coral>
  7. Summary: '-Z' on BCC produces bad code
  8. Keywords: BCC, PC, MSDOS
  9. Sender: news@marlin.jcu.edu.au (USENET News System)
  10. Organization: James Cook University
  11. Date:  3 Jan 93 01:08:53 GMT
  12. Lines: 73
  13.  
  14.  
  15. The appended program was gutted from something I am working on ...
  16. If you compile it with "bcc -ml -Z file.c" it will produce bad code;
  17. compile with "bcc -ml file.c" and all is okay. (The output generated should
  18. always list the same segment, with different offsets.) This happens on both
  19. BCC v3.0 and v3.1. Unfortunately, you will never know when this is going
  20. to bite you ... would suggest that you be beware of compiling with (just)
  21. '-O2' (or '-Ox') since this implies '-Z' as well; better to use '-Ox -Z-'.
  22.  
  23. The "bad" portion of code generated with the '-Z' is in the printf()
  24. statement in the 'for' loop ... the assembly code (generated with '-S') is:
  25.  
  26.    ;    
  27.    ;        fprintf(log, "%2d  %p\n", i, T->Hdrs[i]);
  28.    ;    
  29.     mov    es,word ptr es:[bx+6]    ; BUG!!!!!
  30.     push    word ptr es:[si+2]
  31.     push    word ptr es:[si]
  32.  
  33. The marked statement should be omitted completely. Doing so and compiling
  34. the resultant assembly code yields a working program.
  35.  
  36. Anyone got any other (small!) examples of where compilers produce wrong
  37. code?
  38.  
  39. -SRK
  40.  
  41. -------------- Snip Snip -------------
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44.  
  45. typedef
  46. struct _GTbl
  47.   { void *Data;
  48.     void **Hdrs;
  49.     unsigned short StructSize;
  50.   } GTbl;
  51.  
  52. FILE *log;
  53.  
  54. /* Allocates a buffer area, then set some pointers into this area */
  55. /* NULL checks removed while examining assembly code! */
  56. void
  57. Bug(GTbl *T)
  58. {
  59.   short i;
  60.  
  61.   T->Hdrs = (void**) malloc(10 * sizeof(void*));
  62.   T->Data = (void*) malloc(10 * T->StructSize);
  63.  
  64.   fprintf(log, "Hdrs: %p\n", T->Hdrs);
  65.   fprintf(log, "Data: %p\n", T->Data);
  66.   for (i = 0; i < 10; i++)
  67.   { T->Hdrs[i] = (void*) ((char*)T->Data + i * T->StructSize);
  68.     fprintf(log, "%2d  %p\n", i, T->Hdrs[i]);    /* BUG IN ASSEMBLY HERE */
  69.   }
  70. }
  71.  
  72. main()
  73. {
  74.   GTbl T;
  75.  
  76.   log = fopen("LOG2", "w");
  77.   T.StructSize = 16;
  78.   Bug(&T);
  79.   fclose(log);
  80.   return(0);
  81. }
  82. --
  83. Dept of Computer Science        Internet: stuart@coral.cs.jcu.edu.au
  84. James Cook University
  85. Townsville                      Tel: +61 77 814992
  86. Qld 4811 Australia              FAX: +61 77 814092
  87.