home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1990 / 08 / ladd.lst < prev    next >
File List  |  1990-07-10  |  8KB  |  441 lines

  1. _OPTIMIZING WITH MICROSOFT C 6.0_
  2. by Scott Robert Ladd
  3.  
  4. [LISTING ONE]
  5.  
  6. ***********************
  7. *** Microsoft C 6.0 ***
  8. ***********************
  9.  
  10. #include "stdio.h"
  11.  
  12. /* prototypes */
  13.  
  14. void doit(int i);
  15.  
  16. void (* func_ptr)(int i) = doit;
  17.  
  18. void doit(int i)
  19.     {
  20.     --> push bp
  21.     --> mov  bp,sp
  22.     --> push di
  23.     --> push si
  24.     --> mov  di,WORD PTR [bp+4]
  25.  
  26.     int loop;
  27.  
  28.     for (; i > 0; --i)
  29.  
  30.     --> or  di,di
  31.     --> jle $EX225
  32.  
  33.         {
  34.         for (loop = 0; loop < 26; ++loop)
  35.  
  36.         --> $F227:
  37.         --> sub si,si
  38.         --> mov WORD PTR [bp+4],di
  39.  
  40.             {
  41.             printf("loop character = %c\n", 0x41 + loop);
  42.  
  43.             --> $F230:
  44.             --> lea  ax,WORD PTR [si+65]
  45.             --> push ax
  46.             --> mov  ax,OFFSET DGROUP:$SG233
  47.             --> push ax
  48.             --> call _printf
  49.             --> add  sp,4
  50.             --> inc  si
  51.             --> cmp  si,26
  52.             --> jl   $F230
  53.             }
  54.  
  55.         printf("i / 16 = %d\n\n",i / 16);
  56.  
  57.         --> mov  ax,di
  58.         --> cwd
  59.         --> xor  ax,dx
  60.         --> sub  ax,dx
  61.         --> mov  cx,4
  62.         --> sar  ax,cl
  63.         --> xor  ax,dx
  64.         --> sub  ax,dx
  65.         --> push ax
  66.         --> mov  ax,OFFSET DGROUP:$SG234
  67.         --> push ax
  68.         --> call _printf
  69.         --> add  sp,4
  70.         --> dec  di
  71.         --> jne  $F227
  72.  
  73.         }
  74.  
  75.     --> $EX225:
  76.     --> pop si
  77.     --> pop di
  78.     --> mov sp,bp
  79.     --> pop bp
  80.     --> ret
  81.     --> nop
  82.     }
  83.  
  84. int main(void)
  85.     {
  86.     func_ptr(100);
  87.  
  88.     --> mov  ax,100
  89.     --> push ax
  90.     --> call WORD PTR _func_ptr
  91.     --> add  sp,2
  92.  
  93.     return 0;
  94.  
  95.     --> sub ax,ax
  96.     --> ret
  97.     }
  98.  
  99.  
  100.  
  101. [LISTING TWO]
  102.  
  103. *****************************************
  104. *** Microsoft C 6.0 (using _fastcall) ***
  105. *****************************************
  106.  
  107. #include "stdio.h"
  108.  
  109. /* prototypes */
  110.  
  111. void doit(int i);
  112.  
  113. void (* func_ptr)(int i) = doit;
  114.  
  115. void doit(int i)
  116.     {
  117.  
  118.     --> push bp
  119.     --> mov  bp,sp
  120.     --> sub  sp,2
  121.     --> push ax
  122.     --> push si
  123.  
  124.     int loop;
  125.  
  126.     for (; i > 0; --i)
  127.  
  128.     --> or  ax,ax
  129.     --> jle $EX225
  130.  
  131.         {
  132.         for (loop = 0; loop < 26; ++loop)
  133.  
  134.         --> $F227:
  135.         --> sub si,si
  136.  
  137.             {
  138.             printf("loop character = %c\n", 0x41 + loop);
  139.  
  140.             --> $F230:
  141.             --> lea  ax,WORD PTR [si+65]
  142.             --> push ax
  143.             --> mov  ax,OFFSET DGROUP:$SG233
  144.             --> push ax
  145.             --> call _printf
  146.             --> add  sp,4
  147.             --> inc  si
  148.             --> cmp  si,26
  149.             --> jl   $F230
  150.  
  151.             }
  152.  
  153.         printf("i / 16 = %d\n\n",i / 16);
  154.  
  155.         --> mov  ax,WORD PTR [bp-4]
  156.         --> cwd
  157.         --> xor  ax,dx
  158.         --> sub  ax,dx
  159.         --> mov  cx,4
  160.         --> sar  ax,cl
  161.         --> xor  ax,dx
  162.         --> sub  ax,dx
  163.         --> push ax
  164.         --> mov  ax,OFFSET DGROUP:$SG234
  165.         --> push ax
  166.         --> call _printf
  167.         --> add  sp,4
  168.         --> dec  WORD PTR [bp-4]
  169.         --> jne  $F227
  170.  
  171.         }
  172.  
  173.         --> $EX225:
  174.         --> pop si
  175.         --> mov sp,bp
  176.         --> pop bp
  177.         --> ret
  178.     }
  179.  
  180. int main(void)
  181.     {
  182.     func_ptr(100);
  183.  
  184.     --> mov  ax,100
  185.     --> call WORD PTR _func_ptr
  186.  
  187.     return 0;
  188.  
  189.     --> sub ax,ax
  190.     --> ret
  191.  
  192.     }
  193.  
  194.  
  195.  
  196. [LISTING THREE]
  197.  
  198. ************************
  199. *** Microsoft C 5.10 ***
  200. ************************
  201.  
  202. #include "stdio.h"
  203.  
  204. /* prototypes */
  205.  
  206. void doit(int i);
  207.  
  208. void (* func_ptr)(int i) = doit;
  209.  
  210. void doit(int i)
  211.     {
  212.     --> push    bp
  213.     --> mov     bp,sp
  214.     --> sub     sp,2
  215.     --> push    di
  216.     --> push    si
  217.  
  218.     int loop;
  219.  
  220.     for (; i > 0; --i)
  221.  
  222.     --> cmp WORD PTR [bp+4],0
  223.     --> jle $FB202
  224.     --> mov di,WORD PTR [bp+4]
  225.  
  226.         {
  227.         for (loop = 0; loop < 26; ++loop)
  228.  
  229.         --> $L20002:
  230.         --> sub si,si
  231.  
  232.             {
  233.             printf("loop character = %c\n", 0x41 + loop);
  234.  
  235.             --> $L20000:
  236.             --> lea  ax,WORD PTR [si+65]
  237.             --> push ax
  238.             --> mov  ax,OFFSET DGROUP:$SG206
  239.             --> push ax
  240.             --> call _printf
  241.             --> add  sp,4
  242.         }
  243.  
  244.         --> inc si
  245.         --> cmp si,26
  246.         --> jl  $L20000
  247.         --> mov WORD PTR [bp-2],si  ;loop
  248.  
  249.         printf("i / 16 = %d\n\n",i / 16);
  250.  
  251.         --> mov  ax,di
  252.         --> cwd
  253.         --> xor  ax,dx
  254.         --> sub  ax,dx
  255.         --> mov  cx,4
  256.         --> sar  ax,cl
  257.         --> xor  ax,dx
  258.         --> sub  ax,dx
  259.         --> push ax
  260.         --> mov  ax,OFFSET DGROUP:$SG207
  261.         --> push ax
  262.         --> call _printf
  263.         --> add  sp,4
  264.  
  265.         }
  266.  
  267.         --> dec di
  268.         --> jne $L20002
  269.         --> mov WORD PTR [bp+4],di
  270.  
  271.         --> $FB202:
  272.         --> pop si
  273.         --> pop di
  274.         --> mov sp,bp
  275.         --> pop bp
  276.         --> ret
  277.         --> nop
  278.  
  279.     }
  280.  
  281. int main(void)
  282.     {
  283.     func_ptr(100);
  284.  
  285.     --> mov  ax,100
  286.     --> push ax
  287.     --> call WORD PTR _func_ptr
  288.     --> add  sp,2
  289.  
  290.     return 0;
  291.  
  292.     --> sub  ax,ax
  293.     --> ret
  294.     }
  295.  
  296.  
  297.  
  298. [LISTING FOUR]
  299.  
  300. ********************
  301. *** Watcom C 7.0 ***
  302. ********************
  303.  
  304. #include "stdio.h"
  305.  
  306. /* prototypes */
  307.  
  308. void doit(int i);
  309.  
  310. void (* func_ptr)(int i) = doit;
  311.  
  312. void doit(int i)
  313.     {
  314.     int loop;
  315.  
  316.     --> push    bx
  317.     --> push    cx
  318.     --> push    dx
  319.     --> mov     cx,ax
  320.     --> jmp     short L3
  321.  
  322.     for (; i > 0; --i)
  323.         {
  324.         for (loop = 0; loop < 26; ++loop)
  325.             {
  326.             --> L1:
  327.             --> mov     bx,0041H
  328.  
  329.             printf("loop character = %c\n", 0x41 + loop);
  330.  
  331.             --> L2:
  332.             --> push    bx
  333.             --> mov     ax,offset DGROUP:L4
  334.             --> push    ax
  335.             --> call    near ptr printf_
  336.             --> add     sp,0004H
  337.             --> inc     bx
  338.             --> cmp     bx,005bH
  339.             --> jne     L2
  340.  
  341.             }
  342.  
  343.         printf("i / 16 = %d\n\n",i / 16);
  344.  
  345.         --> mov     bx,0010H
  346.         --> mov     ax,cx
  347.         --> cwd
  348.         --> idiv    bx
  349.         --> push    ax
  350.         --> mov     ax,offset DGROUP:L5
  351.         --> push    ax
  352.         --> call    near ptr printf_
  353.         --> add     sp,0004H
  354.         --> dec     cx
  355.  
  356.         }
  357.  
  358.     --> L3:
  359.     --> test    cx,cx
  360.     --> jg      L1
  361.  
  362.     --> pop     dx
  363.     --> pop     cx
  364.     --> pop     bx
  365.     --> ret
  366.  
  367.     }
  368.  
  369. int main(void)
  370.     {
  371.  
  372.     func_ptr(100);
  373.  
  374.     --> mov     ax,0064H
  375.     --> call    word ptr _func_ptr
  376.  
  377.     return 0;
  378.  
  379.     --> xor     ax,ax
  380.     --> ret
  381.  
  382.     }
  383.  
  384.  
  385. [Listing Five]
  386. /* Skeleton Program demonstrating the use of based pointers */
  387.  
  388. #include <string.h>
  389. #include <stdlib.h>
  390. #include <stdio.h>
  391. #include <malloc.h>
  392.  
  393. #define MAX_TAG 2000
  394. unsigned long get_size(void);
  395. _segment segvar;           /* name a segment for use with based pointers */
  396.  
  397. /* set up structures and tags within segment segvar */
  398.  
  399. typedef mytag {
  400. char filename[14];
  401. unsigned long size;
  402. mytag _based(segvar) *next;
  403. } _based(segvar) *PTAG, TAG;
  404.  
  405. main() {
  406.  
  407.    PTAG head, curptr;
  408.  
  409. /* Allocate a based heap of MAX_TAG structs. Put segment address in segvar. */
  410.  
  411.    if((segvar = _bheapseg(sizeof(TAG) * MAX_TAG))) == NULLSEG){
  412.       printf("error allocating based heap \n");
  413.       exit(-1);
  414.    }
  415.  
  416. /* Allocate memory within segvar for first structure in linked list */
  417.  
  418.    if((head = _bmalloc(segvar, sizeof(TAG)) == _NULLOFF) {
  419.    printf("error allocating TAG \n");
  420.    exit(-1);
  421.    }
  422.    head->size = get_size();
  423.    _fstrcpy((char far *) head->filename, get_name()); /* get a
  424.    filename and copy it to segvar */
  425.  
  426.    if((head->next = _bmalloc(segvar, sizeof(TAG)) == _NULLOFF) {
  427.    printf("error allocating TAG \n");
  428.    exit(-1);
  429.    }
  430. .
  431. .
  432. .
  433. }
  434. unsigned long get_size(void) {
  435. return 1;
  436. }
  437.  
  438. char *get_name(void) {
  439. return("foo");
  440. }
  441.