home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / msj / msjv4_4 / overlays / rdovly.asm < prev    next >
Encoding:
Assembly Source File  |  1989-01-30  |  9.8 KB  |  438 lines

  1. ;    Static Name Aliases
  2. ;
  3.     TITLE   rdovly.c
  4.     NAME    rdovly
  5.  
  6.     .8087
  7. RDOVLY_TEXT    SEGMENT  WORD PUBLIC 'CODE'
  8. RDOVLY_TEXT    ENDS
  9. _DATA    SEGMENT  WORD PUBLIC 'DATA'
  10. _DATA    ENDS
  11. CONST    SEGMENT  WORD PUBLIC 'CONST'
  12. CONST    ENDS
  13. _BSS    SEGMENT  WORD PUBLIC 'BSS'
  14. _BSS    ENDS
  15. DGROUP    GROUP    CONST, _BSS, _DATA
  16.     ASSUME  CS: RDOVLY_TEXT, DS: DGROUP, SS: DGROUP
  17. EXTRN    _myread:FAR
  18. EXTRN    _myopen:FAR
  19. EXTRN    _myclose:FAR
  20. EXTRN    _errputs:FAR
  21. EXTRN    _mylseek:FAR
  22. EXTRN    _executable_name:BYTE
  23. EXTRN    _pspaddr:WORD
  24. _DATA      SEGMENT
  25. $SG156    DB    'Something''s wrong...can''t read .EXE header',  0dH,  0aH,  00H
  26. $SG159    DB    'Found non-EXE signature!',  0dH,  0aH,  00H
  27. _DATA      ENDS
  28. RDOVLY_TEXT      SEGMENT
  29.     ASSUME    CS: RDOVLY_TEXT
  30. ;#include "exehdr.h"
  31. ;
  32. ;/* externally-defined full pathname of executable */
  33. ;
  34. ;#define min(a,b) ((a) < (b)) ? (a) : (b)
  35. ;#define max(a,b) ((a) < (b)) ? (a) : (b)
  36. ;#define OPENMODE_RO 0
  37. ;#define MK_FP(a,b) ((void far *)( ((unsigned long)(a) << 16) | (unsigned)(b) ))
  38. ;
  39. ;extern char executable_name[];
  40. ;extern int pspaddr;
  41. ;
  42. ;void errputs(char far *s);
  43. ;long mylseek(int handle, long offset, int type);
  44. ;int myread(int handle, void far *buf, int len);
  45. ;int myopen(char far *name, int mode);
  46. ;int myclose(int handle);
  47. ;
  48. ;
  49. ;int read_overlay_section(
  50. ;void far *ovarea,                   /* buffer into which to read him */
  51. ;int requested_overlay_number        /* number of overlay to read */
  52. ;)
  53. ;
  54. ;#define RELOC_BUF_SIZE 32
  55. ;{
  56.     PUBLIC    _read_overlay_section
  57. _read_overlay_section    PROC FAR
  58.     push    bp
  59.     mov    bp,sp
  60.     sub    sp,190
  61.     push    di
  62.     push    si
  63. ;    ovarea = 6
  64. ;    requested_overlay_number = 10
  65. ;    reloc_buf = -140
  66. ;    reloc_ptr = -146
  67. ;    reloc_chunk = -186
  68. ;    target_ptr = -154
  69. ;    eh = -184
  70. ;    i = -142
  71. ;    startpos = -8
  72. ;    nextpos = -12
  73. ;    filesize = -150
  74. ;    sectionsize = -4
  75. ;    imagesize = -190
  76. ;    executable_handle = -156
  77. ;    struct reloc_entry_ reloc_buf[RELOC_BUF_SIZE];
  78. ;    struct reloc_entry_ *reloc_ptr;
  79. ;    int reloc_chunk;
  80. ;    unsigned far *target_ptr;
  81. ;    struct exehdr_ eh;
  82. ;    int i;
  83. ;    long startpos, nextpos, filesize;
  84. ;    long sectionsize, imagesize;
  85. ;    int executable_handle;
  86. ;
  87. ;    /* open executable */
  88. ;    executable_handle = myopen(executable_name,OPENMODE_RO);
  89.     mov    ax,0
  90.     push    ax
  91.     mov    ax,OFFSET _executable_name
  92.     mov    dx,SEG _executable_name
  93.     push    dx
  94.     push    ax
  95.     call    FAR PTR _myopen
  96.     add    sp,6
  97.     mov    WORD PTR [bp-156],ax    ;executable_handle
  98. ;
  99. ;    /* determine file size */
  100. ;    filesize = mylseek(executable_handle,0L,2);
  101.     mov    ax,2
  102.     push    ax
  103.     sub    ax,ax
  104.     push    ax
  105.     push    ax
  106.     push    WORD PTR [bp-156]    ;executable_handle
  107.     call    FAR PTR _mylseek
  108.     add    sp,8
  109.     mov    WORD PTR [bp-150],ax    ;filesize
  110.     mov    WORD PTR [bp-148],dx
  111. ;
  112. ;    /* search from beginning of file */
  113. ;    mylseek(executable_handle,0L,0);
  114.     mov    ax,0
  115.     push    ax
  116.     sub    ax,ax
  117.     push    ax
  118.     push    ax
  119.     push    WORD PTR [bp-156]    ;executable_handle
  120.     call    FAR PTR _mylseek
  121.     add    sp,8
  122. ;
  123. ;    while (1) {
  124. $FC153:
  125. ;        /* use mylseek() to avoid calling runtime functions */
  126. ;        startpos = mylseek(executable_handle,0L,1);
  127.     mov    ax,1
  128.     push    ax
  129.     sub    ax,ax
  130.     push    ax
  131.     push    ax
  132.     push    WORD PTR [bp-156]    ;executable_handle
  133.     call    FAR PTR _mylseek
  134.     add    sp,8
  135.     mov    WORD PTR [bp-8],ax    ;startpos
  136.     mov    WORD PTR [bp-6],dx
  137. ;        if (myread(executable_handle,&eh,sizeof(eh)) != sizeof(eh)) {
  138.     mov    ax,28
  139.     push    ax
  140.     lea    ax,WORD PTR [bp-184]    ;eh
  141.     push    ss
  142.     push    ax
  143.     push    WORD PTR [bp-156]    ;executable_handle
  144.     call    FAR PTR _myread
  145.     add    sp,8
  146.     cmp    ax,28
  147.     jne    $JCC132
  148.     jmp    $I155
  149. $JCC132:
  150. ;            errputs("Something's wrong...can't read .EXE header\r\n");
  151.     mov    ax,OFFSET DGROUP:$SG156
  152.     push    ds
  153.     push    ax
  154.     call    FAR PTR _errputs
  155.     add    sp,4
  156. ;            myclose(executable_handle);
  157.     push    WORD PTR [bp-156]    ;executable_handle
  158.     call    FAR PTR _myclose
  159.     add    sp,2
  160. ;            return(1);
  161.     mov    ax,1
  162.     jmp    $EX139
  163. ;        }
  164. ;
  165. ;        if (eh.M_sig != 'M' || eh.Z_sig != 'Z') {
  166. $I155:
  167.     cmp    BYTE PTR [bp-184],77    ;eh
  168.     je    $JCC173
  169.     jmp    $I158
  170. $JCC173:
  171.     cmp    BYTE PTR [bp-183],90
  172.     jne    $JCC183
  173.     jmp    $I157
  174. $JCC183:
  175. $I158:
  176. ;            errputs("Found non-EXE signature!\r\n");
  177.     mov    ax,OFFSET DGROUP:$SG159
  178.     push    ds
  179.     push    ax
  180.     call    FAR PTR _errputs
  181.     add    sp,4
  182. ;            myclose(executable_handle);
  183.     push    WORD PTR [bp-156]    ;executable_handle
  184.     call    FAR PTR _myclose
  185.     add    sp,2
  186. ;            return(1);
  187.     mov    ax,1
  188.     jmp    $EX139
  189. ;        }
  190. ;        if (eh.remain_len == 0)
  191. $I157:
  192.     cmp    WORD PTR [bp-182],0
  193.     je    $JCC224
  194.     jmp    $I160
  195. $JCC224:
  196. ;            sectionsize = (long)eh.page_len * 512;
  197.     mov    ax,WORD PTR [bp-180]
  198.     sub    dx,dx
  199.     shl    ax,1
  200.     rcl    dx,1
  201.     mov    dh,dl
  202.     mov    dl,ah
  203.     mov    ah,al
  204.     sub    al,al
  205.     mov    WORD PTR [bp-4],ax    ;sectionsize
  206.     mov    WORD PTR [bp-2],dx
  207. ;        else
  208.     jmp    $I161
  209. $I160:
  210. ;            sectionsize = (long)(eh.page_len - 1) * 512 + eh.remain_len;
  211.     mov    ax,WORD PTR [bp-180]
  212.     dec    ax
  213.     sub    dx,dx
  214.     shl    ax,1
  215.     rcl    dx,1
  216.     mov    dh,dl
  217.     mov    dl,ah
  218.     mov    ah,al
  219.     sub    al,al
  220.     add    ax,WORD PTR [bp-182]
  221.     adc    dx,0
  222.     mov    WORD PTR [bp-4],ax    ;sectionsize
  223.     mov    WORD PTR [bp-2],dx
  224. $I161:
  225. ;
  226. ;        if (eh.overlay_number == requested_overlay_number) {
  227.     mov    al,BYTE PTR [bp-158]
  228.     sub    ah,ah
  229.     cmp    ax,WORD PTR [bp+10]    ;requested_overlay_number
  230.     je    $JCC297
  231.     jmp    $I162
  232. $JCC297:
  233. ;            /* found ours...load and fix up */
  234. ;
  235. ;            /* move to executable image */
  236. ;            mylseek(executable_handle, startpos + eh.hsize * 16, 0);
  237.     mov    ax,0
  238.     push    ax
  239.     mov    ax,WORD PTR [bp-176]
  240.     mov    cl,4
  241.     shl    ax,cl
  242.     sub    dx,dx
  243.     add    ax,WORD PTR [bp-8]    ;startpos
  244.     adc    dx,WORD PTR [bp-6]
  245.     push    dx
  246.     push    ax
  247.     push    WORD PTR [bp-156]    ;executable_handle
  248.     call    FAR PTR _mylseek
  249.     add    sp,8
  250. ;
  251. ;            myread(executable_handle, ovarea, (int)(sectionsize - eh.hsize * 16));
  252.     mov    ax,WORD PTR [bp-4]    ;sectionsize
  253.     mov    dx,WORD PTR [bp-176]
  254.     mov    cl,4
  255.     shl    dx,cl
  256.     sub    ax,dx
  257.     push    ax
  258.     push    WORD PTR [bp+8]
  259.     push    WORD PTR [bp+6]    ;ovarea
  260.     push    WORD PTR [bp-156]    ;executable_handle
  261.     call    FAR PTR _myread
  262.     add    sp,8
  263. ;
  264. ;            /* fix up relocations in the loaded overlay */
  265. ;
  266. ;            mylseek(executable_handle,startpos + (long)eh.first_reloc,0);
  267.     mov    ax,0
  268.     push    ax
  269.     mov    ax,WORD PTR [bp-160]
  270.     sub    dx,dx
  271.     add    ax,WORD PTR [bp-8]    ;startpos
  272.     adc    dx,WORD PTR [bp-6]
  273.     push    dx
  274.     push    ax
  275.     push    WORD PTR [bp-156]    ;executable_handle
  276.     call    FAR PTR _mylseek
  277.     add    sp,8
  278. ;            while (eh.num_relocs) {
  279. $FC164:
  280.     cmp    WORD PTR [bp-178],0
  281.     jne    $JCC403
  282.     jmp    $FB165
  283. $JCC403:
  284. ;                reloc_chunk = min(RELOC_BUF_SIZE,eh.num_relocs);
  285.     mov    ax,WORD PTR [bp-178]
  286.     sub    ax,32
  287.     sbb    cx,cx
  288.     and    ax,cx
  289.     add    ax,32
  290.     mov    WORD PTR [bp-186],ax    ;reloc_chunk
  291. ;                myread(executable_handle,reloc_buf,reloc_chunk * sizeof(struct reloc_entry_));
  292.     mov    ax,WORD PTR [bp-186]    ;reloc_chunk
  293.     shl    ax,1
  294.     shl    ax,1
  295.     push    ax
  296.     lea    ax,WORD PTR [bp-140]    ;reloc_buf
  297.     push    ss
  298.     push    ax
  299.     push    WORD PTR [bp-156]    ;executable_handle
  300.     call    FAR PTR _myread
  301.     add    sp,8
  302. ;                eh.num_relocs -= reloc_chunk;
  303.     mov    ax,WORD PTR [bp-186]    ;reloc_chunk
  304.     sub    WORD PTR [bp-178],ax
  305. ;                for (i = 0; i < reloc_chunk; i++) {
  306.     mov    WORD PTR [bp-142],0    ;i
  307.     jmp    $F166
  308. $FC167:
  309.     inc    WORD PTR [bp-142]    ;i
  310. $F166:
  311.     mov    ax,WORD PTR [bp-186]    ;reloc_chunk
  312.     cmp    WORD PTR [bp-142],ax    ;i
  313.     jl    $JCC482
  314.     jmp    $FB168
  315. $JCC482:
  316. ;                    reloc_ptr = reloc_buf + i;
  317.     mov    si,WORD PTR [bp-142]    ;i
  318.     shl    si,1
  319.     shl    si,1
  320.     lea    ax,[bp-140][si]
  321.     mov    WORD PTR [bp-146],ax    ;reloc_ptr
  322.     mov    WORD PTR [bp-144],ss
  323. ;                    target_ptr = MK_FP(pspaddr + 0x10 + reloc_ptr->segment,
  324.     les    bx,DWORD PTR [bp-146]    ;reloc_ptr
  325.     mov    ax,WORD PTR es:[bx+2]
  326. RDOVLY_TEXT      ENDS
  327. CONST      SEGMENT
  328. $T20002    DW SEG _pspaddr 
  329. CONST      ENDS
  330. RDOVLY_TEXT      SEGMENT
  331.     ASSUME    CS: RDOVLY_TEXT
  332.     mov    es,$T20002
  333.     add    ax,es:_pspaddr
  334.     add    ax,16
  335.     sub    dx,dx
  336.     mov    dx,ax
  337.     sub    ax,ax
  338.     les    bx,DWORD PTR [bp-146]    ;reloc_ptr
  339.     or    ax,WORD PTR es:[bx]
  340.     mov    WORD PTR [bp-154],ax    ;target_ptr
  341.     mov    WORD PTR [bp-152],dx
  342. ;                                       reloc_ptr->offset);
  343. ;                    *target_ptr += pspaddr + 0x10;
  344.     mov    es,$T20002
  345.     mov    ax,es:_pspaddr
  346.     add    ax,16
  347.     les    bx,DWORD PTR [bp-154]    ;target_ptr
  348.     add    WORD PTR es:[bx],ax
  349. ;                }
  350.     jmp    $FC167
  351. $FB168:
  352. ;            }
  353.     jmp    $FC164
  354. $FB165:
  355. ;            myclose(executable_handle);
  356.     push    WORD PTR [bp-156]    ;executable_handle
  357.     call    FAR PTR _myclose
  358.     add    sp,2
  359. ;            return 0;
  360.     mov    ax,0
  361.     jmp    $EX139
  362. ;        } else {
  363.     jmp    $I169
  364. $I162:
  365. ;            nextpos = startpos + sectionsize;
  366.     mov    ax,WORD PTR [bp-8]    ;startpos
  367.     mov    dx,WORD PTR [bp-6]
  368.     add    ax,WORD PTR [bp-4]    ;sectionsize
  369.     adc    dx,WORD PTR [bp-2]
  370.     mov    WORD PTR [bp-12],ax    ;nextpos
  371.     mov    WORD PTR [bp-10],dx
  372. ;            /* round up to 512-byte bound */
  373. ;            nextpos = (nextpos + 511L) & ~511L;
  374.     mov    ax,WORD PTR [bp-12]    ;nextpos
  375.     mov    dx,WORD PTR [bp-10]
  376.     add    ax,511
  377.     adc    dx,0
  378.     and    ax,-512
  379.     and    dx,-1
  380.     mov    WORD PTR [bp-12],ax    ;nextpos
  381.     mov    WORD PTR [bp-10],dx
  382. ;            if (nextpos >= filesize) {
  383.     mov    ax,WORD PTR [bp-150]    ;filesize
  384.     mov    dx,WORD PTR [bp-148]
  385.     cmp    WORD PTR [bp-10],dx
  386.     jge    $JCC646
  387.     jmp    $I170
  388. $JCC646:
  389.     jle    $JCC651
  390.     jmp    $L20003
  391. $JCC651:
  392.     cmp    WORD PTR [bp-12],ax    ;nextpos
  393.     jae    $JCC659
  394.     jmp    $I170
  395. $JCC659:
  396. $L20003:
  397. ;                myclose(executable_handle);
  398.     push    WORD PTR [bp-156]    ;executable_handle
  399.     call    FAR PTR _myclose
  400.     add    sp,2
  401. ;                return 1;
  402.     mov    ax,1
  403.     jmp    $EX139
  404. ;            }
  405. ;            mylseek(executable_handle,nextpos,0);
  406. $I170:
  407.     mov    ax,0
  408.     push    ax
  409.     push    WORD PTR [bp-10]
  410.     push    WORD PTR [bp-12]    ;nextpos
  411.     push    WORD PTR [bp-156]    ;executable_handle
  412.     call    FAR PTR _mylseek
  413.     add    sp,8
  414. ;        }
  415. $I169:
  416. ;    }
  417.     jmp    $FC153
  418. $FB154:
  419. ;    myclose(executable_handle);
  420.     push    WORD PTR [bp-156]    ;executable_handle
  421.     call    FAR PTR _myclose
  422.     add    sp,2
  423. ;    return 1;
  424.     mov    ax,1
  425.     jmp    $EX139
  426. ;}
  427. $EX139:
  428.     pop    si
  429.     pop    di
  430.     mov    sp,bp
  431.     pop    bp
  432.     ret    
  433.  
  434. _read_overlay_section    ENDP
  435.     nop    
  436. RDOVLY_TEXT    ENDS
  437. END
  438.