home *** CD-ROM | disk | FTP | other *** search
File List | 1990-07-10 | 7.8 KB | 441 lines |
- _OPTIMIZING WITH MICROSOFT C 6.0_
- by Scott Robert Ladd
-
- [LISTING ONE]
-
- ***********************
- *** Microsoft C 6.0 ***
- ***********************
-
- #include "stdio.h"
-
- /* prototypes */
-
- void doit(int i);
-
- void (* func_ptr)(int i) = doit;
-
- void doit(int i)
- {
- --> push bp
- --> mov bp,sp
- --> push di
- --> push si
- --> mov di,WORD PTR [bp+4]
-
- int loop;
-
- for (; i > 0; --i)
-
- --> or di,di
- --> jle $EX225
-
- {
- for (loop = 0; loop < 26; ++loop)
-
- --> $F227:
- --> sub si,si
- --> mov WORD PTR [bp+4],di
-
- {
- printf("loop character = %c\n", 0x41 + loop);
-
- --> $F230:
- --> lea ax,WORD PTR [si+65]
- --> push ax
- --> mov ax,OFFSET DGROUP:$SG233
- --> push ax
- --> call _printf
- --> add sp,4
- --> inc si
- --> cmp si,26
- --> jl $F230
- }
-
- printf("i / 16 = %d\n\n",i / 16);
-
- --> mov ax,di
- --> cwd
- --> xor ax,dx
- --> sub ax,dx
- --> mov cx,4
- --> sar ax,cl
- --> xor ax,dx
- --> sub ax,dx
- --> push ax
- --> mov ax,OFFSET DGROUP:$SG234
- --> push ax
- --> call _printf
- --> add sp,4
- --> dec di
- --> jne $F227
-
- }
-
- --> $EX225:
- --> pop si
- --> pop di
- --> mov sp,bp
- --> pop bp
- --> ret
- --> nop
- }
-
- int main(void)
- {
- func_ptr(100);
-
- --> mov ax,100
- --> push ax
- --> call WORD PTR _func_ptr
- --> add sp,2
-
- return 0;
-
- --> sub ax,ax
- --> ret
- }
-
-
-
- [LISTING TWO]
-
- *****************************************
- *** Microsoft C 6.0 (using _fastcall) ***
- *****************************************
-
- #include "stdio.h"
-
- /* prototypes */
-
- void doit(int i);
-
- void (* func_ptr)(int i) = doit;
-
- void doit(int i)
- {
-
- --> push bp
- --> mov bp,sp
- --> sub sp,2
- --> push ax
- --> push si
-
- int loop;
-
- for (; i > 0; --i)
-
- --> or ax,ax
- --> jle $EX225
-
- {
- for (loop = 0; loop < 26; ++loop)
-
- --> $F227:
- --> sub si,si
-
- {
- printf("loop character = %c\n", 0x41 + loop);
-
- --> $F230:
- --> lea ax,WORD PTR [si+65]
- --> push ax
- --> mov ax,OFFSET DGROUP:$SG233
- --> push ax
- --> call _printf
- --> add sp,4
- --> inc si
- --> cmp si,26
- --> jl $F230
-
- }
-
- printf("i / 16 = %d\n\n",i / 16);
-
- --> mov ax,WORD PTR [bp-4]
- --> cwd
- --> xor ax,dx
- --> sub ax,dx
- --> mov cx,4
- --> sar ax,cl
- --> xor ax,dx
- --> sub ax,dx
- --> push ax
- --> mov ax,OFFSET DGROUP:$SG234
- --> push ax
- --> call _printf
- --> add sp,4
- --> dec WORD PTR [bp-4]
- --> jne $F227
-
- }
-
- --> $EX225:
- --> pop si
- --> mov sp,bp
- --> pop bp
- --> ret
- }
-
- int main(void)
- {
- func_ptr(100);
-
- --> mov ax,100
- --> call WORD PTR _func_ptr
-
- return 0;
-
- --> sub ax,ax
- --> ret
-
- }
-
-
-
- [LISTING THREE]
-
- ************************
- *** Microsoft C 5.10 ***
- ************************
-
- #include "stdio.h"
-
- /* prototypes */
-
- void doit(int i);
-
- void (* func_ptr)(int i) = doit;
-
- void doit(int i)
- {
- --> push bp
- --> mov bp,sp
- --> sub sp,2
- --> push di
- --> push si
-
- int loop;
-
- for (; i > 0; --i)
-
- --> cmp WORD PTR [bp+4],0
- --> jle $FB202
- --> mov di,WORD PTR [bp+4]
-
- {
- for (loop = 0; loop < 26; ++loop)
-
- --> $L20002:
- --> sub si,si
-
- {
- printf("loop character = %c\n", 0x41 + loop);
-
- --> $L20000:
- --> lea ax,WORD PTR [si+65]
- --> push ax
- --> mov ax,OFFSET DGROUP:$SG206
- --> push ax
- --> call _printf
- --> add sp,4
- }
-
- --> inc si
- --> cmp si,26
- --> jl $L20000
- --> mov WORD PTR [bp-2],si ;loop
-
- printf("i / 16 = %d\n\n",i / 16);
-
- --> mov ax,di
- --> cwd
- --> xor ax,dx
- --> sub ax,dx
- --> mov cx,4
- --> sar ax,cl
- --> xor ax,dx
- --> sub ax,dx
- --> push ax
- --> mov ax,OFFSET DGROUP:$SG207
- --> push ax
- --> call _printf
- --> add sp,4
-
- }
-
- --> dec di
- --> jne $L20002
- --> mov WORD PTR [bp+4],di
-
- --> $FB202:
- --> pop si
- --> pop di
- --> mov sp,bp
- --> pop bp
- --> ret
- --> nop
-
- }
-
- int main(void)
- {
- func_ptr(100);
-
- --> mov ax,100
- --> push ax
- --> call WORD PTR _func_ptr
- --> add sp,2
-
- return 0;
-
- --> sub ax,ax
- --> ret
- }
-
-
-
- [LISTING FOUR]
-
- ********************
- *** Watcom C 7.0 ***
- ********************
-
- #include "stdio.h"
-
- /* prototypes */
-
- void doit(int i);
-
- void (* func_ptr)(int i) = doit;
-
- void doit(int i)
- {
- int loop;
-
- --> push bx
- --> push cx
- --> push dx
- --> mov cx,ax
- --> jmp short L3
-
- for (; i > 0; --i)
- {
- for (loop = 0; loop < 26; ++loop)
- {
- --> L1:
- --> mov bx,0041H
-
- printf("loop character = %c\n", 0x41 + loop);
-
- --> L2:
- --> push bx
- --> mov ax,offset DGROUP:L4
- --> push ax
- --> call near ptr printf_
- --> add sp,0004H
- --> inc bx
- --> cmp bx,005bH
- --> jne L2
-
- }
-
- printf("i / 16 = %d\n\n",i / 16);
-
- --> mov bx,0010H
- --> mov ax,cx
- --> cwd
- --> idiv bx
- --> push ax
- --> mov ax,offset DGROUP:L5
- --> push ax
- --> call near ptr printf_
- --> add sp,0004H
- --> dec cx
-
- }
-
- --> L3:
- --> test cx,cx
- --> jg L1
-
- --> pop dx
- --> pop cx
- --> pop bx
- --> ret
-
- }
-
- int main(void)
- {
-
- func_ptr(100);
-
- --> mov ax,0064H
- --> call word ptr _func_ptr
-
- return 0;
-
- --> xor ax,ax
- --> ret
-
- }
-
-
- [Listing Five]
- /* Skeleton Program demonstrating the use of based pointers */
-
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <malloc.h>
-
- #define MAX_TAG 2000
- unsigned long get_size(void);
- _segment segvar; /* name a segment for use with based pointers */
-
- /* set up structures and tags within segment segvar */
-
- typedef mytag {
- char filename[14];
- unsigned long size;
- mytag _based(segvar) *next;
- } _based(segvar) *PTAG, TAG;
-
- main() {
-
- PTAG head, curptr;
-
- /* Allocate a based heap of MAX_TAG structs. Put segment address in segvar. */
-
- if((segvar = _bheapseg(sizeof(TAG) * MAX_TAG))) == NULLSEG){
- printf("error allocating based heap \n");
- exit(-1);
- }
-
- /* Allocate memory within segvar for first structure in linked list */
-
- if((head = _bmalloc(segvar, sizeof(TAG)) == _NULLOFF) {
- printf("error allocating TAG \n");
- exit(-1);
- }
- head->size = get_size();
- _fstrcpy((char far *) head->filename, get_name()); /* get a
- filename and copy it to segvar */
-
- if((head->next = _bmalloc(segvar, sizeof(TAG)) == _NULLOFF) {
- printf("error allocating TAG \n");
- exit(-1);
- }
- .
- .
- .
- }
- unsigned long get_size(void) {
- return 1;
- }
-
- char *get_name(void) {
- return("foo");
- }
-