diff options
Diffstat (limited to 'v2.0/source/EXE2BIN.ASM')
| -rw-r--r-- | v2.0/source/EXE2BIN.ASM | 514 |
1 files changed, 514 insertions, 0 deletions
diff --git a/v2.0/source/EXE2BIN.ASM b/v2.0/source/EXE2BIN.ASM new file mode 100644 index 0000000..80deb43 --- /dev/null +++ b/v2.0/source/EXE2BIN.ASM | |||
| @@ -0,0 +1,514 @@ | |||
| 1 | title LOCATE (EXE2BIN) | ||
| 2 | |||
| 3 | ;Loader for EXE files under 86-DOS | ||
| 4 | |||
| 5 | ;The following switch allows use with the "old linker", which put a version | ||
| 6 | ;number where the new linker puts the number of bytes used in the last page. | ||
| 7 | ;If enabled, this will cause a test for 0004 at this location (the old linker | ||
| 8 | ;version number), and if equal, change it to 200H so all of the last page | ||
| 9 | ;will be used. | ||
| 10 | |||
| 11 | ;VER. 1.5 | ||
| 12 | ; 05/21/82 Added rev number | ||
| 13 | ; | ||
| 14 | ;VER. 1.6 | ||
| 15 | ; 07/01/82 A little less choosy about size matches | ||
| 16 | ; | ||
| 17 | ;VER. 2.0 Rev. 1 M.A.Ulloa | ||
| 18 | ; 10/08/82 Modified to use new 2.0 system calls for file i/o | ||
| 19 | ; | ||
| 20 | ; Rev. 2 M.A.Ulloa | ||
| 21 | ; 10/27/82 Added the DOS version check | ||
| 22 | |||
| 23 | FALSE EQU 0 | ||
| 24 | TRUE EQU NOT FALSE | ||
| 25 | |||
| 26 | OLDLINK EQU 0 ;1 to enable, 0 to disable | ||
| 27 | |||
| 28 | .xlist | ||
| 29 | INCLUDE DOSSYM.ASM | ||
| 30 | .list | ||
| 31 | |||
| 32 | |||
| 33 | subttl Main Code Area | ||
| 34 | page | ||
| 35 | |||
| 36 | |||
| 37 | code segment byte | ||
| 38 | code ends | ||
| 39 | |||
| 40 | DATA SEGMENT PUBLIC BYTE | ||
| 41 | |||
| 42 | |||
| 43 | EXTRN bad_vers_err:BYTE,NOTFND:BYTE,NOROOM:BYTE,DIRFULL:BYTE | ||
| 44 | EXTRN CANTFIX:BYTE,RDBAD:BYTE,FULL:BYTE,PROMPT:BYTE,CRLF:BYTE | ||
| 45 | |||
| 46 | make db "MAUlloa/Microsoft/V20" | ||
| 47 | rev db "2" | ||
| 48 | |||
| 49 | file1_ext db ".EXE",00h | ||
| 50 | file2_ext db ".BIN",00h | ||
| 51 | |||
| 52 | per1 db 0 | ||
| 53 | per2 db 0 | ||
| 54 | |||
| 55 | file1 db 64 dup(?) | ||
| 56 | handle1 dw 1 dup(?) | ||
| 57 | |||
| 58 | file2 db 64 dup(?) | ||
| 59 | handle2 dw 1 dup(?) | ||
| 60 | |||
| 61 | |||
| 62 | INBUF DB 5,0 | ||
| 63 | DB 5 DUP(?) | ||
| 64 | |||
| 65 | ;The following locations must be defined for storing the header: | ||
| 66 | |||
| 67 | RUNVAR LABEL BYTE ;Start of RUN variables | ||
| 68 | RELPT DW ? | ||
| 69 | LASTP LABEL WORD | ||
| 70 | RELSEG DW ? | ||
| 71 | SIZ LABEL WORD ;Share these locations | ||
| 72 | PAGES DW ? | ||
| 73 | RELCNT DW ? | ||
| 74 | HEADSIZ DW ? | ||
| 75 | DW ? | ||
| 76 | LOADLOW DW ? | ||
| 77 | INITSS DW ? | ||
| 78 | INITSP DW ? | ||
| 79 | DW ? | ||
| 80 | INITIP DW ? | ||
| 81 | INITCS DW ? | ||
| 82 | RELTAB DW ? | ||
| 83 | RUNVARSIZ EQU $-RUNVAR | ||
| 84 | |||
| 85 | DATA ENDS | ||
| 86 | |||
| 87 | STACK SEGMENT WORD STACK | ||
| 88 | DB 80H DUP (?) | ||
| 89 | STACK ENDS | ||
| 90 | |||
| 91 | ZLOAD SEGMENT | ||
| 92 | ZLOAD ENDS | ||
| 93 | LOAD EQU ZLOAD | ||
| 94 | |||
| 95 | CODE SEGMENT BYTE | ||
| 96 | |||
| 97 | ASSUME CS:CODE | ||
| 98 | |||
| 99 | LOCATE PROC FAR | ||
| 100 | JMP SHORT LOCSTRT | ||
| 101 | |||
| 102 | HEADER DB "Vers 2.00" | ||
| 103 | |||
| 104 | LOCSTRT: | ||
| 105 | MOV SI,81H | ||
| 106 | PUSH DS | ||
| 107 | XOR AX,AX | ||
| 108 | PUSH AX ;Push return address to DS:0 | ||
| 109 | |||
| 110 | ;Code to print header | ||
| 111 | ; PUSH DS | ||
| 112 | ; MOV DX,DATA | ||
| 113 | ; MOV DS,DX | ||
| 114 | ; MOV DX,OFFSET HEADER | ||
| 115 | ; MOV AH,STD_CON_STRING_OUTPUT | ||
| 116 | ; INT 21H | ||
| 117 | ; POP DS | ||
| 118 | |||
| 119 | ;----- Check Version Number --------------------------------------------; | ||
| 120 | mov ah,Get_Version | ||
| 121 | int 21h | ||
| 122 | cmp al,2 | ||
| 123 | jge vers_ok ; version >= 2, enter locate | ||
| 124 | push ds | ||
| 125 | mov dx,data | ||
| 126 | mov ds,dx | ||
| 127 | mov dx,offset bad_vers_err | ||
| 128 | MOV AH,STD_CON_STRING_OUTPUT | ||
| 129 | INT 21H | ||
| 130 | pop ds | ||
| 131 | ret ;long return to DOS | ||
| 132 | |||
| 133 | ;-----------------------------------------------------------------------; | ||
| 134 | vers_ok: | ||
| 135 | |||
| 136 | |||
| 137 | MOV BX,WORD PTR DS:2 ;Get size of memory | ||
| 138 | MOV DX,DATA | ||
| 139 | MOV ES,DX | ||
| 140 | |||
| 141 | assume es:data | ||
| 142 | |||
| 143 | ;-----------------------------------------------------------------------; | ||
| 144 | |||
| 145 | ;----- Get the first file name | ||
| 146 | call kill_bl | ||
| 147 | jnc sj01 | ||
| 148 | mov ds,dx | ||
| 149 | jmp bad_file | ||
| 150 | sj01: | ||
| 151 | mov di,offset file1 | ||
| 152 | sj0: | ||
| 153 | lodsb ;get character of file name | ||
| 154 | cmp al,' ' | ||
| 155 | je sj2 | ||
| 156 | cmp al,0dh | ||
| 157 | je sj2 | ||
| 158 | cmp al,'.' ;an extension separator found? | ||
| 159 | jne sj1 | ||
| 160 | mov es:[per1],-1 | ||
| 161 | sj1: | ||
| 162 | stosb | ||
| 163 | jmp short sj0 | ||
| 164 | sj2: | ||
| 165 | dec si | ||
| 166 | mov byte ptr es:[di],00h ;nul terminate the filename | ||
| 167 | call kill_bl | ||
| 168 | jc no_second | ||
| 169 | |||
| 170 | ;----- Get the second file name | ||
| 171 | mov di,offset file2 | ||
| 172 | sj3: | ||
| 173 | lodsb ;get character of file name | ||
| 174 | cmp al,' ' | ||
| 175 | je sj5 | ||
| 176 | cmp al,0dh | ||
| 177 | je sj5 | ||
| 178 | cmp al,'.' ;an extension separator found? | ||
| 179 | jne sj4 | ||
| 180 | mov es:[per2],-1 | ||
| 181 | sj4: | ||
| 182 | stosb | ||
| 183 | jmp short sj3 | ||
| 184 | sj5: | ||
| 185 | mov byte ptr es:[di],00h ;nul terminate | ||
| 186 | jmp short check_ext | ||
| 187 | |||
| 188 | ;----- Copy file1 to file2 | ||
| 189 | no_second: | ||
| 190 | mov ds,dx | ||
| 191 | |||
| 192 | assume ds:data | ||
| 193 | |||
| 194 | mov si,offset file1 | ||
| 195 | mov di,offset file2 | ||
| 196 | sj6: | ||
| 197 | lodsb | ||
| 198 | cmp al,'.' | ||
| 199 | je sj7 | ||
| 200 | cmp al,00h | ||
| 201 | je sj7 | ||
| 202 | stosb | ||
| 203 | jmp short sj6 | ||
| 204 | sj7: | ||
| 205 | mov byte ptr [di],00h | ||
| 206 | |||
| 207 | ;----- Check that files have an extension, otherwise set default | ||
| 208 | check_ext: | ||
| 209 | mov ds,dx | ||
| 210 | |||
| 211 | assume ds:data | ||
| 212 | |||
| 213 | cmp [per1],-1 | ||
| 214 | je file1_ok | ||
| 215 | mov si,offset file1 | ||
| 216 | sj8: | ||
| 217 | lodsb | ||
| 218 | cmp al,00h | ||
| 219 | jne sj8 | ||
| 220 | mov di,si | ||
| 221 | mov si,offset file1_ext | ||
| 222 | call put_ext | ||
| 223 | |||
| 224 | file1_ok: | ||
| 225 | cmp [per2],-1 | ||
| 226 | je file2_ok | ||
| 227 | mov si,offset file2 | ||
| 228 | sj9: | ||
| 229 | lodsb | ||
| 230 | cmp al,00h | ||
| 231 | jne sj9 | ||
| 232 | mov di,si | ||
| 233 | mov si,offset file2_ext | ||
| 234 | call put_ext | ||
| 235 | jmp short file2_ok | ||
| 236 | |||
| 237 | ;----- Fill in the default extent | ||
| 238 | put_ext proc near | ||
| 239 | dec di | ||
| 240 | mov cx,5 ;move extent: period,extent,null | ||
| 241 | rep movsb | ||
| 242 | ret | ||
| 243 | put_ext endp | ||
| 244 | |||
| 245 | ;----- Find the first non-blank | ||
| 246 | kill_bl proc near | ||
| 247 | cld | ||
| 248 | sj10: | ||
| 249 | lodsb | ||
| 250 | cmp al,' ' | ||
| 251 | je sj10 | ||
| 252 | dec si | ||
| 253 | cmp al,0dh | ||
| 254 | clc | ||
| 255 | jne sj11 | ||
| 256 | stc | ||
| 257 | sj11: | ||
| 258 | ret | ||
| 259 | kill_bl endp | ||
| 260 | |||
| 261 | file2_ok: | ||
| 262 | |||
| 263 | ;-----------------------------------------------------------------------; | ||
| 264 | |||
| 265 | mov dx,offset file1 | ||
| 266 | mov ah,open | ||
| 267 | mov al,0 ;ror reading only | ||
| 268 | INT 21H ;Open input file | ||
| 269 | jc bad_file | ||
| 270 | mov [handle1],ax | ||
| 271 | jmp exeload | ||
| 272 | |||
| 273 | bad_file: | ||
| 274 | MOV DX,OFFSET NOTFND | ||
| 275 | xERROR: | ||
| 276 | MOV AH,STD_CON_STRING_OUTPUT | ||
| 277 | INT 21H | ||
| 278 | RET ;FAR return to MS-DOS | ||
| 279 | TOOBIG: | ||
| 280 | MOV DX,OFFSET NOROOM | ||
| 281 | JMP xERROR | ||
| 282 | BADEXE: | ||
| 283 | MOV DX,OFFSET CANTFIX | ||
| 284 | ERRORJ: JMP xERROR | ||
| 285 | |||
| 286 | EXELOAD: | ||
| 287 | MOV DX,OFFSET RUNVAR ;Read header in here | ||
| 288 | MOV CX,RUNVARSIZ ;Amount of header info we need | ||
| 289 | push bx | ||
| 290 | mov bx,[handle1] | ||
| 291 | MOV AH,read | ||
| 292 | INT 21H ;Read in header | ||
| 293 | pop bx | ||
| 294 | CMP [RELPT],5A4DH ;Check signature word | ||
| 295 | JNZ BADEXE | ||
| 296 | MOV AX,[HEADSIZ] ;size of header in paragraphs | ||
| 297 | ADD AX,31 ;Round up first | ||
| 298 | CMP AX,1000H ;Must not be >=64K | ||
| 299 | JAE TOOBIG | ||
| 300 | AND AX,NOT 31 | ||
| 301 | MOV CL,4 | ||
| 302 | SHL AX,CL ;Header size in bytes | ||
| 303 | |||
| 304 | push dx | ||
| 305 | push cx | ||
| 306 | push ax | ||
| 307 | push bx | ||
| 308 | mov dx,ax | ||
| 309 | xor cx,cx | ||
| 310 | mov al,0 | ||
| 311 | mov bx,[handle1] | ||
| 312 | mov ah,lseek | ||
| 313 | int 21h | ||
| 314 | pop bx | ||
| 315 | pop ax | ||
| 316 | pop cx | ||
| 317 | pop dx | ||
| 318 | |||
| 319 | XCHG AL,AH | ||
| 320 | SHR AX,1 ;Convert to pages | ||
| 321 | MOV DX,[PAGES] ;Total size of file in 512-byte pages | ||
| 322 | SUB DX,AX ;Size of program in pages | ||
| 323 | CMP DX,80H ;Fit in 64K? | ||
| 324 | JAE TOOBIG | ||
| 325 | XCHG DH,DL | ||
| 326 | SHL DX,1 ;Convert pages to bytes | ||
| 327 | MOV AX,[LASTP] ;Get count of bytes in last page | ||
| 328 | OR AX,AX ;If zero, use all of last page | ||
| 329 | JZ WHOLEP | ||
| 330 | |||
| 331 | IF OLDLINK | ||
| 332 | CMP AX,4 ;Produced by old linker? | ||
| 333 | JZ WHOLEP ;If so, use all of last page too | ||
| 334 | ENDIF | ||
| 335 | |||
| 336 | SUB DX,200H ;Subtract last page | ||
| 337 | ADD DX,AX ;Add in byte count for last page | ||
| 338 | WHOLEP: | ||
| 339 | MOV [SIZ],DX | ||
| 340 | ADD DX,15 | ||
| 341 | SHR DX,CL ;Convert bytes to paragraphs | ||
| 342 | MOV BP,LOAD | ||
| 343 | ADD DX,BP ;Size + start = minimum memory (paragr.) | ||
| 344 | CMP DX,BX ;Enough memory? | ||
| 345 | JA TOOBIG | ||
| 346 | MOV DX,OFFSET CANTFIX | ||
| 347 | MOV AX,[INITSS] | ||
| 348 | OR AX,[INITSP] | ||
| 349 | OR AX,[INITCS] | ||
| 350 | ERRORNZ: | ||
| 351 | jz xj | ||
| 352 | JMP ERRORJ ;Must not have SS, SP, or CS to init. | ||
| 353 | xj: MOV AX,[INITIP] | ||
| 354 | OR AX,AX ;If IP=0, do binary fix | ||
| 355 | JZ BINFIX | ||
| 356 | CMP AX,100H ;COM file must be set up for CS:100 | ||
| 357 | JNZ ERRORNZ | ||
| 358 | |||
| 359 | push dx | ||
| 360 | push cx | ||
| 361 | push ax | ||
| 362 | push bx | ||
| 363 | mov dx,100h ;chop off first 100h | ||
| 364 | xor cx,cx | ||
| 365 | mov al,1 ;seek from current position | ||
| 366 | mov bx,[handle1] | ||
| 367 | mov ah,lseek | ||
| 368 | int 21h | ||
| 369 | pop bx | ||
| 370 | pop ax | ||
| 371 | pop cx | ||
| 372 | pop dx | ||
| 373 | |||
| 374 | SUB [SIZ],AX ;And count decreased size | ||
| 375 | CMP [RELCNT],0 ;Must have no fixups | ||
| 376 | JNZ ERRORNZ | ||
| 377 | BINFIX: | ||
| 378 | XOR BX,BX ;Initialize fixup segment | ||
| 379 | ;See if segment fixups needed | ||
| 380 | CMP [RELCNT],0 | ||
| 381 | JZ LOADEXE | ||
| 382 | GETSEG: | ||
| 383 | MOV DX,OFFSET PROMPT | ||
| 384 | MOV AH,STD_CON_STRING_OUTPUT | ||
| 385 | INT 21H | ||
| 386 | MOV AH,STD_CON_STRING_INPUT | ||
| 387 | MOV DX,OFFSET INBUF | ||
| 388 | INT 21H ;Get user response | ||
| 389 | MOV DX,OFFSET CRLF | ||
| 390 | MOV AH,STD_CON_STRING_OUTPUT | ||
| 391 | INT 21H | ||
| 392 | MOV SI,OFFSET INBUF+2 | ||
| 393 | MOV BYTE PTR [SI-1],0 ;Any digits? | ||
| 394 | JZ GETSEG | ||
| 395 | DIGLP: | ||
| 396 | LODSB | ||
| 397 | SUB AL,"0" | ||
| 398 | JC DIGERR | ||
| 399 | CMP AL,10 | ||
| 400 | JB HAVDIG | ||
| 401 | AND AL,5FH ;Convert to upper case | ||
| 402 | SUB AL,7 | ||
| 403 | CMP AL,10 | ||
| 404 | JB DIGERR | ||
| 405 | CMP AL,10H | ||
| 406 | JAE DIGERR | ||
| 407 | HAVDIG: | ||
| 408 | SHL BX,1 | ||
| 409 | SHL BX,1 | ||
| 410 | SHL BX,1 | ||
| 411 | SHL BX,1 | ||
| 412 | OR BL,AL | ||
| 413 | JMP DIGLP | ||
| 414 | |||
| 415 | DIGERR: | ||
| 416 | CMP BYTE PTR [SI-1],0DH ;Is last char. a CR? | ||
| 417 | JNZ GETSEG | ||
| 418 | LOADEXE: | ||
| 419 | XCHG BX,BP ;BX has LOAD, BP has fixup | ||
| 420 | |||
| 421 | MOV CX,[SIZ] | ||
| 422 | MOV AH,read | ||
| 423 | push di | ||
| 424 | mov di,[handle1] | ||
| 425 | PUSH DS | ||
| 426 | MOV DS,BX | ||
| 427 | XOR DX,DX | ||
| 428 | push bx | ||
| 429 | mov bx,di | ||
| 430 | INT 21H ;Read in up to 64K | ||
| 431 | pop bx | ||
| 432 | POP DS | ||
| 433 | pop di | ||
| 434 | Jnc HAVEXE ;Did we get it all? | ||
| 435 | MOV DX,OFFSET RDBAD | ||
| 436 | jmp xERROR ;Non fatal, print warning | ||
| 437 | HAVEXE: | ||
| 438 | CMP [RELCNT],0 ;Any fixups to do? | ||
| 439 | JZ STORE | ||
| 440 | MOV AX,[RELTAB] ;Get position of table | ||
| 441 | |||
| 442 | push dx | ||
| 443 | push cx | ||
| 444 | push ax | ||
| 445 | push bx | ||
| 446 | mov dx,ax | ||
| 447 | xor cx,cx | ||
| 448 | mov al,0 | ||
| 449 | mov bx,[handle1] | ||
| 450 | mov ah,lseek | ||
| 451 | int 21h | ||
| 452 | pop bx | ||
| 453 | pop ax | ||
| 454 | pop cx | ||
| 455 | pop dx | ||
| 456 | |||
| 457 | MOV DX,OFFSET RELPT ;4-byte buffer for relocation address | ||
| 458 | RELOC: | ||
| 459 | MOV DX,OFFSET RELPT ;4-byte buffer for relocation address | ||
| 460 | MOV CX,4 | ||
| 461 | MOV AH,read | ||
| 462 | push bx | ||
| 463 | mov bx,[handle1] | ||
| 464 | INT 21H ;Read in one relocation pointer | ||
| 465 | pop bx | ||
| 466 | Jnc RDCMP | ||
| 467 | JMP BADEXE | ||
| 468 | RDCMP: | ||
| 469 | MOV DI,[RELPT] ;Get offset of relocation pointer | ||
| 470 | MOV AX,[RELSEG] ;Get segment | ||
| 471 | ADD AX,BX ;Bias segment with actual load segment | ||
| 472 | MOV ES,AX | ||
| 473 | ADD ES:[DI],BP ;Relocate | ||
| 474 | DEC [RELCNT] ;Count off | ||
| 475 | JNZ RELOC | ||
| 476 | STORE: | ||
| 477 | MOV AH,CREAT | ||
| 478 | MOV DX,OFFSET file2 | ||
| 479 | xor cx,cx | ||
| 480 | INT 21H | ||
| 481 | Jc MKERR | ||
| 482 | mov [handle2],ax | ||
| 483 | MOV CX,[SIZ] | ||
| 484 | MOV AH,write | ||
| 485 | push di | ||
| 486 | mov di,[handle2] | ||
| 487 | PUSH DS | ||
| 488 | MOV DS,BX | ||
| 489 | XOR DX,DX ;Address 0 in segment | ||
| 490 | push bx | ||
| 491 | mov bx,di | ||
| 492 | INT 21H | ||
| 493 | pop bx | ||
| 494 | POP DS | ||
| 495 | pop di | ||
| 496 | Jc WRTERR ;Must be zero if more to come | ||
| 497 | MOV AH,CLOSE | ||
| 498 | push bx | ||
| 499 | mov bx,[handle2] | ||
| 500 | INT 21H | ||
| 501 | pop bx | ||
| 502 | RET | ||
| 503 | |||
| 504 | WRTERR: | ||
| 505 | MOV DX,OFFSET FULL | ||
| 506 | JMP xERROR | ||
| 507 | MKERR: | ||
| 508 | MOV DX,OFFSET DIRFULL | ||
| 509 | JMP xERROR | ||
| 510 | |||
| 511 | LOCATE ENDP | ||
| 512 | CODE ENDS | ||
| 513 | END LOCATE | ||
| 514 | |||