     1                                  
     2                                  %if 0
     3                                  
     4                                  lDOS iniload packed payload
     5                                   by C. Masloch, 2018-2020
     6                                  
     7                                  Usage of the works is permitted provided that this
     8                                  instrument is retained with the works, so that any entity
     9                                  that uses the works is notified of this instrument.
    10                                  
    11                                  DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.
    12                                  
    13                                  
    14                                  Includes one of several depackers, some of which are under
    15                                  separate usage conditions. Refer to the individual depacker
    16                                  source files for the applicable usage conditions.
    17                                  
    18                                  %endif
    19                                  
    20                                  %include "lmacros3.mac"
     1                              <1> [list -]
    14                              <2> [list -]
    14                              <1> [list -]
    21                                  
    22                                  	struc LOADSTACKVARS, -10h
    23 FFFFFFF0 ????????                lsvFirstCluster:	resd 1
    24 FFFFFFF4 ????????                lsvFATSector:		resd 1
    25 FFFFFFF8 ????                    lsvFATSeg:		resw 1
    26 FFFFFFFA ????                    lsvLoadSeg:		resw 1
    27 FFFFFFFC ????????                lsvDataStart:		resd 1
    28                                  	endstruc
    29                                  
    30                                  	struc LOADDATA, LOADSTACKVARS - 10h
    31 FFFFFFE0 ????                    ldMemoryTop:	resw 1
    32 FFFFFFE2 ????                    ldLoadTop:	resw 1
    33 FFFFFFE4 ????                    ldSectorSeg:	resw 1
    34 FFFFFFE6 ??                      ldFATType:	resb 1
    35 FFFFFFE7 ??                      ldHasLBA:	resb 1
    36 FFFFFFE8 ????                    ldClusterSize:	resw 1
    37 FFFFFFEA ????                    ldParaPerSector:resw 1
    38 FFFFFFEC ????                    ldLoadingSeg:	resw 1
    39 FFFFFFEE ????                    ldLoadUntilSeg:	resw 1
    40                                  	endstruc
    41                                  
    42                                  
    43                                  %ifndef _MAP
    44                                  %elifempty _MAP
    45                                  %else	; defined non-empty, str or non-str
    46                                  	[map all _MAP]
    47                                  %endif
    48                                  
    49                                  	defaulting
    50                                  
    51                                  	numdef DEBUG0		; use errordata to generate an error code
    52                                  	numdef DEBUG1		; dump_stack_frame after an error occurred
    53                                  	numdef DEBUG2		; dump_stack_frame before blz_depack_safe call
    54                                  	numdef DEBUG3		; dump_stack_frame at start of blz_depack_safe
    55                                  
    56                                  	numdef ALLOW_OVERLAPPING,	1	; allow overlapping src and dst
    57                                  	numdef TEST_PROGRAM,		0
    58                                  	numdef TEST_PROGRAM_DECOMPRESSED_SIZE,		0,0
    59                                  %if _TEST_PROGRAM && ! _TEST_PROGRAM_DECOMPRESSED_SIZE
    60                                   %error Test program has to learn of decompressed size.
    61                                  %endif
    62                                  	numdef TEST_PROGRESS,	0
    63                                  
    64                                  	numdef EXEC_OFFSET,	0
    65                                  	numdef EXEC_SEGMENT,	0
    66                                  	numdef IMAGE_EXE,	0
    67                                  %if ! _IMAGE_EXE && _TEST_PROGRAM
    68                                   %error Test program can only be used as EXE.
    69                                  %endif
    70                                  	numdef IMAGE_EXE_CS,	-16	; relative-segment for CS
    71                                  	numdef IMAGE_EXE_IP,	256 +64	; value for IP
    72                                  		; The next two are only used if _IMAGE_EXE_AUTO_STACK is 0.
    73                                  	numdef IMAGE_EXE_SS,	-16	; relative-segment for SS
    74                                  	numdef IMAGE_EXE_SP,	0FFFEh	; value for SP (0 underflows)
    75                                  	numdef IMAGE_EXE_AUTO_STACK,	0, 2048	; use stack behind image
    76                                  		; _IMAGE_EXE_AUTO_STACK here differs from iniload's def of
    77                                  		;  the same name. This one is only used as a flag; if non-zero,
    78                                  		;  keep the stack given to us by iniload; if zero, set up the
    79                                  		;  stack specified by _IMAGE_EXE_SS and _IMAGE_EXE_SP.
    80                                  	numdef IMAGE_EXE_GET_COM_STACK, 0
    81                                  					; set up stack for flat .COM style entry
    82                                  	numdef DEVICE,			0
    83                                  	gendef DEVICE_NAME,		""
    84                                  	numdef DEVICE_ATTRIBUTE,	8000h
    85                                  	numdef DEVICE_ZERO_ENTRYPOINT,	0
    86                                  	numdef BOOTLDR,		1	; support boot loaded entrypoint
    87                                  
    88                                  	numdef BRIEFLZ,		0
    89                                  	numdef LZ4,		0
    90                                  	numdef SNAPPY,		0
    91                                  	numdef EXODECR,		0
    92                                  	numdef X,		0
    93                                  	numdef HEATSHRINK,	0
    94                                  	numdef LZD,		0
    95                                  	numdef LZO,		0
    96                                  	numdef LZSA2,		0
    97                                  	numdef APL,		0
    98                                  	numdef BZP,		0
    99                                  %if (!!_BRIEFLZ + !!_LZ4 + !!_SNAPPY + !!_EXODECR + !!_X + !!_HEATSHRINK 	+ !!_LZD + !!_LZO + !!_LZSA2 + !!_APL + !!_BZP) != 1
   101                                   %fatal Exactly one compression method must be selected.
   102                                  %endif
   103                                  %assign ADDITIONAL_MEMORY 0
   104                                  %if _BRIEFLZ
   105                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.BLZ"
   106                                  %elif _LZ4
   107                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.LZ4"
   108                                  %elif _SNAPPY
   109                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.SZ"
   110                                  %elif _EXODECR
   111                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.EXO"
   112                                  %elif _X
   113                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.X"
   114                                  %elif _HEATSHRINK
   115                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.HS"
   116                                  %elif _LZD
   117                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.LZ"
   118                                  %elif _LZO
   119                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.LZO"
   120                                  %elif _LZSA2
   121                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.SA2"
   122                                  %elif _APL
   123                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.APL"
   124                                  %elif _BZP
   125                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.BZP"
   126                                  %endif
   127                                  	numdef PAYLOAD_KERNEL_MAX_PARAS,	0, 0
   128                                  %if _PAYLOAD_KERNEL_MAX_PARAS && ! _IMAGE_EXE
   129                                   %error Kernel mode max paras requires building dual-mode executable
   130                                  %endif
   131                                  	numdef COUNTER,		0, 32
   132                                  %if (_COUNTER - 1) & _COUNTER
   133                                   %error COUNTER must be a power of two
   134                                  %endif
   135                                  
   136                                  	strdef INILOAD_CFG, ""
   137                                  %ifnidn _INILOAD_CFG, ""
   138                                   %include _INILOAD_CFG
     1                              <1> ; File auto-generated, do not edit.
     2                              <1> strdef INILOAD_SIGNATURE, "eb"
     3                              <1> %assign _DEVICE 1
     4                              <1> %assign _DEVICE_ZERO_ENTRYPOINT 1
     5                              <1> %assign _DEVICE_ATTRIBUTE 8800h
     6                              <1> %define _DEVICE_NAME "LDEBUG$$"
   139                                  %endif
   140                                  
   141                                  %if _DEVICE
   142                                   %ifidn _DEVICE_NAME, ""
   143                                    %error Device name must be set
   144                                   %endif
   145                                  %endif
   146                                  
   147                                  
   148                                  	cpu 8086
   149                                  	section INIT0 start=0 vstart=0
   150                                  init0_start:
   151                                  %if _DEVICE
   152                                  		; The device header is of a fixed format.
   153                                  		;  For our purposes, the 4-byte code for
   154                                  		;  each the strategy entry and the
   155                                  		;  interrupt entry is part of this format.
   156                                  		; (DOS may read the attributes or entrypoint
   157                                  		;  offsets before calling either, so in the inicomp
   158                                  		;  stage we need to recreate in the entrypoints part
   159                                  		;  exactly what the application has here.)
   160                                  device_header:
   161                                  .next:
   162                                   %if _DEVICE_ZERO_ENTRYPOINT
   163 00000000 EB3E                    	fill 2, -1, jmp strict short j_zero_entrypoint
   164 00000002 FFFF                    	dw -1
   165                                   %else
   166                                  	dd -1				; link to next device
   167                                   %endif
   168                                  .attributes:
   169 00000004 0088                    	dw _DEVICE_ATTRIBUTE		; attributes
   170                                  .strategy:
   171 00000006 [1200]                  	dw .strategy_entry		; -> strategy entry
   172                                  .interrupt:
   173 00000008 [1600]                  	dw .interrupt_entry		; -> interrupt entry
   174                                  .name:
   175 0000000A 4C44454255472424        	fill 8, 32, db _DEVICE_NAME	; character device name,
   176                                  					;  or block device number of units
   177                                  					;  + optional name
   178                                  .strategy_entry:
   179 00000012 EB5190<rep 2h>          	fill 4, 90h, jmp device_entrypoint
   180                                  .interrupt_entry:
   181 00000016 CB90<rep 3h>            	fill 4, 90h, retf
   182                                  %endif
   183                                  
   184                                  
   185                                  %if _IMAGE_EXE || _DEVICE
   186 0000001A 90                      	nop
   187 0000001B 90<rep 5h>              	align 32, nop
   188                                  init0_kernel_entry:
   189                                  		; cs:ip = load seg : 32 here
   190                                  %if ($ - $$) != 32
   191                                   %error Wrong kernel mode entrypoint
   192                                  %endif
   193                                  %if _TEST_PROGRAM
   194                                  @@:
   195                                  	int3
   196                                  	sti
   197                                  	hlt
   198                                  	jmp @B
   199                                  %endif
   200                                  %if _BOOTLDR
   201                                  %if ! _IMAGE_EXE
   202                                  j_zero_entrypoint:
   203                                  %endif
   204 00000020 31DB                    	xor bx, bx		; 0 = kernel mode
   205 00000022 50                      	push ax
   206 00000023 8CC8                    	mov ax, cs
   207 00000025 83C01D                  	add ax, (init0_end - init0_start) >> 4
   208 00000028 8B56E2                  	mov dx, word [ bp + ldLoadTop ]	; => after end of available space
   209 0000002B BE6000                  	mov si, 60h		; => destination
   210 0000002E EB20                    	jmp init0_common
   211                                  %endif
   212                                  
   213                                   %if _IMAGE_EXE
   214 00000030 90<rep 10h>             	align 64, nop
   215                                  j_zero_entrypoint:
   216                                  init0_exe_entry:
   217                                  		; NOTE:	This part is called with ip = 256 + 64, cs = PSP.
   218                                  %if ($ - $$) != 64
   219                                   %error Wrong EXE mode entrypoint
   220                                  %endif
   221                                  %if _IMAGE_EXE_GET_COM_STACK
   222                                  	mov bx, ds		; => PSP
   223                                  	dec bx			; => MCB
   224                                  	mov ds, bx
   225                                  	add bx, word [3]	; add size of memory block
   226                                  	sub bx, -1 + paras(512)	; add one, then subtract enough paragraphs
   227                                  	cli
   228                                  	mov ss, bx
   229                                  	mov sp, 512		; -> new stack
   230                                  	sti
   231                                  %endif
   232 00000040 BB0100                  	mov bx, 1		; EXE mode
   233 00000043 50                      	push ax
   234                                    %if _TEST_PROGRAM
   235                                  	mov si, 81h
   236                                  	xor dx, dx
   237                                  	xor ax, ax
   238                                  cmdline:
   239                                  .:
   240                                  	lodsb
   241                                  	cmp al, 9
   242                                  	je .
   243                                  	cmp al, 32
   244                                  	je .
   245                                  	cmp al, 13
   246                                  	je .end
   247                                  	cmp al, '0'
   248                                  	jb .notdigit
   249                                  	cmp al, '9'
   250                                  	ja .notdigit
   251                                  .digit:
   252                                  	sub al, '0'
   253                                  	mov cx, dx		; times 1
   254                                  	add dx, dx
   255                                  	jc .invalid
   256                                  	add dx, dx		; times 4
   257                                  	jc .invalid
   258                                  	add dx, cx		; times 5
   259                                  	jc .invalid
   260                                  	add dx, dx		; times 10
   261                                  	jc .invalid
   262                                  	add dx, ax		; add in low digit
   263                                  	jc .invalid
   264                                  	jmp .
   265                                  
   266                                  .notdigit:
   267                                  	cmp al, 'A'
   268                                  	je .a
   269                                  	cmp al, 'a'
   270                                  	je .a
   271                                  	cmp al, 'B'
   272                                  	je .b
   273                                  	cmp al, 'b'
   274                                  	je .b
   275                                  .invalid:
   276                                  	mov dx, -1		; invalid
   277                                  	jmp .end
   278                                  
   279                                  .a:
   280                                  	and bh, ~ 80h
   281                                  	jmp .
   282                                  
   283                                  .b:
   284                                  	or bh, 80h
   285                                  	jmp .
   286                                  
   287                                  .end:
   288                                  	push dx
   289                                    %endif
   290 00000044 8CD2                    	mov dx, ss		; => after end of available space
   291 00000046 8CC8                    	mov ax, cs
   292 00000048 83C02D                  	add ax, (256 + (init0_end - init0_start)) >> 4	; => source
   293 0000004B 8CCE                    	mov si, cs
   294 0000004D 83C610                  	add si, 256 >> 4	; => destination
   295                                   %endif
   296                                  
   297                                  init0_common:
   298                                  		; REM:	This part must be position-independent, as it is
   299                                  		;	 called either with ip = init0_common (kernel mode
   300                                  		;	 or device mode)
   301                                  		;	 or ip = init0_common + 256 (application mode).
   302                                  		; INP:	si => depack destination
   303                                  		;	ax => source of payload + INIT1
   304                                  		;	dx => behind available memory
   305                                  		; OUT:	cs:ip -> init1_start:
   306                                  		;	ax => INIT1
   307                                  		;	dx => payload source
   308                                  		;	si => depack destination
   309                                  		;	bx = mode
   310 00000050 FC                      	cld
   311                                  %if _TEST_PROGRAM
   312                                  	sub dx, ( (payload_end - payload) ) >> 4
   313                                  	jc .error
   314                                  	mov cx, ( (payload_end - payload) 		+ (init1_end - init1_start) 		+ (init2_end - init2_start) 		) >> 4
   318                                  	sub dx, cx
   319                                  	jc .error
   320                                  	cmp dx, ax
   321                                  	jb .error
   322                                  %else
   323 00000051 B9AB17                  	mov cx, ( (payload_end - payload) 		+ (init1_end - init1_start) 		) >> 4
   326                                  				; cx = amount paragraphs in payload + INIT1
   327 00000054 29CA                    	sub dx, cx		; => destination for payload + INIT1
   328                                  				;  (highest possible spot in available memory)
   329                                  %endif
   330 00000056 56                      	push si			; preserve destination
   331 00000057 E8F300                  	call init0_movp		; move up payload + INIT1
   332 0000005A 5E                      	pop si
   333                                  %else
   334                                  	cld
   335                                  	mov ax, cs
   336                                  	add ax, (init0_end - init0_start) >> 4
   337                                  	mov cx, ( (payload_end - payload) 		+ (init1_end - init1_start) 		) >> 4
   340                                  	mov dx, word [ bp + ldLoadTop ]
   341                                  	sub dx, cx
   342                                  	call init0_movp
   343                                  %endif
   344                                  
   345 0000005B 89D0                    	mov ax, dx		; => payload
   346                                  %if _TEST_PROGRAM
   347                                  	add ax, ( 		+ (payload_end - payload) 		+ (init1_end - init1_start) 		) >> 4		; => INIT2
   351                                  %else
   352 0000005D 056817                  	add ax, ( 		+ (payload_end - payload) 		) >> 4		; => INIT1
   355                                  %endif
   356 00000060 31C9                    	xor cx, cx
   357 00000062 50                      	push ax
   358 00000063 51                      	push cx
   359 00000064 CB                      	retf			; jump to relocated INIT1:init1_start
   360                                  				;  or INIT2:init2_start
   361                                  
   362                                  
   363                                  %if _TEST_PROGRAM
   364                                  .error:
   365                                  	mov di, bx
   366                                  
   367                                  	push cs
   368                                  	pop ds
   369                                  	mov dx, init0_msg.error_stderr + 256
   370                                  	mov cx, init0_msg.error_stderr.length
   371                                  	mov bx, 2
   372                                  	mov ah, 40h
   373                                  	int 21h
   374                                  
   375                                  	test di, 8000h
   376                                  	jnz @F
   377                                  	mov dx, init0_msg.error_stdout + 256
   378                                  	mov cx, init0_msg.error_stdout.length
   379                                  	mov bx, 1
   380                                  	mov ah, 40h
   381                                  	int 21h
   382                                  @@:
   383                                  	mov ax, 4CFFh
   384                                  	int 21h
   385                                  %endif
   386                                  
   387                                  
   388                                  %if _DEVICE
   389                                  device_entrypoint:
   390                                  %if _TEST_PROGRAM
   391                                  @@:
   392                                  	int3
   393                                  	sti
   394                                  	hlt
   395                                  	jmp @B
   396                                  %endif
   397 00000065 26807F0200              	cmp byte [es:bx + 2], 0		; command code 0 (init) ?
   398 0000006A 7407                    	je @F
   399                                  
   400 0000006C 26C747030381            	mov word [es:bx + 3], 8103h	; error, done, code: unknown command
   401 00000072 CB                      	retf
   402                                  
   403                                  @@:
   404 00000073 2E830E[0000]FF          	or word [cs:device_header.next], -1
   405 00000079 0E                      	push cs
   406 0000007A 2EFF36[0600]            	push word [cs:device_header.strategy]
   407                                  					; -> far return to payload's strategy
   408 0000007F 55                      	push bp
   409 00000080 1E                      	push ds
   410 00000081 56                      	push si
   411 00000082 57                      	push di
   412 00000083 52                      	push dx
   413 00000084 51                      	push cx
   414 00000085 50                      	push ax
   415 00000086 53                      	push bx
   416 00000087 06                      	push es
   417                                  
   418 00000088 268B4710                	mov ax, word [es:bx + 14 + 2]
   419 0000008C 31D2                    	xor dx, dx
   420 0000008E B90400                  	mov cx, 4
   421                                  @@:
   422 00000091 D1E0                    	shl ax, 1
   423 00000093 D1D2                    	rcl dx, 1
   424 00000095 E2FA                    	loop @B
   425 00000097 2603470E                	add ax, word [es:bx + 14]
   426 0000009B 83D200                  	adc dx, 0		; dx:ax = linear address behind end
   427                                  
   428 0000009E B104                    	mov cl, 4
   429                                  @@:
   430 000000A0 D1EA                    	shr dx, 1
   431 000000A2 D1D8                    	rcr ax, 1
   432 000000A4 E2FA                    	loop @B			; ax => behind end
   433                                  
   434 000000A6 8CCF                    	mov di, cs
   435                                  image_size: equ  ( (init0_end - init0_start) 		+ (payload_end - payload) 		+ (init1_end - init1_start) 		)
   439 000000A8 81C7C817                	add di, image_size >> 4		; di => end of image
   440                                  
   441 000000AC 39F8                    	cmp ax, di			; enough memory to hold all of us ?
   442 000000AE 7203E98A00              	jae .have_some_memory		; yes -->
   443                                  
   444 000000B3 0E                      	push cs
   445 000000B4 1F                      	pop ds
   446 000000B5 BA[F400]                	mov dx, .msg_no_memory
   447 000000B8 B409                    	mov ah, 09h
   448 000000BA CD21                    	int 21h
   449                                  
   450 000000BC B80030                  	mov ax, 3000h
   451 000000BF CD21                    	int 21h
   452 000000C1 3C05                    	cmp al, 5
   453 000000C3 7307                    	jae @F
   454 000000C5 BA[1301]                	mov dx, .msg_dos_below_5
   455 000000C8 B409                    	mov ah, 09h
   456 000000CA CD21                    	int 21h
   457                                  @@:
   458                                  
   459 000000CC BA[3A01]                	mov dx, .msg_linebreak
   460 000000CF B409                    	mov ah, 09h
   461 000000D1 CD21                    	int 21h
   462                                  
   463                                  		; tear down the stack frame and modify the request header
   464 000000D3 07                      	pop es
   465 000000D4 5B                      	pop bx
   466 000000D5 26C747030381            	mov word [es:bx + 3], 8103h	; set error, done, invalid command
   467 000000DB 26C6470D00              	mov byte [es:bx + 13], 0	; set number of units = 0
   468 000000E0 268C4F10                	mov word [es:bx + 14 + 2], cs
   469 000000E4 2683670E00              	and word [es:bx + 14], 0	; -> after end of memory to allocate
   470 000000E9 58                      	pop ax
   471 000000EA 59                      	pop cx
   472 000000EB 5A                      	pop dx
   473 000000EC 5F                      	pop di
   474 000000ED 5E                      	pop si
   475 000000EE 1F                      	pop ds
   476 000000EF 5D                      	pop bp
   477 000000F0 83C404                  	add sp, 4		; discard far return to payload's strategy
   478 000000F3 CB                      	retf			; return to DOS
   479                                  
   480 000000F4 4C6F6164206572726F-     .msg_no_memory:		ascic "Load error: Not enough memory."
   480 000000FD 723A204E6F7420656E-
   480 00000106 6F756768206D656D6F-
   480 0000010F 72792E24           
   481 00000113 204E6F74653A20444F-     .msg_dos_below_5:	ascic " Note: DOS must be at least version 5."
   481 0000011C 53206D757374206265-
   481 00000125 206174206C65617374-
   481 0000012E 2076657273696F6E20-
   481 00000137 352E24             
   482 0000013A 0D0A24                  .msg_linebreak:		ascic 13,10
   483                                  
   484                                  
   485                                  .have_some_memory:
   486 0000013D 89C2                    	mov dx, ax		; => after end of memory
   487 0000013F BB0200                  	mov bx, 2		; device mode
   488 00000142 50                      	push ax
   489 00000143 8CC8                    	mov ax, cs
   490 00000145 83C01D                  	add ax, (init0_end - init0_start) >> 4
   491 00000148 8CCE                    	mov si, cs		; => destination
   492 0000014A E903FF                  	jmp init0_common
   493                                  %endif
   494                                  
   495                                  
   496                                  		; Move paragraphs
   497                                  		;
   498                                  		; INP:	ax => source
   499                                  		;	dx => destination
   500                                  		;	cx = number of paragraphs
   501                                  		; CHG:	cx, ds, si, es, di
   502                                  		; OUT:	ax and dx unchanged
   503                                  		; Note:	Doesn't work correctly on HMA; doesn't always wrap to LMA either.
   504                                  		;	Do not provide a wrapped/HMA source or destination!
   505                                  init0_movp:
   506 0000014D 39D0                    	cmp ax, dx		; source above destination ?
   507 0000014F 770A                    	ja .up			; yes, move up (forwards) -->
   508 00000151 747B                    	je .return		; same, no need to move -->
   509 00000153 50                      	push ax
   510 00000154 01C8                    	add ax, cx		; (expected not to carry)
   511 00000156 39D0                    	cmp ax, dx		; end of source is above destination ?
   512 00000158 58                      	pop ax
   513 00000159 7730                    	ja .down		; yes, move from top down -->
   514                                  	; Here, the end of source is below-or-equal the destination,
   515                                  	;  so they do not overlap. In this case we prefer moving up.
   516                                  
   517                                  .up:
   518 0000015B 50                      	push ax
   519 0000015C 52                      	push dx
   520                                  .uploop:
   521 0000015D 8ED8                    	mov ds, ax
   522 0000015F 8EC2                    	mov es, dx
   523 00000161 31FF                    	xor di, di
   524 00000163 31F6                    	xor si, si		; -> start of segment
   525 00000165 81E90010                	sub cx, 1000h		; 64 KiB left ?
   526 00000169 7610                    	jbe .uplast		; no -->
   527 0000016B 51                      	push cx
   528 0000016C B90080                  	mov cx, 10000h /2
   529 0000016F F3A5                    	rep movsw		; move 64 KiB
   530 00000171 59                      	pop cx
   531 00000172 050010                  	add ax, 1000h
   532 00000175 81C20010                	add dx, 1000h		; -> next segment
   533 00000179 EBE2                    	jmp short .uploop	; proceed for more -->
   534                                  .uplast:
   535 0000017B 81C10010                	add cx, 1000h		; restore counter
   536 0000017F D1E1                    	shl cx, 1
   537 00000181 D1E1                    	shl cx, 1
   538 00000183 D1E1                    	shl cx, 1		; *8, paragraphs to words
   539 00000185 F3A5                    	rep movsw		; move last part
   540 00000187 5A                      	pop dx
   541 00000188 58                      	pop ax
   542 00000189 EB43                    	jmp short .return
   543                                  
   544                                  .down:
   545 0000018B FD                      	std			; _AMD_ERRATUM_109_WORKAROUND as below
   546                                  .dnloop:
   547 0000018C 81E90010                	sub cx, 1000h		; 64 KiB left ?
   548 00000190 761A                    	jbe .dnlast		; no -->
   549 00000192 50                      	push ax
   550 00000193 52                      	push dx
   551 00000194 01C8                    	add ax, cx
   552 00000196 01CA                    	add dx, cx
   553 00000198 8ED8                    	mov ds, ax		; -> 64 KiB not yet moved
   554 0000019A 8EC2                    	mov es, dx
   555 0000019C 5A                      	pop dx
   556 0000019D 58                      	pop ax
   557 0000019E BFFEFF                  	mov di, -2
   558 000001A1 89FE                    	mov si, di		; moved from last word down
   559 000001A3 51                      	push cx
   560 000001A4 B90080                  	mov cx, 10000h /2
   561 000001A7 F3A5                    	rep movsw		; move 64 KiB
   562 000001A9 59                      	pop cx
   563 000001AA EBE0                    	jmp short .dnloop	; proceed for more -->
   564                                  .dnlast:
   565 000001AC 81C10010                	add cx, 1000h		; restore counter
   566 000001B0 D1E1                    	shl cx, 1
   567 000001B2 D1E1                    	shl cx, 1
   568 000001B4 D1E1                    	shl cx, 1		; *8, paragraphs to words
   569 000001B6 89CF                    	mov di, cx
   570 000001B8 4F                      	dec di
   571 000001B9 D1E7                    	shl di, 1		; words to offset, -> last word
   572 000001BB 89FE                    	mov si, di
   573 000001BD 8ED8                    	mov ds, ax
   574 000001BF 8EC2                    	mov es, dx		; first segment correct
   575                                  
   576                                  
   577                                  	numdef AMD_ERRATUM_109_WORKAROUND, 1
   578                                  %if 0
   579                                  
   580                                  Jack R. Ellis pointed out this erratum:
   581                                  
   582                                  Quoting from https://www.amd.com/system/files/TechDocs/25759.pdf page 69:
   583                                  
   584                                  109   Certain Reverse REP MOVS May Produce Unpredictable Behavior
   585                                  
   586                                  Description
   587                                  
   588                                  In certain situations a REP MOVS instruction may lead to
   589                                  incorrect results. An incorrect address size, data size
   590                                  or source operand segment may be used or a succeeding
   591                                  instruction may be skipped. This may occur under the
   592                                  following conditions:
   593                                  
   594                                  * EFLAGS.DF=1 (the string is being moved in the reverse direction).
   595                                  
   596                                  * The number of items being moved (RCX) is between 1 and 20.
   597                                  
   598                                  * The REP MOVS instruction is preceded by some microcoded instruction
   599                                    that has not completely retired by the time the REP MOVS begins
   600                                    execution. The set of such instructions includes BOUND, CLI, LDS,
   601                                    LES, LFS, LGS, LSS, IDIV, and most microcoded x87 instructions.
   602                                  
   603                                  Potential Effect on System
   604                                  
   605                                  Incorrect results may be produced or the system may hang.
   606                                  
   607                                  Suggested Workaround
   608                                  
   609                                  Contact your AMD representative for information on a BIOS update.
   610                                  
   611                                  %endif
   612                                  
   613                                  %if _AMD_ERRATUM_109_WORKAROUND
   614 000001C1 E308                    	jcxz @FF
   615 000001C3 83F914                  	cmp cx, 20
   616 000001C6 7703                    	ja @FF
   617                                  @@:
   618 000001C8 A5                      	movsw
   619 000001C9 E2FD                    	loop @B
   620                                  @@:
   621                                  %endif
   622 000001CB F3A5                    	rep movsw		; move first part
   623 000001CD FC                      	cld
   624                                  .return:
   625 000001CE C3                      	retn
   626                                  
   627                                  
   628                                  	section PAYLOAD align=16 follows=INIT0
   629                                  payload:
   630 00000000 <bin 17672h>            	incbin _PAYLOAD_FILE
   631                                  .end:
   632 00017672 26<rep Eh>              	align 16, db 38
   633                                  payload_end:
   634                                  
   635                                  
   636                                  	section INIT1 align=16 follows=PAYLOAD vstart=0
   637                                  init1_start:
   638                                  		; INP:	ax = cs = INIT1
   639                                  		;	dx = cs - (payload_end - payload) >> 4 => source data
   640                                  		;	if kernel mode:
   641                                  		;	 ss:bp -> LOADDATA and LOADSTACKVARS
   642                                  		;	 ss:sp -> valid stack above [bp + ldLoadTop]
   643                                  		;	 60h => destination
   644                                  		;	any mode:
   645                                  		;	 word [ss:sp] = value for ax
   646                                  		;	 bx = 2 if device mode, 1 if EXE mode, 0 if kernel mode
   647                                  		;	 si => destination (60h for kernel mode,
   648                                  		;		after PSP for EXE mode,
   649                                  		;		at device header for device mode)
   650                                  		;		(not set if only kernel mode supported)
   651                                  		;	 if EXE mode:
   652                                  		;	  ss:sp -> valid stack above INIT1
   653                                  		;	  bp = unset
   654                                  		;	 if device mode:
   655                                  		;	  ss:sp -> device entrypoint stack
   656                                  		;	  holds: es, bx, ax, cx, dx, di, si, ds, bp,
   657                                  		;		  far address of payload strategy entrypoint,
   658                                  		;		  far return address to DOS
   659                                  		;	  bp = unset
   660                                  		; STT:	UP
   661                                  		; CHG:	ax, bx, cx, dx, es, ds, si, di
   662                                  %if _IMAGE_EXE || _DEVICE
   663                                  	lframe
   664 00000000 5589E5                  	lenter
   665                                  	lvar word,	exemode		; must be bp - 2!
   666 00000003 53                      	 push bx
   667                                   %if ?exemode != -2
   668                                    %error exemode variable must be directly below bp
   669                                   %endif
   670 00000004 56                      	push si
   671                                  %else
   672                                  	xor bx, bx		; always tell them it is kernel mode
   673                                  %endif
   674                                  
   675                                  %if _IMAGE_EXE
   676 00000005 8EC6                    	mov es, si		; es => destination
   677                                  %else
   678                                  	mov ax, 60h
   679                                  	mov es, ax		; es => destination
   680                                  %endif
   681 00000007 31FF                    	xor di, di		; -> destination
   682                                  
   683 00000009 8EDA                    	mov ds, dx
   684 0000000B 31F6                    	xor si, si		; -> source
   685                                  
   686 0000000D B97276                  	mov cx, (payload.end - payload) & 0FFFFh
   687                                  %if (payload.end - payload) >> 16
   688 00000010 BA0100                  	mov dx, (payload.end - payload) >> 16	; = length of source
   689                                  %else
   690                                  	xor dx, dx
   691                                  %endif
   692                                  %if _PAYLOAD_KERNEL_MAX_PARAS
   693                                  	mov ax, _PAYLOAD_KERNEL_MAX_PARAS
   694                                  	test bx, bx
   695                                  	jz @F
   696                                  %endif
   697 00000013 B8FFFF                  	mov ax, -1
   698                                  @@:
   699                                  		; INP:	ds:si -> source
   700                                  		;	dx:cx = length of source
   701                                  		;	es:di -> destination (below source)
   702                                  		;	bx = EXE mode flag
   703                                  		;	 (1 if EXE mode, 0 if kernel mode)
   704                                  		;	 (always 0 if this is a build without EXE mode)
   705                                  		;	if _PAYLOAD_KERNEL_MAX_PARAS:
   706                                  		;	 ax = maximum amount in paragraphs of destination needed
   707                                  		;	 (-1 if full source should be decompressed)
   708                                  		;	else:
   709                                  		;	 ax = -1
   710                                  		; Note:	The destination reaches up to below the source.
   711 00000016 E80201                  	call depack
   712                                  %ifn _TEST_PROGRAM
   713 00000019 7234                    	jc strict short error
   714                                  %endif
   715                                  %if _IMAGE_EXE
   716 0000001B 5E                      	pop si			; si
   717                                   %if _TEST_PROGRAM
   718                                  	pop ax			; (discard ?exemode, leave bx as returned)
   719                                   %else
   720 0000001C 5B                      	pop bx			; ?exemode
   721                                   %endif
   722 0000001D 5D                      	pop bp			; bp
   723 0000001E 58                      	pop ax			; ax
   724                                  	lleave ctx
   725                                  %endif
   726                                  %if _TEST_PROGRAM
   727                                  	retf
   728                                  	nop
   729                                  %endif
   730                                  
   731                                  %if _DEVICE
   732 0000001F F6C302                  	test bl, 2
   733 00000022 7411                    	jz .jmp_exe_or_kernel_mode
   734                                  
   735                                  .jmp_device_mode:
   736 00000024 8EDE                    	mov ds, si
   737 00000026 830E[0000]FF            	or word [device_header.next], -1
   738 0000002B 07                      	pop es
   739 0000002C 5B                      	pop bx
   740 0000002D 58                      	pop ax
   741 0000002E 59                      	pop cx
   742 0000002F 5A                      	pop dx
   743 00000030 5F                      	pop di
   744 00000031 5E                      	pop si
   745 00000032 1F                      	pop ds
   746 00000033 5D                      	pop bp
   747 00000034 CB                      	retf			; transfer to payload strategy entrypoint
   748                                  				; still on stack: far return address to DOS
   749                                  
   750                                  .jmp_exe_or_kernel_mode:
   751                                  %endif
   752                                  
   753                                  %if _IMAGE_EXE
   754 00000035 F6C301                  	test bl, 1
   755 00000038 7410                    	jz .jmp_kernel_mode
   756                                  
   757                                  .jmp_exe_mode:
   758                                  %if ! _IMAGE_EXE_AUTO_STACK
   759                                  	mov cx, cs
   760                                  	lea dx, [si + _IMAGE_EXE_SS]
   761                                  	push dx			; stack = relocated ss value
   762                                  	add dx, (_IMAGE_EXE_SP + 2 + 15) >> 4
   763                                  	cmp cx, dx		; INIT1 code is above intended stack ?
   764                                  	jae @F			; yes -->
   765                                  
   766                                  	lframe
   767                                  	lenter
   768                                  	lvar	word, exemode
   769                                  	 push bx
   770                                   %if ?exemode != -2
   771                                    %error exemode variable must be directly below bp
   772                                   %endif
   773                                  	mov bx, -1		; unimplemented, return error
   774                                  	jmp error
   775                                  	lleave ctx
   776                                  @@:
   777                                  	cli
   778                                  	pop ss			; = relocated ss value
   779                                  	mov sp, (_IMAGE_EXE_SP + 2) & 0FFFFh	; change stack
   780                                  	sti
   781                                  %endif
   782                                  
   783 0000003A 31C9                    	xor cx, cx
   784 0000003C 51                      	push cx			; put zero on top of stack
   785                                  
   786                                  %if _IMAGE_EXE_IP < 256
   787                                  	mov cl, _IMAGE_EXE_IP
   788                                  %elif (_IMAGE_EXE_IP & 255) == 0
   789                                  	mov ch, _IMAGE_EXE_IP >> 8
   790                                  %else
   791 0000003D B94001                  	mov cx, _IMAGE_EXE_IP
   792                                  %endif
   793                                  
   794                                  %if _IMAGE_EXE_CS == -16
   795 00000040 83C6F0                  	add si, -16
   796 00000043 8EDE                    	mov ds, si
   797 00000045 8EC6                    	mov es, si
   798                                  %else
   799                                  	lea dx, [si - 10h]	; => PSP
   800                                  	mov ds, dx
   801                                  	mov es, dx		; ds = es => PSP
   802                                  	add si, _IMAGE_EXE_CS	; = relocated cs value
   803                                  %endif
   804 00000047 56                      	push si
   805 00000048 51                      	push cx
   806 00000049 CB                      	retf			; jump to EXE mode of image
   807                                  
   808                                  %endif
   809                                  .jmp_kernel_mode:
   810 0000004A EA20006000              	jmp 60h + _EXEC_SEGMENT:_EXEC_OFFSET
   811                                  
   812                                  
   813                                  error:
   814 0000004F 0E                      	push cs
   815 00000050 1F                      	pop ds
   816                                  %if _DEBUG0
   817                                  	mov si, msg.error_begin
   818                                  	call disp_error
   819                                  	xchg ax, bx			; ax = error code
   820                                  	call disp_ax_hex
   821                                  	mov si, msg.error_end
   822                                  %else
   823 00000051 BE[F600]                	mov si, msg.error
   824                                  %endif
   825 00000054 E87D00                  	call disp_error
   826                                  
   827                                  %if _DEVICE
   828 00000057 F646FE02                	test byte [bp - 2], 2
   829 0000005B 7435                    	jz .exit_app_or_kernel
   830                                  
   831 0000005D 5E                      	pop si				; => device segment
   832 0000005E 5B                      	pop bx				; mode word
   833 0000005F 5D                      	pop bp
   834 00000060 58                      	pop ax
   835 00000061 8EC6                    	mov es, si
   836 00000063 31FF                    	xor di, di			; -> device header
   837 00000065 0E                      	 push cs
   838 00000066 1F                      	 pop ds
   839 00000067 BE[B000]                	mov si, device_header_copy	; -> to reset header
   840 0000006A B91100                  	mov cx, words(device_header_copy.length)
   841 0000006D F3A5                    	rep movsw		; overwrite device header with default
   842                                  				;  (reset to a valid state after unsuccessful
   843                                  				;  decompression, which may have partially
   844                                  				;  written the header already)
   845 0000006F 06                      	 push es
   846 00000070 1F                      	 pop ds				; -> device segment
   847 00000071 07                      	pop es
   848 00000072 5B                      	pop bx
   849 00000073 26C747030381            	mov word [es:bx + 3], 8103h	; set error, done, invalid command
   850 00000079 26C6470D00              	mov byte [es:bx + 13], 0	; set number of units = 0
   851 0000007E 268C5F10                	mov word [es:bx + 14 + 2], ds
   852 00000082 2683670E00              	and word [es:bx + 14], 0	; -> after end of memory to allocate
   853 00000087 58                      	pop ax
   854 00000088 59                      	pop cx
   855 00000089 5A                      	pop dx
   856 0000008A 5F                      	pop di
   857 0000008B 5E                      	pop si
   858 0000008C 1F                      	pop ds
   859 0000008D 5D                      	pop bp
   860 0000008E 83C404                  	add sp, 4		; discard far return to payload's strategy
   861 00000091 CB                      	retf			; return to DOS
   862                                  
   863                                  
   864                                  .exit_app_or_kernel:
   865                                  %endif
   866                                  
   867                                  %if _IMAGE_EXE
   868                                  %if _BOOTLDR
   869 00000092 F646FE01                	test byte [bp - 2], 1
   870 00000096 7405                    	jz .exit_kernel_mode
   871                                  %endif
   872                                  
   873 00000098 B87F4C                  	mov ax, 4C7Fh
   874 0000009B CD21                    	int 21h
   875                                  
   876                                  .exit_kernel_mode:
   877                                  %endif
   878                                  %if _BOOTLDR
   879 0000009D 31C0                    	xor ax, ax
   880 0000009F CD16                    	int 16h
   881 000000A1 CD19                    	int 19h
   882                                  %endif
   883                                  
   884                                  
   885                                  %if _DEVICE
   886 000000A3 90<rep Dh>              	align 16
   887                                  device_header_copy:
   888                                  .:
   889                                  .next:
   890 000000B0 FFFFFFFF                	dd -1				; already initialised
   891                                  .attributes:
   892 000000B4 0088                    	dw _DEVICE_ATTRIBUTE
   893                                  .strategy:
   894 000000B6 1200                    	dw .strategy_entry - .		; -> strategy entry
   895                                  .interrupt:
   896 000000B8 1600                    	dw .interrupt_entry - .		; -> interrupt entry
   897                                  .name:
   898 000000BA 4C44454255472424        	fill 8, 32, db _DEVICE_NAME
   899                                  .strategy_entry:
   900 000000C2 EB0690<rep 2h>          	fill 4, 90h, jmp .set_error	; rel8 or rel16 jump, not minus .
   901                                  .interrupt_entry:
   902 000000C6 CB90<rep 3h>            	fill 4, 90h, retf
   903                                  
   904                                  .set_error:
   905 000000CA 26C747030381            	mov word [es:bx + 3], 8103h	; set error, done, invalid command
   906 000000D0 CB                      	retf
   907                                  .length: equ $ - .
   908                                  %endif
   909                                  
   910                                  
   911                                  disp_error.loop:
   912 000000D1 E80600                  	call disp_al
   913                                  disp_error:
   914 000000D4 AC                      	lodsb
   915 000000D5 84C0                    	test al, al
   916 000000D7 75F8                    	jnz .loop
   917 000000D9 C3                      	retn
   918                                  
   919                                  %if _DEBUG0 || _DEBUG1 || _DEBUG2 || _DEBUG3
   920                                  disp_ax_hex:			; ax
   921                                  		xchg al,ah
   922                                  		call disp_al_hex		; display former ah
   923                                  		xchg al,ah			;  and fall trough for al
   924                                  disp_al_hex:			; al
   925                                  		push cx
   926                                  		mov cl,4
   927                                  		ror al,cl
   928                                  		call disp_al_lownibble_hex	; display former high-nibble
   929                                  		rol al,cl
   930                                  		pop cx
   931                                  						;  and fall trough for low-nibble
   932                                  disp_al_lownibble_hex:
   933                                  		push ax			 ; save ax for call return
   934                                  		and al,00001111b		; high nibble must be zero
   935                                  		add al,'0'			; if number is 0-9, now it's the correct character
   936                                  		cmp al,'9'
   937                                  		jna .decimalnum		 ; if we get decimal number with this, ok -->
   938                                  		add al,7			;  otherwise, add 7 and we are inside our alphabet
   939                                   .decimalnum:
   940                                  		call disp_al
   941                                  		pop ax
   942                                  		retn
   943                                  %endif
   944                                  
   945                                  disp_al:
   946                                  %if _TEST_PROGRAM
   947                                  	retn
   948                                  %else
   949 000000DA 50                      	push ax
   950                                  %endif
   951                                  %if _BOOTLDR
   952 000000DB 53                      	push bx
   953 000000DC 55                      	push bp
   954 000000DD B40E                    	mov ah, 0Eh
   955 000000DF BB0700                  	mov bx, 7
   956                                  %endif
   957                                  %if _IMAGE_EXE || _DEVICE
   958 000000E2 52                      	push dx
   959                                  
   960                                  %if _BOOTLDR
   961 000000E3 F646FE03                	test byte [bp - 2], 1 | 2
   962 000000E7 7406                    	jz .display_kernel_mode
   963                                  %endif
   964                                  
   965 000000E9 92                      	xchg dx, ax				; dl = input al
   966 000000EA B402                    	mov ah, 02h
   967 000000EC CD21                    	int 21h
   968                                  %if _BOOTLDR
   969 000000EE A9                      	db __TEST_IMM16				; (skip int)
   970                                  %endif
   971                                  
   972                                  .display_kernel_mode:
   973                                  %endif
   974                                  %if _BOOTLDR
   975 000000EF CD10                    	int 10h
   976                                  %endif
   977                                  %if _IMAGE_EXE
   978 000000F1 5A                      	pop dx
   979                                  %endif
   980                                  %if _BOOTLDR
   981 000000F2 5D                      	pop bp
   982 000000F3 5B                      	pop bx
   983                                  %endif
   984 000000F4 58                      	pop ax
   985                                  disp_error.ret:
   986 000000F5 C3                      	retn
   987                                  
   988                                  
   989                                  msg:
   990                                  %if _DEBUG0
   991                                  .error_begin:	db "Load error: Decompression failure, code ",0
   992                                  .error_end:	db "h.",13,10,0
   993                                  %else
   994 000000F6 4C6F6164206572726F-     .error:		db "Load error: Decompression failure.",13,10,0
   994 000000FF 723A204465636F6D70-
   994 00000108 72657373696F6E2066-
   994 00000111 61696C7572652E0D0A-
   994 0000011A 00                 
   995                                  %endif
   996                                  
   997                                  
   998                                  %assign NEED_NORMALISE_POINTER_WITH_DISPLACEMENT 1
   999                                  %assign CHECK_POINTERS_VARIABLE_SRC 1
  1000                                  %assign CHECK_POINTERS_VARIABLE_DST 1
  1001                                  
  1002                                  		; Specific depacker's file is included within label msg.
  1003                                  		; In the file, lframe is used and lleave ctx is not used.
  1004                                  
  1005                                  %if _BRIEFLZ
  1006                                  	%include "brieflz.asm"
  1007                                  %endif
  1008                                  
  1009                                  
  1010                                  %if _LZ4
  1011                                  	%include "lz4.asm"
     1                              <1> 
     2                              <1> %if 0
     3                              <1> 
     4                              <1> 8086 Assembly lDOS iniload payload LZ4 depacker
     5                              <1>  by C. Masloch, 2018
     6                              <1> 
     7                              <1> Usage of the works is permitted provided that this
     8                              <1> instrument is retained with the works, so that any entity
     9                              <1> that uses the works is notified of this instrument.
    10                              <1> 
    11                              <1> DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.
    12                              <1> 
    13                              <1> %endif
    14                              <1> 
    15                              <1> 
    16                              <1> lz4_flg_version_mask:	equ 1100_0000b
    17                              <1> lz4_flg_version:	equ 0100_0000b
    18                              <1> lz4_flg_b_indep:	equ 0010_0000b
    19                              <1> lz4_flg_b_checksum:	equ 0001_0000b
    20                              <1> lz4_flg_c_size:		equ 0000_1000b
    21                              <1> lz4_flg_c_checksum:	equ 0000_0100b
    22                              <1> lz4_flg_reserved:	equ 0000_0010b
    23                              <1> lz4_flg_dict_id:	equ 0000_0001b
    24                              <1> 
    25                              <1> lz4_bd_reserved_mask:	equ 1000_1111b
    26                              <1> 		; BD block max size is not used by us
    27                              <1> 
    28                              <1> 
    29                              <1> 		; INP:	ds:si -> source
    30                              <1> 		;	dx:cx = length of source
    31                              <1> 		;	es:di -> destination (below source)
    32                              <1> 		;	if _IMAGE_EXE:
    33                              <1> 		;	 bx = EXE mode flag (bit 0)
    34                              <1> 		;	else:
    35                              <1> 		;	 bx = 0
    36                              <1> 		;	if _PAYLOAD_KERNEL_MAX_PARAS:
    37                              <1> 		;	 ax = maximum amount in paragraphs of destination needed
    38                              <1> 		;	 (-1 if full source should be decompressed)
    39                              <1> 		;	else:
    40                              <1> 		;	 ax = -1
    41                              <1> 		; OUT:	NC if success
    42                              <1> 		;	CY if error,
    43                              <1> 		;	 bx = ?errordata (if _DEBUG0)
    44                              <1> 		; CHG:	ax, (bx), cx, dx, es, ds, si, di
    45                              <1> 		; STT:	UP
    46                              <1> 		;
    47                              <1> 		; Note:	The destination reaches up to below the source.
    48                              <1> 		; Note:	The input pointers need not be normalised yet.
    49                              <1> 		;	 Normalised means that the offset part is below 16.
    50                              <1> depack:
    51                              <1> 	lframe near
    52                              <1> %if _IMAGE_EXE || _DEBUG0 || _COUNTER
    53 0000011B 5589E5              <1> 	lenter early
    54                              <1>  %if _IMAGE_EXE
    55                              <1> 	lvar word,	exemode	; must be bp - 2!
    56 0000011E 53                  <1> 	 push bx
    57                              <1>   %if ?exemode != -2
    58                              <1>    %error exemode variable must be directly below bp
    59                              <1>   %endif
    60                              <1>  %endif
    61                              <1>  %if _DEBUG0 || _COUNTER
    62                              <1> 	xor bx, bx
    63                              <1>  %endif
    64                              <1>  %if _DEBUG0
    65                              <1> 	lvar word,	errordata
    66                              <1> 	 push bx
    67                              <1>  %endif
    68                              <1> %endif
    69                              <1> 	lvar word,	token_and_counter
    70                              <1> 	lequ ?token_and_counter, token
    71                              <1> 	lequ ?token_and_counter + 1, counter
    72                              <1> %if _COUNTER
    73                              <1> 	 push bx		; initialise counter (high byte) to zero
    74                              <1> %endif
    75                              <1> 	lvar dword,	src_behind_block
    76                              <1> 	lvar word,	frame_descriptor_flg_and_bd
    77 0000011F 8D66F6              <1> 	lenter
    78                              <1> 
    79 00000122 E88602              <1> 	call normalise_both_pointers
    80                              <1> 
    81                              <1> 	lvar dword,	src
    82 00000125 1E                  <1> 	 push ds
    83 00000126 56                  <1> 	 push si
    84                              <1> 	lvar dword,	original_dst
    85 00000127 06                  <1> 	 push es
    86 00000128 57                  <1> 	 push di
    87                              <1> 	lvar dword,	dst
    88 00000129 06                  <1> 	 push es
    89 0000012A 57                  <1> 	 push di
    90                              <1> 	lvar dword,	length_of_source
    91 0000012B 52                  <1> 	 push dx
    92 0000012C 51                  <1> 	 push cx
    93                              <1> 
    94                              <1> %if _PAYLOAD_KERNEL_MAX_PARAS
    95                              <1> 	cmp ax, -1		; no maximum specified ?
    96                              <1> 	je @FF			; retain -1 in ax -->
    97                              <1> 	test di, di		; do we need an additional paragraph ?
    98                              <1> 	jz @F			; no -->
    99                              <1> 	inc ax			; es + ax => paragraph after necessary part
   100                              <1> @@:
   101                              <1> 	mov dx, es
   102                              <1> 	add ax, dx		; => paragraph after necessary part
   103                              <1> 		; If the normalised destination pointer's segment grows
   104                              <1> 		;  to this segment then enough has been decompressed.
   105                              <1> d0	mov byte [bp + ?errordata], 40h
   106                              <1> 	jc .error		; should not carry
   107                              <1> @@:
   108                              <1> 	lvar word,	dst_max_segment
   109                              <1> 	 push ax
   110                              <1> %endif
   111                              <1> 
   112 0000012D 1E                  <1> 	 push ds
   113 0000012E 56                  <1> 	 push si
   114 0000012F E8D902              <1> 	call pointer_to_linear
   115                              <1> 
   116 00000132 89D3                <1> 	mov bx, dx
   117 00000134 91                  <1> 	xchg cx, ax		; bx:cx = source linear
   118                              <1> 
   119 00000135 06                  <1> 	 push es
   120 00000136 57                  <1> 	 push di
   121 00000137 E8D102              <1> 	call pointer_to_linear
   122                              <1> 
   123                              <1> %if _ALLOW_OVERLAPPING
   124 0000013A 034EE6              <1> 	add cx, word [bp + ?length_of_source]
   125 0000013D 135EE8              <1> 	adc bx, word [bp + ?length_of_source + 2]
   126                              <1> 		; In case of allowing overlapping source and destination,
   127                              <1> 		;  the ?length_of_destination variable is set to
   128                              <1> 		;  ?src + ?length_of_source - ?dst, allowing to write to
   129                              <1> 		;  all of the source buffer (with the checks already in place
   130                              <1> 		;  from the default handling). Additional checks are done by
   131                              <1> 		;  calling check_pointers_not_overlapping. This is done after
   132                              <1> 		;  every change of ?dst to verify that the write pointer stays
   133                              <1> 		;  below-or-equal the read pointer.
   134                              <1> 		; This means the remaining source data may be corrupted by a
   135                              <1> 		;  write, but nothing after the source data is written to,
   136                              <1> 		;  so the error handling (in INIT1 after the data) still works.
   137                              <1> %endif
   138                              <1> 
   139 00000140 29C1                <1> 	sub cx, ax
   140 00000142 19D3                <1> 	sbb bx, dx		; bx:cx = source linear - destination linear
   141                              <1> 
   142                              <1> 	lvar dword,	length_of_destination
   143 00000144 53                  <1> 	 push bx		; push into [bp + ?length_of_destination + 2]
   144 00000145 51                  <1> 	 push cx		; push into [bp + ?length_of_destination]
   145                              <1> 
   146                              <1> %if _ALLOW_OVERLAPPING
   147 00000146 E84602              <1> 	call check_pointers_not_overlapping
   148                              <1> 		; Note:	We initially check here that the write pointer is
   149                              <1> 		;	 low enough, ie below-or-equal the read pointer.
   150                              <1> 		;	 Doing this check here (as well as after any
   151                              <1> 		;	 copied match) allows us to drop the check done
   152                              <1> 		;	 after moving literal bytes.
   153                              <1> d0	mov byte [bp + ?errordata], 1Bh
   154 00000149 720D                <1> 	jc .error
   155                              <1> %endif
   156                              <1> 
   157                              <1> .loop_frame:
   158 0000014B 31C9                <1> 	xor cx, cx
   159 0000014D 394EE8              <1> 	cmp word [bp + ?length_of_source + 2], cx
   160 00000150 750B                <1> 	jne @F
   161 00000152 394EE6              <1> 	cmp word [bp + ?length_of_source], cx
   162 00000155 7506                <1> 	jne @F
   163                              <1> 
   164                              <1> .end:
   165 00000157 A8                  <1> 	db __TEST_IMM8			; (NC)
   166                              <1> .error:
   167 00000158 F9                  <1> 	stc
   168                              <1> 
   169                              <1> %if _COUNTER
   170                              <1> 	lahf
   171                              <1> 	mov al, 13
   172                              <1> 	call disp_al
   173                              <1> 	mov al, 10
   174                              <1> 	call disp_al
   175                              <1> 	sahf
   176                              <1> %endif
   177                              <1> d0	mov bx, word [bp + ?errordata]
   178 00000159 89EC5D              <1> 	lleave code
   179 0000015C C3                  <1> 	lret
   180                              <1> 
   181                              <1> 
   182                              <1> @@:
   183 0000015D B104                <1> 	mov cl, 4
   184 0000015F E80A02              <1> 	call read_le_data_src
   185                              <1> d0	mov byte [bp + ?errordata], 1
   186 00000162 72F4                <1> 	jc .error
   187                              <1> 
   188 00000164 81FA4D18            <1> 	cmp dx, 184Dh			; signature match ?
   189                              <1> d0	mov byte [bp + ?errordata], 2
   190 00000168 75EE                <1> 	jne .error			; no -->
   191 0000016A 3D0422              <1> 	cmp ax, 2204h			; usual frame ?
   192 0000016D 7427                <1> 	je @F				; yes -->
   193                              <1> 
   194 0000016F 24F0                <1> 	and al,  ~0Fh
   195 00000171 3D502A              <1> 	cmp ax, 2A50h			; skippable frame ?
   196                              <1> d0	mov byte [bp + ?errordata], 3
   197 00000174 75E2                <1> 	jne .error			; no -->
   198                              <1> 
   199                              <1> 		; cx = 4 still here
   200 00000176 E8F301              <1> 	call read_le_data_src		; read length of skippable frame
   201                              <1> d0	mov byte [bp + ?errordata], 4
   202 00000179 72DD                <1> 	jc .error
   203                              <1> 
   204 0000017B 1E                  <1> 	 push ds
   205 0000017C 56                  <1> 	 push si
   206 0000017D 89C1                <1> 	mov cx, ax
   207 0000017F 89D3                <1> 	mov bx, dx			; displacement = skippable frame length
   208 00000181 E85802              <1> 	call normalise_pointer_with_displacement_bxcx
   209 00000184 8F46F2              <1> 	 pop word [bp + ?src]
   210 00000187 8F46F4              <1> 	 pop word [bp + ?src + 2]	; ?src += length
   211                              <1> d0	mov byte [bp + ?errordata], 1Dh
   212 0000018A 72CC                <1> 	jc .error
   213                              <1> 
   214 0000018C 2946E6              <1> 	sub word [bp + ?length_of_source], ax
   215 0000018F 1956E8              <1> 	sbb word [bp + ?length_of_source + 2], dx
   216                              <1> 					; ?length_of_source -= length
   217                              <1> d0	mov byte [bp + ?errordata], 5
   218 00000192 72C4                <1> 	jc .error
   219 00000194 EBB5                <1> 	jmp .loop_frame
   220                              <1> 
   221                              <1> 
   222                              <1> @@:
   223 00000196 B102                <1> 	mov cl, 2
   224 00000198 E8D101              <1> 	call read_le_data_src		; read FLG (low byte), BD (high byte)
   225                              <1> d0	mov byte [bp + ?errordata], 6
   226                              <1> .error_CY_1:
   227 0000019B 72BB                <1> 	jc .error
   228                              <1> 
   229 0000019D 8946F6              <1> 	mov word [bp + ?frame_descriptor_flg_and_bd], ax
   230 000001A0 89C3                <1> 	mov bx, ax
   231 000001A2 80E3C0              <1> 	and bl, lz4_flg_version_mask
   232 000001A5 80FB40              <1> 	cmp bl, lz4_flg_version		; version correct ?
   233                              <1> d0	mov byte [bp + ?errordata], 7
   234                              <1> .error_NZ_1:
   235 000001A8 75AE                <1> 	jne .error			; no -->
   236                              <1> 
   237 000001AA A9038F              <1> 	test ax, (lz4_bd_reserved_mask << 8) 		 | lz4_flg_reserved | lz4_flg_dict_id	; unsupported bits set?
   239                              <1> d0	mov byte [bp + ?errordata], 8
   240 000001AD 75F9                <1> 	jnz .error_NZ_1			; yes -->
   241                              <1> 
   242                              <1> 		; not checked: lz4_flg_b_indep (linked is always supported),
   243                              <1> 		;  lz4_flg_b_checksum, lz4_flg_c_checksum (both checked later)
   244                              <1> 
   245 000001AF A808                <1> 	test al, lz4_flg_c_size		; content size field present ?
   246 000001B1 7407                <1> 	jz @F				; no -->
   247 000001B3 B108                <1> 	mov cl, 8
   248 000001B5 E8B401              <1> 	call read_le_data_src		; (skip content size field)
   249                              <1> d0	mov byte [bp + ?errordata], 9
   250 000001B8 72E1                <1> 	jc .error_CY_1
   251                              <1> @@:
   252 000001BA B101                <1> 	mov cl, 1
   253 000001BC E8AD01              <1> 	call read_le_data_src		; (skip header checksum field)
   254                              <1> d0	mov byte [bp + ?errordata], 0Ah
   255 000001BF 72DA                <1> 	jc .error_CY_1
   256                              <1> 
   257                              <1> .loop_block:
   258                              <1> %if _COUNTER
   259                              <1> 	mov al, '#'
   260                              <1> 	call disp_al
   261                              <1> %endif
   262 000001C1 B90400              <1> 	mov cx, 4
   263 000001C4 E8A501              <1> 	call read_le_data_src		; read block size field
   264                              <1> 					; leaves ds:si -> source
   265                              <1> d0	mov byte [bp + ?errordata], 0Bh
   266 000001C7 72D2                <1> 	jc .error_CY_1
   267                              <1> 
   268 000001C9 85D2                <1> 	test dx, dx			; nonzero ?
   269 000001CB 7512                <1> 	jnz @FF
   270 000001CD 85C0                <1> 	test ax, ax
   271 000001CF 750E                <1> 	jnz @FF				; yes -->
   272                              <1> 
   273 000001D1 F646F604            <1> 	test byte [bp + ?frame_descriptor_flg_and_bd], lz4_flg_c_checksum
   274 000001D5 7405                <1> 	jz @F
   275                              <1> 		; cx = 4 already
   276 000001D7 E89201              <1> 	call read_le_data_src		; (skip c checksum field)
   277                              <1> d0	mov byte [bp + ?errordata], 0Ch
   278                              <1> .error_CY_2:
   279 000001DA 72BF                <1> 	jc .error_CY_1
   280                              <1> @@:
   281 000001DC E96CFF              <1> 	jmp .loop_frame			; next frame, if any -->
   282                              <1> 
   283                              <1> 
   284                              <1> @@:
   285 000001DF 1E                  <1> 	 push ds
   286 000001E0 56                  <1> 	 push si			; -> source
   287 000001E1 89D3                <1> 	mov bx, dx
   288 000001E3 80E77F              <1> 	and bh, ~80h			; clear "uncompressed" flag
   289 000001E6 89C1                <1> 	mov cx, ax			; bx:cx = block size
   290 000001E8 E8F101              <1> 	call normalise_pointer_with_displacement_bxcx
   291                              <1> 					; -> source after block
   292 000001EB 8F46F8              <1> 	 pop word [bp + ?src_behind_block]
   293 000001EE 8F46FA              <1> 	 pop word [bp + ?src_behind_block + 2]
   294                              <1> d0	mov byte [bp + ?errordata], 1Eh
   295 000001F1 7303E962FF          <1> 	jc .error
   296 000001F6 F6C680              <1> 	test dh, 80h			; is it uncompressed ?
   297 000001F9 7448                <1> 	jz .compressed			; no -->
   298                              <1> 
   299                              <1> .uncompressed:
   300 000001FB 87DA                <1> 	xchg bx, dx			; get size without "uncompressed" flag
   301 000001FD E8EB00              <1> 	call lz4_copy_literal_or_uncompressed
   302                              <1> d0	mov byte [bp + ?errordata], 0Dh
   303 00000200 72D8                <1> 	jc .error_CY_2
   304                              <1> 
   305                              <1> .end_block:
   306 00000202 FF76FA              <1> 	 push word [bp + ?src_behind_block + 2]
   307 00000205 FF76F8              <1> 	 push word [bp + ?src_behind_block]
   308 00000208 E80002              <1> 	call pointer_to_linear
   309 0000020B 89C1                <1> 	mov cx, ax
   310 0000020D 89D3                <1> 	mov bx, dx			; bx:cx = linear ?src_behind_block
   311                              <1> 
   312 0000020F FF76F4              <1> 	 push word [bp + ?src + 2]
   313 00000212 FF76F2              <1> 	 push word [bp + ?src]
   314 00000215 E8F301              <1> 	call pointer_to_linear		; dx:ax = linear ?src
   315                              <1> 
   316 00000218 29C1                <1> 	sub cx, ax
   317 0000021A 19D3                <1> 	sbb bx, dx			; ?src_behind_block - ?src
   318                              <1> 					;  = amount to skip
   319                              <1> d0	mov byte [bp + ?errordata], 0Fh
   320 0000021C 72BC                <1> 	jc .error_CY_2
   321                              <1> 
   322 0000021E 294EE6              <1> 	sub word [bp + ?length_of_source], cx
   323 00000221 195EE8              <1> 	sbb word [bp + ?length_of_source + 2], bx
   324                              <1> d0	mov byte [bp + ?errordata], 10h
   325                              <1> .error_CY_3:
   326 00000224 72B4                <1> 	jc .error_CY_2			; not enough data to skip -->
   327                              <1> 
   328 00000226 FF76FA              <1> 	push word [bp + ?src_behind_block + 2]
   329 00000229 FF76F8              <1> 	push word [bp + ?src_behind_block]
   330 0000022C 8F46F2              <1> 	pop word [bp + ?src]
   331 0000022F 8F46F4              <1> 	pop word [bp + ?src + 2]	; set ?src -> behind block
   332                              <1> 
   333 00000232 F646F610            <1> 	test byte [bp + ?frame_descriptor_flg_and_bd], lz4_flg_b_checksum
   334 00000236 7408                <1> 	jz @F				; no block checksum -->
   335 00000238 B90400              <1> 	mov cx, 4
   336 0000023B E82E01              <1> 	call read_le_data_src		; (skip block checksum field)
   337                              <1> d0	mov byte [bp + ?errordata], 0Eh
   338 0000023E 72E4                <1> 	jc .error_CY_3
   339                              <1> @@:
   340                              <1> 
   341 00000240 E97EFF              <1> 	jmp .loop_block
   342                              <1> 
   343                              <1> 
   344                              <1> .compressed:
   345                              <1> .loop_sequence:
   346                              <1> 
   347                              <1> %if _COUNTER
   348                              <1> 	inc byte [bp + ?counter]
   349                              <1> 	test byte [bp + ?counter], _COUNTER - 1
   350                              <1> 	jnz @F
   351                              <1> 	mov al, '.'
   352                              <1> 	call disp_al
   353                              <1> @@:
   354                              <1> %endif
   355                              <1> 
   356 00000243 BB[BE02]            <1> 	mov bx, .lodsb
   357 00000246 E8EB00              <1> 	call lz4_func_bx_with_src	; load token
   358                              <1> d0	mov byte [bp + ?errordata], 11h
   359 00000249 72D9                <1> 	jc .error_CY_3
   360 0000024B 8846FC              <1> 	mov byte [bp + ?token], al
   361                              <1> 
   362 0000024E B104                <1> 	mov cl, 4
   363 00000250 D2E8                <1> 	shr al, cl
   364 00000252 E8F000              <1> 	call lz4_get_length		; dx:cx = length of literals
   365                              <1> d0	mov byte [bp + ?errordata], 12h
   366 00000255 72CD                <1> 	jc .error_CY_3
   367                              <1> 
   368 00000257 89C8                <1> 	mov ax, cx			; dx:ax = length of literals (may be 0)
   369 00000259 E88F00              <1> 	call lz4_copy_literal_or_uncompressed
   370                              <1> 					; copy over the literals
   371                              <1> d0	mov byte [bp + ?errordata], 13h
   372 0000025C 72C6                <1> 	jc .error_CY_3
   373                              <1> 
   374 0000025E E87500              <1> 	call lz4_check_behind_block_higher_than_src
   375                              <1> 					; do we have more data in this block ?
   376 00000261 7258                <1> 	jc .end_sequence		; no -->
   377                              <1> 
   378 00000263 BB[CA02]            <1> 	mov bx, .lodsw
   379 00000266 E8CB00              <1> 	call lz4_func_bx_with_src	; load match offset
   380                              <1> d0	mov byte [bp + ?errordata], 14h
   381                              <1> .error_CY_4:
   382 00000269 72B9                <1> 	jc .error_CY_3
   383                              <1> 
   384 0000026B 85C0                <1> 	test ax, ax			; offset is 0 ?
   385                              <1> d0	mov byte [bp + ?errordata], 15h
   386 0000026D F9                  <1> 	stc
   387 0000026E 74F9                <1> 	jz .error_CY_4			; yes, error -->
   388                              <1> 
   389 00000270 89C1                <1> 	mov cx, ax
   390 00000272 F7D9                <1> 	neg cx				; -1 .. -65535
   391 00000274 BBFFFF              <1> 	mov bx, -1			; sign-extend (always negative sense)
   392                              <1> 
   393 00000277 FF76EC              <1> 	 push word [bp + ?dst + 2]
   394 0000027A FF76EA              <1> 	 push word [bp + ?dst]
   395 0000027D E85C01              <1> 	call normalise_pointer_with_displacement_bxcx
   396 00000280 5E                  <1> 	 pop si
   397 00000281 1F                  <1> 	 pop ds				; ds:si -> source of matching
   398                              <1> d0	mov byte [bp + ?errordata], 1Fh
   399 00000282 7203E9D1FE          <1> 	jnc .error
   400                              <1> 
   401 00000287 8CD8                <1> 	mov ax, ds			; is source of matching within ?dst ?
   402 00000289 3946F0              <1> 	cmp word [bp + ?original_dst + 2], ax
   403                              <1> d0	mov byte [bp + ?errordata], 16h
   404                              <1> .error_A_1:
   405 0000028C 7603E9C7FE          <1> 	ja .error
   406 00000291 7205                <1> 	jb @F
   407 00000293 3976EE              <1> 	cmp word [bp + ?original_dst], si
   408                              <1> d0	mov byte [bp + ?errordata], 17h
   409 00000296 77F4                <1> 	ja .error_A_1			; no, ?original_dst is above it -->
   410                              <1> @@:
   411                              <1> 
   412 00000298 8A46FC              <1> 	mov al, byte [bp + ?token]
   413 0000029B 240F                <1> 	and al, 0Fh			; get match length from token
   414 0000029D E8A500              <1> 	call lz4_get_length		; dx:cx = length of match - 4
   415                              <1> d0	mov byte [bp + ?errordata], 18h
   416 000002A0 72C7                <1> 	jc .error_CY_4
   417 000002A2 89C8                <1> 	mov ax, cx			; dx:ax = length of match - 4
   418 000002A4 83C004              <1> 	add ax, 4
   419 000002A7 83D200              <1> 	adc dx, 0			; dx:ax = length of match
   420                              <1> d0	mov byte [bp + ?errordata], 19h
   421 000002AA 72BD                <1> 	jc .error_CY_4
   422 000002AC E85100              <1> 	call lz4_copy_data		; give ?dst -> dest, ds:si -> source
   423                              <1> 		; returns: ?dst incremented, ds:si -> after match source
   424                              <1> d0	mov byte [bp + ?errordata], 1Ah
   425 000002AF 72B8                <1> 	jc .error_CY_4
   426                              <1> 
   427                              <1> %if _ALLOW_OVERLAPPING
   428 000002B1 E8DB00              <1> 	call check_pointers_not_overlapping
   429                              <1> d0	mov byte [bp + ?errordata], 1Ch
   430 000002B4 72B3                <1> 	jc .error_CY_4
   431                              <1> %endif
   432                              <1> 
   433                              <1> .next_sequence:
   434 000002B6 E81D00              <1> 	call lz4_check_behind_block_higher_than_src
   435 000002B9 7388                <1> 	jnc .loop_sequence		; process next sequence -->
   436                              <1> 
   437                              <1> .end_sequence:
   438 000002BB E944FF              <1> 	jmp .end_block
   439                              <1> 
   440                              <1> 
   441                              <1> 		; INP:	ds:si -> source data
   442                              <1> 		;	?length_of_source
   443                              <1> 		; OUT:	CY if error (source buffer too small)
   444                              <1> 		;	NC if success,
   445                              <1> 		;	 al = value read
   446                              <1> 		;	 ds:si incremented (NOT normalised)
   447                              <1> 		;	 ?length_of_source decremented
   448                              <1> .lodsb:
   449 000002BE 836EE601            <1> 	sub word [bp + ?length_of_source], 1
   450 000002C2 835EE800            <1> 	sbb word [bp + ?length_of_source + 2], 0
   451 000002C6 7201                <1> 	jb .retn
   452 000002C8 AC                  <1> 	lodsb
   453                              <1> .retn:
   454 000002C9 C3                  <1> 	retn
   455                              <1> 
   456                              <1> 
   457                              <1> 		; INP:	ds:si -> source data
   458                              <1> 		;	?length_of_source
   459                              <1> 		; OUT:	CY if error (source buffer too small)
   460                              <1> 		;	NC if success,
   461                              <1> 		;	 ax = value read
   462                              <1> 		;	 ds:si incremented (NOT normalised)
   463                              <1> 		;	 ?length_of_source decremented
   464                              <1> .lodsw:
   465 000002CA 836EE602            <1> 	sub word [bp + ?length_of_source], 2
   466 000002CE 835EE800            <1> 	sbb word [bp + ?length_of_source + 2], 0
   467 000002D2 72F5                <1> 	jb .retn
   468 000002D4 AD                  <1> 	lodsw
   469 000002D5 C3                  <1> 	retn
   470                              <1> 
   471                              <1> 
   472                              <1> 		; INP:	?src_behind_block -> block end (normalised)
   473                              <1> 		;	?src -> current address in source buffer
   474                              <1> 		; OUT:	CY if ?src is >= (above-or-equal) ?src_behind_block
   475                              <1> 		;	NC else (?src is below ?src_behind_block, more data)
   476                              <1> 		; CHG:	ax, dx
   477                              <1> 		;
   478                              <1> 		; Note:	Although ?src should always equal ?src_behind_block
   479                              <1> 		;	 if CY here, we actually check for above-or-equal.
   480                              <1> lz4_check_behind_block_higher_than_src:
   481 000002D6 8B56FA              <1> 	mov dx, word [bp + ?src_behind_block + 2]
   482 000002D9 8B46F8              <1> 	mov ax, word [bp + ?src_behind_block]
   483                              <1> 					; dx:ax -> behind block
   484                              <1> 
   485 000002DC 3956F4              <1> 	cmp word [bp + ?src + 2], dx	; compare segment
   486 000002DF 7708                <1> 	ja .error			; ?src > dx:ax -->
   487 000002E1 7205                <1> 	jb .success			; ?src < dx:ax -->
   488 000002E3 3946F2              <1> 	cmp word [bp + ?src], ax	; segment is same, compare offset
   489 000002E6 7301                <1> 	jae .error			; ?src >= dx:ax -->
   490                              <1> 
   491                              <1> .success:
   492 000002E8 A8                  <1> 	db __TEST_IMM8			; (NC)
   493                              <1> .error:
   494 000002E9 F9                  <1> 	stc
   495 000002EA C3                  <1> 	retn
   496                              <1> 
   497                              <1> 
   498                              <1> 		; INP:	?dst -> destination (normalised)
   499                              <1> 		;	?src -> source (normalised)
   500                              <1> 		;	dx:ax = how long the literal data is (0 is valid)
   501                              <1> 		; OUT:	?dst incremented (normalised)
   502                              <1> 		;	?src incremented (normalised)
   503                              <1> 		;	?length_of_destination shortened
   504                              <1> 		;	?length_of_source shortened
   505                              <1> 		;	CY if error (either buffer too small)
   506                              <1> 		;	NC if success
   507                              <1> 		; CHG:	cx, dx, ax, es, di, ds, si
   508                              <1> lz4_copy_literal_or_uncompressed:
   509 000002EB C576F2              <1> 	lds si, [bp + ?src]
   510                              <1> 
   511 000002EE 2946E6              <1> 	sub word [bp + ?length_of_source], ax
   512 000002F1 1956E8              <1> 	sbb word [bp + ?length_of_source + 2], dx
   513                              <1> 					; enough data exists ?
   514 000002F4 7209                <1> 	jb .return			; no --> (CY)
   515                              <1> 
   516 000002F6 E80700              <1> 	call lz4_copy_data		; CY/NC depending on status
   517                              <1> 
   518 000002F9 8976F2              <1> 	mov word [bp + ?src], si
   519 000002FC 8C5EF4              <1> 	mov word [bp + ?src + 2], ds
   520                              <1> 					; (CF preserved)
   521                              <1> .return:
   522 000002FF C3                  <1> 	retn
   523                              <1> 
   524                              <1> 
   525                              <1> 		; INP:	?dst -> destination (normalised)
   526                              <1> 		;	ds:si -> source (normalised)
   527                              <1> 		;	dx:ax = how long the data is (0 is valid)
   528                              <1> 		; OUT:	?dst incremented (normalised)
   529                              <1> 		;	ds:si incremented (normalised)
   530                              <1> 		;	?length_of_destination shortened
   531                              <1> 		;	CY if error (buffer too small)
   532                              <1> 		;	NC if success
   533                              <1> 		;	Instead of returning, this may jump to depack.end
   534                              <1> 		;	 if the destination segment grows up to the value
   535                              <1> 		;	 stored in the ?dst_max_segment variable. It is
   536                              <1> 		;	 assumed that this will use the stack frame to leave
   537                              <1> 		;	 the function, therefore discarding any of the stack
   538                              <1> 		;	 contents between the frame and the stack top.
   539                              <1> 		; CHG:	cx, dx, ax, es, di, ds, si
   540                              <1> lz4_copy_data:
   541                              <1> d0	inc byte [bp + ?errordata + 1]
   542 00000300 2946E2              <1> 	sub word [bp + ?length_of_destination], ax
   543 00000303 1956E4              <1> 	sbb word [bp + ?length_of_destination + 2], dx
   544                              <1> 					; enough space left ?
   545 00000306 722A                <1> 	jb .error			; no -->
   546                              <1> 
   547 00000308 C47EEA              <1> 	les di, [bp + ?dst]
   548                              <1> .loop:
   549 0000030B B9F0FF              <1> 	mov cx, 64 * 1024 - 16		; block size
   550                              <1> 		; If both pointers are normalised, moving 0FFF0h bytes is
   551                              <1> 		;  valid and results in a maximum offset of 0FFFFh in either.
   552                              <1> 
   553 0000030E 85D2                <1> 	test dx, dx			; >= 1_0000h ?
   554 00000310 7508                <1> 	jnz @F				; yes, full block -->
   555 00000312 85C0                <1> 	test ax, ax			; == 0 ?
   556 00000314 7415                <1> 	jz .end				; yes, done -->
   557 00000316 39C8                <1> 	cmp ax, cx			; can move in one (last) block ?
   558 00000318 7607                <1> 	jbe .last			; yes -->
   559                              <1> @@:
   560                              <1> 					; no, move one block and continue
   561 0000031A 29C8                <1> 	sub ax, cx
   562 0000031C 83DA00              <1> 	sbb dx, 0			; left over remaining
   563 0000031F EB03                <1> 	jmp .copy
   564                              <1> 
   565                              <1> .last:
   566 00000321 91                  <1> 	xchg cx, ax			; cx = remaining length
   567 00000322 31C0                <1> 	xor ax, ax			; no more remaining
   568                              <1> 
   569                              <1> .copy:
   570 00000324 F3A4                <1> 	rep movsb			; move one block (full or partial)
   571                              <1> 
   572 00000326 E88200              <1> 	call normalise_both_pointers
   573 00000329 EBE0                <1> 	jmp .loop
   574                              <1> 
   575                              <1> .end:
   576 0000032B 897EEA              <1> 	mov word [bp + ?dst], di
   577 0000032E 8C46EC              <1> 	mov word [bp + ?dst + 2], es
   578                              <1> %if _PAYLOAD_KERNEL_MAX_PARAS
   579                              <1> 	mov ax, es
   580                              <1> 	cmp ax, word [bp + ?dst_max_segment]
   581                              <1> 	jae depack.end
   582                              <1> %endif
   583                              <1> 
   584 00000331 A8                  <1> 	db __TEST_IMM8			; (NC)
   585                              <1> .error:
   586 00000332 F9                  <1> 	stc
   587 00000333 C3                  <1> 	retn
   588                              <1> 
   589                              <1> 
   590                              <1> 		; INP:	?src -> source data
   591                              <1> 		;	bx = function to call
   592                              <1> 		; OUT:	ds:si = ?src -> behind data (normalised)
   593                              <1> 		;	CY if error (buffer too small)
   594                              <1> 		;	NC if success
   595                              <1> 		;	(CF is passed from what the function at bx returns)
   596                              <1> 		;
   597                              <1> 		; Protocol of function in bx called by this:
   598                              <1> 		; INP:	ds:si -> source data
   599                              <1> 		;	?length_of_source
   600                              <1> 		;	(may take more inputs depending on function)
   601                              <1> 		; OUT:	ds:si incremented (NOT normalised)
   602                              <1> 		;	?length_of_source decremented
   603                              <1> 		;	CY if error (buffer too small or other error)
   604                              <1> 		;	NC if success
   605                              <1> 		;	(may give more outputs depending on function)
   606                              <1> lz4_func_bx_with_src:
   607 00000334 C576F2              <1> 	lds si, [bp + ?src]
   608 00000337 FFD3                <1> 	call bx
   609 00000339 9C                  <1> 	pushf
   610 0000033A E87500              <1> 	call normalise_dssi_pointer
   611 0000033D 8976F2              <1> 	mov word [bp + ?src], si
   612 00000340 8C5EF4              <1> 	mov word [bp + ?src + 2], ds
   613 00000343 9D                  <1> 	popf
   614 00000344 C3                  <1> 	retn
   615                              <1> 
   616                              <1> 
   617                              <1> 		; INP:	?src -> source data
   618                              <1> 		;	?length_of_source
   619                              <1> 		;	al = first field data (0 to 15)
   620                              <1> 		; OUT:	dx:cx = length value
   621                              <1> 		;	?src incremented
   622                              <1> 		;	?length_of_source decremented
   623                              <1> 		;	CY if error (too high or out of source data)
   624                              <1> 		;	NC if success
   625                              <1> 		; CHG:	ax, bx
   626                              <1> lz4_get_length:
   627 00000345 1E                  <1> 	push ds
   628 00000346 56                  <1> 	push si
   629                              <1> 
   630 00000347 31C9                <1> 	xor cx, cx
   631 00000349 31D2                <1> 	xor dx, dx			; dx:cx = length field
   632 0000034B B40F                <1> 	mov ah, 15			; first byte maximum value
   633                              <1> .loop:
   634 0000034D 00C1                <1> 	add cl, al
   635 0000034F 80D500              <1> 	adc ch, 0
   636 00000352 83D200              <1> 	adc dx, 0			; add in this byte
   637 00000355 7211                <1> 	jc .error			; if too high -->
   638 00000357 38E0                <1> 	cmp al, ah			; is this the maximum value ?
   639 00000359 750C                <1> 	jne .end			; no -->
   640 0000035B BB[BE02]            <1> 	mov bx, depack.lodsb
   641 0000035E E8D3FF              <1> 	call lz4_func_bx_with_src	; load next byte
   642 00000361 7205                <1> 	jc .error
   643 00000363 B4FF                <1> 	mov ah, 255			; next maximum value is 255
   644 00000365 EBE6                <1> 	jmp .loop			; -->
   645                              <1> 
   646                              <1> .end:
   647 00000367 A8                  <1> 	db __TEST_IMM8			; (NC)
   648                              <1> .error:
   649 00000368 F9                  <1> 	stc
   650                              <1> 
   651 00000369 5E                  <1> 	pop si
   652 0000036A 1F                  <1> 	pop ds
   653 0000036B C3                  <1> 	retn
   654                              <1> 
   655                              <1> 
   656                              <1> 		; INP:	?src -> source data (normalised)
   657                              <1> 		;	cx = size to read, 1 to 4 or higher (at most 0FFF0h)
   658                              <1> 		;	?length_of_source
   659                              <1> 		; OUT:	If cx >= 4,
   660                              <1> 		;	 dx:ax = value read as little-endian
   661                              <1> 		;	If cx == 3,
   662                              <1> 		;	 dl:ax = value read
   663                              <1> 		;	If cx == 2,
   664                              <1> 		;	 ax = value read
   665                              <1> 		;	If cx == 1,
   666                              <1> 		;	 al = value read
   667                              <1> 		;	If cx == 0,
   668                              <1> 		;	 invalid, CY
   669                              <1> 		;	ds:si = ?src = incremented source (normalised)
   670                              <1> 		;	?length_of_source decremented
   671                              <1> 		;	CY if error (source buffer too small, or cx zero)
   672                              <1> 		;	NC if success
   673                              <1> 		; CHG:	ds, si, dx, ax
   674                              <1> 		;
   675                              <1> 		; Note:	Always reads four bytes at INP:ds:si -> data.
   676                              <1> read_le_data_src:
   677 0000036C E31F                <1> 	jcxz .error
   678                              <1> 
   679 0000036E 294EE6              <1> 	sub word [bp + ?length_of_source], cx
   680 00000371 835EE800            <1> 	sbb word [bp + ?length_of_source + 2], 0
   681 00000375 7216                <1> 	jb .error
   682                              <1> 
   683 00000377 C576F2              <1> 	lds si, [bp + ?src]
   684                              <1> 
   685 0000037A AD                  <1> 	lodsw
   686 0000037B 92                  <1> 	xchg ax, dx		; dx = low word (first)
   687 0000037C AD                  <1> 	lodsw			; ax = high word (second)
   688 0000037D 92                  <1> 	xchg ax, dx		; fix order, dx:ax = dword
   689                              <1> 
   690 0000037E 83EE04              <1> 	sub si, 4
   691 00000381 01CE                <1> 	add si, cx
   692                              <1> 
   693 00000383 E82C00              <1> 	call normalise_dssi_pointer
   694 00000386 8976F2              <1> 	mov word [bp + ?src], si
   695 00000389 8C5EF4              <1> 	mov word [bp + ?src + 2], ds
   696                              <1> 
   697 0000038C A8                  <1> 	db __TEST_IMM8		; (NC)
   698                              <1> .error:
   699 0000038D F9                  <1> 	stc
   700 0000038E C3                  <1> 	retn
   701                              <1> 
  1012                                  %endif
  1013                                  
  1014                                  
  1015                                  %if _SNAPPY
  1016                                  	%include "snappy.asm"
  1017                                  %endif
  1018                                  
  1019                                  
  1020                                  %if _EXODECR
  1021                                  	%include "exodecr.asm"
  1022                                  %endif
  1023                                  
  1024                                  
  1025                                  %if _X
  1026                                  	%include "x.asm"
  1027                                  %endif
  1028                                  
  1029                                  
  1030                                  %if _HEATSHRINK
  1031                                  	%include "heatshr.asm"
  1032                                  %endif
  1033                                  
  1034                                  
  1035                                  %if _LZD
  1036                                  	%include "lzd.asm"
  1037                                  %endif
  1038                                  
  1039                                  
  1040                                  %if _LZO
  1041                                  	%include "lzo.asm"
  1042                                  %endif
  1043                                  
  1044                                  
  1045                                  %if _LZSA2
  1046                                  	%include "lzsa2.asm"
  1047                                  %endif
  1048                                  
  1049                                  
  1050                                  %if _APL
  1051                                  	%include "apl.asm"
  1052                                  %endif
  1053                                  
  1054                                  
  1055                                  %if _BZP
  1056                                  	%include "bzp.asm"
  1057                                  %endif
  1058                                  
  1059                                  
  1060                                  %if _ALLOW_OVERLAPPING
  1061                                  		; INP:	?src, ?dst
  1062                                  		; OUT:	CY if error (?src < ?dst)
  1063                                  		;	NC if success
  1064                                  		; CHG:	ax, bx, cx, dx
  1065                                  check_pointers_not_overlapping:
  1066                                  %if CHECK_POINTERS_VARIABLE_DST
  1067 0000038F FF76EC                  	 push word [bp + ?dst + 2]
  1068 00000392 FF76EA                  	 push word [bp + ?dst]
  1069                                  %else
  1070                                  	 push es
  1071                                  	 push di
  1072                                  %endif
  1073 00000395 E87300                  	call pointer_to_linear
  1074                                  
  1075 00000398 91                      	xchg cx, ax
  1076 00000399 87DA                    	xchg bx, dx			; bx:cx = linear ?dst after write
  1077                                  
  1078                                  %if CHECK_POINTERS_VARIABLE_SRC
  1079 0000039B FF76F4                  	 push word [bp + ?src + 2]
  1080 0000039E FF76F2                  	 push word [bp + ?src]
  1081                                  %else
  1082                                  	 push ds
  1083                                  	 push si
  1084                                  %endif
  1085 000003A1 E86700                  	call pointer_to_linear		; dx:ax = linear ?src before next read
  1086                                  
  1087 000003A4 39DA                    	cmp dx, bx			; ?src >= ?dst ?
  1088 000003A6 7502                    	jne @F
  1089 000003A8 39C8                    	cmp ax, cx
  1090                                  @@:
  1091                                  					; (CY) if error (src < dst)
  1092                                  					; (NC) if no error
  1093 000003AA C3                      	retn
  1094                                  %endif
  1095                                  
  1096                                  	; This leaves the lframe context created within the
  1097                                  	;  specific depacker's file. The above function
  1098                                  	;  check_pointers_not_overlapping uses the frame.
  1099                                  	lleave ctx
  1100                                  
  1101                                  
  1102                                  		; INP:	ds:si = pointer
  1103                                  		;	es:di = pointer
  1104                                  		; OUT:	ds:si normalised
  1105                                  		;	es:di normalised
  1106                                  normalise_both_pointers:
  1107 000003AB 06                      	 push es
  1108 000003AC 57                      	 push di
  1109 000003AD E80A00                  	call normalise_pointer
  1110 000003B0 5F                      	 pop di
  1111 000003B1 07                      	 pop es
  1112                                  
  1113                                  normalise_dssi_pointer:
  1114 000003B2 1E                      	 push ds
  1115 000003B3 56                      	 push si
  1116 000003B4 E80300                  	call normalise_pointer
  1117 000003B7 5E                      	 pop si
  1118 000003B8 1F                      	 pop ds
  1119 000003B9 C3                      	retn
  1120                                  
  1121                                  
  1122                                  		; INP:	word [ss:sp + 2] = segment
  1123                                  		;	word [ss:sp] = offset
  1124                                  		;
  1125                                  		; Note:	Does not work correctly with pointers that point to
  1126                                  		;	 a HMA location. Do not use then!
  1127                                  normalise_pointer:
  1128                                  %if NEED_NORMALISE_POINTER_WITH_DISPLACEMENT
  1129                                  	lframe near
  1130                                  	lpar word,	segment
  1131                                  	lpar word,	offset
  1132                                  	lpar_return
  1133 000003BA 5589E5                  	lenter
  1134                                  
  1135 000003BD 837E040F                	cmp word [bp + ?offset], 15
  1136 000003C1 7617                    	jbe .ret
  1137                                  
  1138 000003C3 53                      	push bx
  1139 000003C4 51                      	push cx
  1140                                  
  1141 000003C5 31DB                    	xor bx, bx
  1142 000003C7 31C9                    	xor cx, cx
  1143 000003C9 FF7606                  	 push word [bp + ?segment]
  1144 000003CC FF7604                  	 push word [bp + ?offset]
  1145 000003CF E80A00                  	call normalise_pointer_with_displacement_bxcx
  1146 000003D2 8F4604                  	 pop word [bp + ?offset]
  1147 000003D5 8F4606                  	 pop word [bp + ?segment]
  1148                                  
  1149 000003D8 59                      	pop cx
  1150 000003D9 5B                      	pop bx
  1151                                  .ret:
  1152 000003DA 5D                      	lleave
  1153 000003DB C3                      	lret
  1154                                  
  1155                                  
  1156                                  		; INP:	word [ss:sp + 2] = segment
  1157                                  		;	word [ss:sp] = offset
  1158                                  		;	bx:cx = add/sub displacement
  1159                                  		; OUT:	CY if the displacement carries
  1160                                  		;	NC if not
  1161                                  normalise_pointer_with_displacement_bxcx:
  1162                                  %endif
  1163                                  	lframe near
  1164                                  	lpar word,	segment
  1165                                  	lpar word,	offset
  1166                                  	lpar_return
  1167 000003DC 5589E5                  	lenter
  1168                                  
  1169                                  %ifn NEED_NORMALISE_POINTER_WITH_DISPLACEMENT
  1170                                  	cmp word [bp + ?offset], 15
  1171                                  	jbe .ret
  1172                                  %endif
  1173                                  
  1174 000003DF 50                      	push ax
  1175 000003E0 51                      	push cx
  1176 000003E1 52                      	push dx
  1177                                  
  1178 000003E2 FF7606                  	 push word [bp + ?segment]
  1179 000003E5 FF7604                  	 push word [bp + ?offset]
  1180 000003E8 E82000                  	call pointer_to_linear
  1181                                  
  1182                                  %if NEED_NORMALISE_POINTER_WITH_DISPLACEMENT
  1183                                  	; push bx
  1184                                  	; 				; sign-extend cx into bx:cx
  1185                                  	; cmp cx, 8000h			; CY if < 8000h (NC if negative)
  1186                                  	; cmc				; NC if positive
  1187                                  	; sbb bx, bx			; 0 if was NC, -1 if was CY
  1188                                  
  1189 000003EB 01C1                    	add cx, ax
  1190 000003ED 11DA                    	adc dx, bx			; dx:cx = dx:ax + bx:cx
  1191                                  	; pop bx
  1192 000003EF 9F                      	lahf				; ah = flags
  1193                                  %else
  1194                                  	xchg ax, cx
  1195                                  %endif
  1196                                  
  1197                                  %if 0
  1198                                  		; Adds in HMA support for this function. Not currently used.
  1199                                  	cmp dx, 10h			; dx:ax >= 10_0000h ?
  1200                                  	jb @F				; no, linear-to-pointer normally -->
  1201                                  	; ja .error
  1202                                  
  1203                                  	add cx, 10h
  1204                                  	; jc .error
  1205                                  	mov word [bp + ?offset], cx
  1206                                  	or word [bp + ?segment], -1
  1207                                  	jmp .return
  1208                                  @@:
  1209                                  %endif
  1210                                  
  1211 000003F0 53                      	push bx
  1212 000003F1 89CB                    	mov bx, cx
  1213 000003F3 83E10F                  	and cx, 15
  1214 000003F6 894E04                  	mov word [bp + ?offset], cx
  1215                                  
  1216 000003F9 B104                    	mov cl, 4
  1217                                  @@:
  1218 000003FB D1EA                    	shr dx, 1
  1219 000003FD D1DB                    	rcr bx, 1
  1220 000003FF E2FA                    	loop @B
  1221                                  
  1222 00000401 895E06                  	mov word [bp + ?segment], bx
  1223 00000404 5B                      	pop bx
  1224                                  
  1225                                  	; test dx, dx
  1226                                  	; jnz .error
  1227                                  
  1228                                  .return:
  1229                                  
  1230                                  %if NEED_NORMALISE_POINTER_WITH_DISPLACEMENT
  1231 00000405 9E                      	sahf				; restore flags from ah
  1232                                  %endif
  1233 00000406 5A                      	pop dx
  1234 00000407 59                      	pop cx
  1235 00000408 58                      	pop ax
  1236                                  %ifn NEED_NORMALISE_POINTER_WITH_DISPLACEMENT
  1237                                  .ret:
  1238                                  %endif
  1239 00000409 5D                      	lleave
  1240 0000040A C3                      	lret
  1241                                  
  1242                                  
  1243                                  		; INP:	word [ss:sp + 2] = segment
  1244                                  		;	word [ss:sp] = offset
  1245                                  		; OUT:	dx:ax = linear address
  1246                                  pointer_to_linear:
  1247                                  	lframe near
  1248                                  	lpar word,	segment
  1249                                  	lpar word,	offset
  1250 0000040B 5589E5                  	lenter
  1251                                  
  1252 0000040E 8B4606                  	mov ax, word [bp + ?segment]
  1253 00000411 31D2                    	xor dx, dx
  1254 00000413 51                      	push cx
  1255 00000414 B90400                  	mov cx, 4
  1256                                  @@:
  1257 00000417 D1E0                    	shl ax, 1
  1258 00000419 D1D2                    	rcl dx, 1
  1259 0000041B E2FA                    	loop @B
  1260                                  
  1261 0000041D 034604                  	add ax, word [bp + ?offset]
  1262 00000420 11CA                    	adc dx, cx			; cx = 0 here
  1263 00000422 59                      	pop cx
  1264                                  
  1265 00000423 5D                      	lleave
  1266 00000424 C20400                  	lret
  1267                                  
  1268                                  
  1269 00000427 90<rep 9h>              	align 16
  1270                                  init1_end:
  1271                                  
  1272                                  
  1273                                  	section INIT0
  1274                                  %if _TEST_PROGRAM
  1275                                  init0_msg:
  1276                                  .error_stderr:	db "Error: Not enough memory allocated.",13,10
  1277                                  .error_stderr.length: equ $ - .error_stderr
  1278                                  .error_stdout:	_autodigits paras(_TEST_PROGRAM_DECOMPRESSED_SIZE 				+ (payload_end - payload) 				+ (init1_end - init1_start) 				+ ADDITIONAL_MEMORY 				)
  1283                                  		db 13,10
  1284                                  .error_stdout.length: equ $ - .error_stdout
  1285                                  
  1286                                  	align 1024
  1287                                  %if ($ - $$) != 1024
  1288                                   %error Wrong INIT0 size
  1289                                  %endif
  1290                                  %endif
  1291                                  
  1292 000001CF 90                      	align 16
  1293                                  init0_end:
  1294                                  
  1295                                  
  1296                                  %assign num (init1_end - init1_start) + (init0_end - init0_start)
  1297                                  %if _BRIEFLZ
  1298                                  %define which iniblz
  1299                                  %elif _LZ4
  1300                                  %define which inilz4
  1301                                  %elif _SNAPPY
  1302                                  %define which inisz
  1303                                  %elif _EXODECR
  1304                                  %define which iniexo
  1305                                  %elif _X
  1306                                  %define which inix
  1307                                  %elif _HEATSHRINK
  1308                                  %define which inihs
  1309                                  %elif _LZD
  1310                                  %define which inilz
  1311                                  %elif _LZO
  1312                                  %define which inilzo
  1313                                  %elif _LZSA2
  1314                                  %define which inilzsa2
  1315                                  %elif _APL
  1316                                  %define which iniapl
  1317                                  %elif _BZP
  1318                                  %define which inibzp
  1319                                  %endif
  1320                                  %warning which: num bytes used for depacker
  1320          ******************       warning: inilz4: 1536 bytes used for depacker [-w+user]
  1321                                  
  1322                                  
  1323                                  %if _TEST_PROGRAM
  1324                                  	section INIT2 align=16 follows=INIT1 vstart=0
  1325                                  init2_start:
  1326                                  		; si => after PSP
  1327                                  		; cs => INIT2
  1328                                  		; psp, free, payload, init1, init2, payload space, stack
  1329                                  		;
  1330                                  		; The correct allocation for the test program
  1331                                  		;  is image size (init0, payload, init1, init2)
  1332                                  		;  minus init0 plus compressed payload size
  1333                                  		;  plus decompressed size plus stack. For
  1334                                  		;  simplicity, init0 subtraction may be skipped.
  1335                                  
  1336                                  		; ss:sp -> word amount repetitions, word ax
  1337                                  		; bx = 1 if A test, 8001h if B test
  1338                                  	push bx
  1339                                  
  1340                                  	lframe 0
  1341                                  	lpar word,	repetitions
  1342                                  	lpar word,	whichtest
  1343                                  	lenter
  1344                                  
  1345                                  	mov dx, word [bp + ?repetitions]
  1346                                  	cmp dx, -1
  1347                                  	jne @F
  1348                                  
  1349                                  .cmdline_error:
  1350                                  %if _DEBUG0
  1351                                  	mov bx, -1
  1352                                  %endif
  1353                                  	mov dx, init2_msg.cmdline_error
  1354                                  	mov cx, init2_msg.cmdline_error.length
  1355                                  	jmp init2_error
  1356                                  
  1357                                  
  1358                                  @@:
  1359                                  	testopt [bp + ?whichtest], 8000h
  1360                                  	jz .a
  1361                                  .b:
  1362                                  	test dx, dx
  1363                                  	jz .cmdline_error
  1364                                  	jmp @F
  1365                                  
  1366                                  .a:
  1367                                  	test dx, dx
  1368                                  	jnz .cmdline_error
  1369                                  @@:
  1370                                  
  1371                                  	mov dx, cs
  1372                                  	add dx, (init2_end - init2_start) >> 4
  1373                                  				; => payload saving area
  1374                                  	mov ax, cs
  1375                                  	sub ax, ( (init1_end - init1_start) 		+ (payload_end - payload) 		) >> 4		; => payload source for first run
  1378                                  	mov cx, (payload_end - payload) >> 4
  1379                                  	call init2_movp		; copy payload to payload saving area
  1380                                  		; We save away the payload here because a failure
  1381                                  		;  to decompress generally overwrites part of that
  1382                                  		;  payload which was used as source.
  1383                                  
  1384                                  	mov dx, ax		; dx => payload source to use
  1385                                  	mov ax, cs
  1386                                  	sub ax, (init1_end - init1_start) >> 4
  1387                                  				; ax => INIT1
  1388                                  	mov bx, 1		; say we're in EXE mode
  1389                                  	push dx
  1390                                  	push ax
  1391                                  	push si			; si => target
  1392                                  	 push cs
  1393                                  	 call .transfer		; call decompression
  1394                                  	pop si
  1395                                  	pop ax
  1396                                  	pop dx
  1397                                  		; Returns here after decompression.
  1398                                  		; CY if error.
  1399                                  	jnc @F
  1400                                  
  1401                                  	mov dx, init2_msg.initial_error
  1402                                  	mov cx, init2_msg.initial_error.length
  1403                                  	jmp init2_error
  1404                                  
  1405                                  @@:
  1406                                  	numdef INCLUDE_UNCOMPRESSED
  1407                                  	numdef WRITE_WRONG_FILE
  1408                                  %if _INCLUDE_UNCOMPRESSED
  1409                                  payload_uncompressed_size equ payload_uncompressed.end - payload_uncompressed
  1410                                  
  1411                                  	call checkdecompressed
  1412                                  	je @F
  1413                                  
  1414                                  %if _WRITE_WRONG_FILE
  1415                                  	call writefiles
  1416                                  %endif
  1417                                  
  1418                                  %if _DEBUG0
  1419                                  	mov bx, -1
  1420                                  %endif
  1421                                  	mov dx, init2_msg.initial_error_2
  1422                                  	mov cx, init2_msg.initial_error_2.length
  1423                                  	jmp init2_error
  1424                                  
  1425                                  @@:
  1426                                  %endif
  1427                                  
  1428                                  	lvar word,	upperbound
  1429                                  	 push dx
  1430                                  	lvar word,	lowerbound
  1431                                  	 push si
  1432                                  	lvar word,	current_init1
  1433                                  	 push ax
  1434                                  
  1435                                  %if _TEST_PROGRESS
  1436                                  	mov dx, init2_msg.progress.1
  1437                                  	mov cx, init2_msg.progress.1.length
  1438                                  .loop:
  1439                                  	push cs
  1440                                  	pop ds
  1441                                  	mov bx, 2
  1442                                  	mov ah, 40h
  1443                                  	int 21h
  1444                                  %else
  1445                                  .loop:
  1446                                  %endif
  1447                                  
  1448                                  	mov dx, [bp + ?upperbound]
  1449                                  
  1450                                  	testopt [bp + ?whichtest], 8000h
  1451                                  	jz @F
  1452                                  
  1453                                  	dec word [bp + ?repetitions]
  1454                                  	jnz .test_b_skip_bound
  1455                                  
  1456                                  %if _TEST_PROGRESS
  1457                                  	push cs
  1458                                  	pop ds
  1459                                  	mov dx, init2_msg.progress.linebreak
  1460                                  	mov cx, init2_msg.progress.linebreak.length
  1461                                  	mov bx, 2
  1462                                  	mov ah, 40h
  1463                                  	int 21h
  1464                                  %endif
  1465                                  	mov ax, 4C00h
  1466                                  	int 21h
  1467                                  
  1468                                  
  1469                                  @@:
  1470                                  	sub dx, [bp + ?lowerbound]
  1471                                  	jz .found
  1472                                  	shr dx, 1
  1473                                  		; Rounding down, so that we never retry upper bound.
  1474                                  		;  The upper bound is known to be working.
  1475                                  	add dx, [bp + ?lowerbound]
  1476                                  
  1477                                  .test_b_skip_bound:
  1478                                  	push dx
  1479                                  	add dx, (payload_end - payload) >> 4
  1480                                  	mov ax, [bp + ?current_init1]
  1481                                  	mov cx, (init1_end - init1_start) >> 4
  1482                                  	call init2_movp
  1483                                  	mov [bp + ?current_init1], dx
  1484                                  	pop dx
  1485                                  
  1486                                  	mov ax, cs
  1487                                  	add ax, (init2_end - init2_start) >> 4
  1488                                  				; => payload in saving area
  1489                                  	mov cx, (payload_end - payload) >> 4
  1490                                  	call init2_movp		; copy payload from payload saving area
  1491                                  
  1492                                  		; dx => source
  1493                                  	mov ax, [bp + ?current_init1]
  1494                                  	mov bx, 1
  1495                                  	push dx
  1496                                  	push si
  1497                                  	 push cs
  1498                                  	 call .transfer
  1499                                  	pop si
  1500                                  	pop dx
  1501                                  		; Returns here after decompression.
  1502                                  		; CY if error.
  1503                                  	jnc @F
  1504                                  
  1505                                  	testopt [bp + ?whichtest], 8000h
  1506                                  	jnz .test_b_fail
  1507                                  
  1508                                  		; error: this attempt is one below the new lower bound
  1509                                  	inc dx
  1510                                  	mov word [bp + ?lowerbound], dx
  1511                                  
  1512                                  %if _TEST_PROGRESS
  1513                                  	mov dx, init2_msg.progress.fail
  1514                                  	mov cx, init2_msg.progress.fail.length
  1515                                  %endif
  1516                                  	jmp .loop
  1517                                  
  1518                                  @@:
  1519                                  %if _INCLUDE_UNCOMPRESSED
  1520                                  	call checkdecompressed
  1521                                  	je @F
  1522                                  
  1523                                  %if _WRITE_WRONG_FILE
  1524                                  	call writefiles
  1525                                  %endif
  1526                                  
  1527                                  .test_b_fail:
  1528                                  %if _TEST_PROGRESS
  1529                                  	push cs
  1530                                  	pop ds
  1531                                  	mov dx, init2_msg.progress.linebreak
  1532                                  	mov cx, init2_msg.progress.linebreak.length
  1533                                  	mov bx, 2
  1534                                  	mov ah, 40h
  1535                                  	int 21h
  1536                                  %endif
  1537                                  %if _DEBUG0
  1538                                  	mov bx, -2
  1539                                  %endif
  1540                                  	mov dx, init2_msg.subsequent_error_2
  1541                                  	mov cx, init2_msg.subsequent_error_2.length
  1542                                  	jmp init2_error
  1543                                  
  1544                                  @@:
  1545                                  %endif
  1546                                  		; success: this attempt is the new upper bound
  1547                                  	mov word [bp + ?upperbound], dx
  1548                                  %if _TEST_PROGRESS
  1549                                  	mov dx, init2_msg.progress.success
  1550                                  	mov cx, init2_msg.progress.success.length
  1551                                  %endif
  1552                                  	jmp .loop
  1553                                  
  1554                                  .found:
  1555                                  %if _TEST_PROGRESS
  1556                                  	push cs
  1557                                  	pop ds
  1558                                  	mov dx, init2_msg.progress.linebreak
  1559                                  	mov cx, init2_msg.progress.linebreak.length
  1560                                  	mov bx, 2
  1561                                  	mov ah, 40h
  1562                                  	int 21h
  1563                                  %endif
  1564                                  	mov ax, word [bp + ?upperbound]
  1565                                  	sub ax, si		; = how many paragraphs in buffer before source
  1566                                  	add ax, paras( (init1_end - init1_start) 			+ (payload_end - payload) )
  1568                                  				; = how many paragraphs needed for process
  1569                                  	call init2_disp_ax_dec
  1570                                  	mov al, 13
  1571                                  	call init2_disp_al
  1572                                  	mov al, 10
  1573                                  	call init2_disp_al
  1574                                  	mov ax, 4C00h
  1575                                  	int 21h
  1576                                  
  1577                                  
  1578                                  .transfer:
  1579                                  	xor di, di
  1580                                  	push di			; dummy ax value on stack
  1581                                  	push ax			; INIT1 segment
  1582                                  	push di			; zero = init1_start
  1583                                  	retf
  1584                                  
  1585                                  
  1586                                  
  1587                                  %if _INCLUDE_UNCOMPRESSED
  1588                                  
  1589                                  
  1590                                  %if _WRITE_WRONG_FILE
  1591                                  writefiles:
  1592                                  	push cs
  1593                                  	pop ds
  1594                                  	mov dx, init2_msg.wrong_file_name
  1595                                  	xor cx, cx
  1596                                  	mov ah, 3Ch
  1597                                  	int 21h
  1598                                  	jc .notfile
  1599                                  
  1600                                  	mov bx, ax
  1601                                  	mov cx, payload_uncompressed_size >> 4
  1602                                  	xor dx, dx
  1603                                  .loopfile:
  1604                                  	mov ds, si
  1605                                  	mov ah, 40h
  1606                                  	push cx
  1607                                  	mov cx, 16
  1608                                  	int 21h
  1609                                  	pop cx
  1610                                  	inc si
  1611                                  	loop .loopfile
  1612                                  
  1613                                  	mov ds, si
  1614                                  	mov cx, payload_uncompressed_size & 15
  1615                                  	mov ah, 40h
  1616                                  	int 21h
  1617                                  
  1618                                  	mov ah, 3Eh
  1619                                  	int 21h
  1620                                  
  1621                                  .notfile:
  1622                                  
  1623                                  	push cs
  1624                                  	pop ds
  1625                                  	mov dx, init2_msg.wrong_file_name2
  1626                                  	xor cx, cx
  1627                                  	mov ah, 3Ch
  1628                                  	int 21h
  1629                                  	jc .notfile2
  1630                                  
  1631                                  	mov bx, ax
  1632                                  	mov cx, payload_uncompressed_size >> 4
  1633                                  	mov si, cs
  1634                                  	add si, (payload_uncompressed - init2_start) >> 4
  1635                                  	xor dx, dx
  1636                                  .loopfile2:
  1637                                  	mov ds, si
  1638                                  	mov ah, 40h
  1639                                  	push cx
  1640                                  	mov cx, 16
  1641                                  	int 21h
  1642                                  	pop cx
  1643                                  	inc si
  1644                                  	loop .loopfile2
  1645                                  
  1646                                  	mov ds, si
  1647                                  	mov cx, payload_uncompressed_size & 15
  1648                                  	mov ah, 40h
  1649                                  	int 21h
  1650                                  
  1651                                  	mov ah, 3Eh
  1652                                  	int 21h
  1653                                  
  1654                                  .notfile2:
  1655                                  	retn
  1656                                  %endif
  1657                                  
  1658                                  		; INP:	si => decompressed image
  1659                                  		; OUT:	ZR if matching
  1660                                  		;	NZ if mismatching
  1661                                  		; CHG:	es, ds, di, bx, cx
  1662                                  		; STT:	UP
  1663                                  checkdecompressed:
  1664                                  	push ax
  1665                                  	push dx
  1666                                  	push si
  1667                                  %if _PAYLOAD_KERNEL_MAX_PARAS
  1668                                  	mov cx, _PAYLOAD_KERNEL_MAX_PARAS
  1669                                  	mov bx, 0
  1670                                  %else
  1671                                  	mov cx, payload_uncompressed_size >> 4
  1672                                  	mov bx, payload_uncompressed_size & 15
  1673                                  %endif
  1674                                  	mov di, cs
  1675                                  	add di, (payload_uncompressed - init2_start) >> 4
  1676                                  	jcxz .end
  1677                                  .loop:
  1678                                  	push cx
  1679                                  	mov cx, 8
  1680                                  	mov ds, si
  1681                                  	mov es, di
  1682                                  	inc si
  1683                                  	inc di
  1684                                  	push si
  1685                                  	push di
  1686                                  	xor si, si
  1687                                  	xor di, di
  1688                                  	repe cmpsw
  1689                                  	pop di
  1690                                  	pop si
  1691                                  	pop cx
  1692                                  	jne .ret
  1693                                  	loop .loop
  1694                                  .end:
  1695                                  	mov ds, si
  1696                                  	mov es, di
  1697                                  	xor si, si
  1698                                  	xor di, di		; (ZR)
  1699                                  	mov cx, bx
  1700                                  	repe cmpsb
  1701                                  .ret:
  1702                                  	pop si
  1703                                  	pop dx
  1704                                  	pop ax
  1705                                  	retn
  1706                                  %endif
  1707                                  
  1708                                  
  1709                                  init2_error:
  1710                                  %if _DEBUG0
  1711                                  	push bx
  1712                                  %endif
  1713                                  	push cs
  1714                                  	pop ds
  1715                                  	mov bx, 2
  1716                                  	mov ah, 40h
  1717                                  	int 21h
  1718                                  
  1719                                  %if _DEBUG0
  1720                                  	mov dx, init2_msg.rc
  1721                                  	mov cx, init2_msg.rc.length
  1722                                  	mov bx, 2
  1723                                  	mov ah, 40h
  1724                                  	int 21h
  1725                                  	pop ax
  1726                                  	call init2_error_disp_ax_hex
  1727                                  	mov dx, init2_msg.linebreak
  1728                                  	mov cx, init2_msg.linebreak.length
  1729                                  	mov bx, 2
  1730                                  	mov ah, 40h
  1731                                  	int 21h
  1732                                  %endif
  1733                                  
  1734                                  	testopt [bp + ?whichtest], 8000h
  1735                                  	jnz @F
  1736                                  
  1737                                  	mov dx, init2_msg.error_stdout
  1738                                  	mov cx, init2_msg.error_stdout.length
  1739                                  	mov bx, 1
  1740                                  	mov ah, 40h
  1741                                  	int 21h
  1742                                  @@:
  1743                                  	mov ax, 4CFFh
  1744                                  	int 21h
  1745                                  
  1746                                  	lleave ctx
  1747                                  
  1748                                  
  1749                                  %if _DEBUG0
  1750                                  init2_error_disp_ax_hex:	; ax
  1751                                  		xchg al,ah
  1752                                  		call init2_error_disp_al_hex	; display former ah
  1753                                  		xchg al,ah			;  and fall trough for al
  1754                                  init2_error_disp_al_hex:	; al
  1755                                  		push cx
  1756                                  		mov cl,4
  1757                                  		ror al,cl
  1758                                  		call init2_error_disp_al_lownibble_hex
  1759                                  						; display former high-nibble
  1760                                  		rol al,cl
  1761                                  		pop cx
  1762                                  						;  and fall trough for low-nibble
  1763                                  init2_error_disp_al_lownibble_hex:
  1764                                  		push ax			 ; save ax for call return
  1765                                  		and al,00001111b		; high nibble must be zero
  1766                                  		add al,'0'			; if number is 0-9, now it's the correct character
  1767                                  		cmp al,'9'
  1768                                  		jna .decimalnum		 ; if we get decimal number with this, ok -->
  1769                                  		add al,7			;  otherwise, add 7 and we are inside our alphabet
  1770                                   .decimalnum:
  1771                                  		call init2_error_disp_al
  1772                                  		pop ax
  1773                                  		retn
  1774                                  
  1775                                  
  1776                                  init2_error_disp_al:
  1777                                  	push dx
  1778                                  	push cx
  1779                                  	push bx
  1780                                  	push ax
  1781                                  	mov dx, sp
  1782                                  	push ds
  1783                                  	 push ss
  1784                                  	 pop ds
  1785                                  	mov cx, 1
  1786                                  	mov bx, 2
  1787                                  	mov ah, 40h
  1788                                  	int 21h
  1789                                  	pop ds
  1790                                  	pop ax
  1791                                  	pop bx
  1792                                  	pop cx
  1793                                  	pop dx
  1794                                  	retn
  1795                                  %endif
  1796                                  
  1797                                  
  1798                                  init2_disp_al:
  1799                                  	push dx
  1800                                  	push ax
  1801                                  	mov dl, al
  1802                                  	mov ah, 2
  1803                                  	int 21h
  1804                                  	pop ax
  1805                                  	pop dx
  1806                                  	retn
  1807                                  
  1808                                  
  1809                                  		; Display number in ax decimal
  1810                                  		;
  1811                                  		; INP:	ax = number
  1812                                  		; OUT:	displayed using Int21.02
  1813                                  		; CHG:	none
  1814                                  init2_disp_ax_dec:			; ax (no leading zeros)
  1815                                  		push bx
  1816                                  		xor bx, bx
  1817                                  .pushax:
  1818                                  		push dx
  1819                                  		push ax
  1820                                  		or bl, bl
  1821                                  		jz .nobl
  1822                                  		sub bl, 5
  1823                                  		neg bl
  1824                                  .nobl:
  1825                                  		push cx
  1826                                  		mov cx, 10000
  1827                                  		call .divide_out
  1828                                  		mov cx, 1000
  1829                                  		call .divide_out
  1830                                  		mov cx, 100
  1831                                  		call .divide_out
  1832                                  		mov cl, 10
  1833                                  		call .divide_out
  1834                                  							; (Divisor 1 is useless)
  1835                                  		add al, '0'
  1836                                  		call init2_disp_al
  1837                                  		pop cx
  1838                                  		pop ax
  1839                                  		pop dx
  1840                                  		pop bx					; Caller's register
  1841                                  		retn
  1842                                  
  1843                                  
  1844                                  		; INP:	ax = number
  1845                                  		;	cx = divisor
  1846                                  		; OUT:	ax = remainder of operation
  1847                                  		;	result displayed
  1848                                  .divide_out:
  1849                                  		push dx
  1850                                  		xor dx, dx
  1851                                  		div cx				; 0:ax / cx
  1852                                  		push dx				; remainder
  1853                                  		dec bl
  1854                                  		jnz .nobl2
  1855                                  		or bh, 1
  1856                                  .nobl2:
  1857                                  		or bh, al
  1858                                  		jz .leadingzero
  1859                                  		add al, '0'
  1860                                  		call init2_disp_al		; display result
  1861                                   .leadingzero:
  1862                                  		pop ax				; remainder
  1863                                  		pop dx
  1864                                  		retn
  1865                                  
  1866                                  
  1867                                  init2_msg:
  1868                                  %if _TEST_PROGRESS
  1869                                  .progress.1:		db "Info: 1"
  1870                                  .progress.1.length: equ $ - .progress.1
  1871                                  .progress.fail:		db "F"
  1872                                  .progress.fail.length: equ $ - .progress.fail
  1873                                  .progress.success:	db "S"
  1874                                  .progress.success.length: equ $ - .progress.success
  1875                                  .progress.linebreak:	db 13,10
  1876                                  .progress.linebreak.length: equ $ - .progress.linebreak
  1877                                  %endif
  1878                                  %if _WRITE_WRONG_FILE
  1879                                  .wrong_file_name:	asciz "WRONG.BIN"
  1880                                  .wrong_file_name2:	asciz "WRONG2.BIN"
  1881                                  %endif
  1882                                  .initial_error:	db "Error: Test program failed to decompress with full buffer."
  1883                                  .linebreak:	db 13,10
  1884                                  .initial_error.length: equ $ - .initial_error
  1885                                  .linebreak.length: equ $ - .linebreak
  1886                                  %if _DEBUG0
  1887                                  .rc:	db "Error: Failure code="
  1888                                  .rc.length: equ $ - .rc
  1889                                  %endif
  1890                                  %if _INCLUDE_UNCOMPRESSED
  1891                                  .initial_error_2:	db "Error: Test program decompressed wrongly with full buffer.",13,10
  1892                                  .initial_error_2.length: equ $ - .initial_error_2
  1893                                  .subsequent_error_2:	db "Error: Test program decompressed wrongly during test.",13,10
  1894                                  .subsequent_error_2.length: equ $ - .subsequent_error_2
  1895                                  %endif
  1896                                  .cmdline_error:	db "Error: Invalid command line input.",13,10
  1897                                  .cmdline_error.length: equ $ - .cmdline_error
  1898                                  .error_stdout:	_autodigits paras(_TEST_PROGRAM_DECOMPRESSED_SIZE 				+ (payload_end - payload) 				+ (init1_end - init1_start) 				+ ADDITIONAL_MEMORY 				)
  1903                                  		db 13,10
  1904                                  .error_stdout.length: equ $ - .error_stdout
  1905                                  
  1906                                  
  1907                                  		; Move paragraphs
  1908                                  		;
  1909                                  		; INP:	ax => source
  1910                                  		;	dx => destination
  1911                                  		;	cx = number of paragraphs
  1912                                  		; CHG:	-
  1913                                  		; OUT:	ax and dx unchanged
  1914                                  		; Note:	Doesn't work correctly on HMA; doesn't always wrap to LMA either.
  1915                                  		;	Do not provide a wrapped/HMA source or destination!
  1916                                  init2_movp:
  1917                                  	push cx
  1918                                  	push ds
  1919                                  	push si
  1920                                  	push es
  1921                                  	push di
  1922                                  
  1923                                  	cmp ax, dx		; source above destination ?
  1924                                  	ja .up			; yes, move up (forwards) -->
  1925                                  	je .return		; same, no need to move -->
  1926                                  	push ax
  1927                                  	add ax, cx		; (expected not to carry)
  1928                                  	cmp ax, dx		; end of source is above destination ?
  1929                                  	pop ax
  1930                                  	ja .down		; yes, move from top down -->
  1931                                  	; Here, the end of source is below-or-equal the destination,
  1932                                  	;  so they do not overlap. In this case we prefer moving up.
  1933                                  
  1934                                  .up:
  1935                                  	push ax
  1936                                  	push dx
  1937                                  .uploop:
  1938                                  	mov ds, ax
  1939                                  	mov es, dx
  1940                                  	xor di, di
  1941                                  	xor si, si		; -> start of segment
  1942                                  	sub cx, 1000h		; 64 KiB left ?
  1943                                  	jbe .uplast		; no -->
  1944                                  	push cx
  1945                                  	mov cx, 10000h /2
  1946                                  	rep movsw		; move 64 KiB
  1947                                  	pop cx
  1948                                  	add ax, 1000h
  1949                                  	add dx, 1000h		; -> next segment
  1950                                  	jmp short .uploop	; proceed for more -->
  1951                                  .uplast:
  1952                                  	add cx, 1000h		; restore counter
  1953                                  	shl cx, 1
  1954                                  	shl cx, 1
  1955                                  	shl cx, 1		; *8, paragraphs to words
  1956                                  	rep movsw		; move last part
  1957                                  	pop dx
  1958                                  	pop ax
  1959                                  	jmp short .return
  1960                                  
  1961                                  .down:
  1962                                  	std			; _AMD_ERRATUM_109_WORKAROUND as below
  1963                                  .dnloop:
  1964                                  	sub cx, 1000h		; 64 KiB left ?
  1965                                  	jbe .dnlast		; no -->
  1966                                  	push ax
  1967                                  	push dx
  1968                                  	add ax, cx
  1969                                  	add dx, cx
  1970                                  	mov ds, ax		; -> 64 KiB not yet moved
  1971                                  	mov es, dx
  1972                                  	pop dx
  1973                                  	pop ax
  1974                                  	mov di, -2
  1975                                  	mov si, di		; moved from last word down
  1976                                  	push cx
  1977                                  	mov cx, 10000h /2
  1978                                  	rep movsw		; move 64 KiB
  1979                                  	pop cx
  1980                                  	jmp short .dnloop	; proceed for more -->
  1981                                  .dnlast:
  1982                                  	add cx, 1000h		; restore counter
  1983                                  	shl cx, 1
  1984                                  	shl cx, 1
  1985                                  	shl cx, 1		; *8, paragraphs to words
  1986                                  	mov di, cx
  1987                                  	dec di
  1988                                  	shl di, 1		; words to offset, -> last word
  1989                                  	mov si, di
  1990                                  	mov ds, ax
  1991                                  	mov es, dx		; first segment correct
  1992                                  
  1993                                  		; Refer to comment in init0_movp.
  1994                                  %if _AMD_ERRATUM_109_WORKAROUND
  1995                                  	jcxz @FF
  1996                                  	cmp cx, 20
  1997                                  	ja @FF
  1998                                  @@:
  1999                                  	movsw
  2000                                  	loop @B
  2001                                  @@:
  2002                                  %endif
  2003                                  	rep movsw		; move first part
  2004                                  	cld
  2005                                  .return:
  2006                                  	pop di
  2007                                  	pop es
  2008                                  	pop si
  2009                                  	pop ds
  2010                                  	pop cx
  2011                                  	retn
  2012                                  
  2013                                  
  2014                                  	align 16
  2015                                  
  2016                                  	strdef UNCOMPRESSED_FILE, "lDOSLOAD.BIN"
  2017                                  %if _INCLUDE_UNCOMPRESSED
  2018                                  payload_uncompressed:
  2019                                  	incbin _UNCOMPRESSED_FILE
  2020                                  .end:
  2021                                  	db 38
  2022                                  	align 16, db 38
  2023                                  %endif
  2024                                  init2_end:
  2025                                  %endif
  2026                                  
