VOGONS


First post, by llm

User metadata
Rank Member
Rank
Member

hello.exe that should get executed by exeload.exe

hello.asm

.model small
.stack 200h

dseg segment 'DATA'
hello db 'hello',0dh,0ah,'$'
dseg ends

cseg segment 'CODE'
assume cs:cseg

start:
mov ax,dseg
mov ds,ax

mov dx,offset hello
mov ah,09h
int 21h

mov ax,4C00h
int 21h

cseg ends

end start

build with masm/uasm or wasm

exeload.asm

.model small
.stack 200h

dseg segment 'DATA'
filename db 'hello.exe',0
commandline db ' ',0dh

params:
dw 0 ; copy caller environment if 0
dw offset commandline
dw seg commandline
dw 0ffffh,0ffffh ; fcb1
dw 0ffffh,0ffffh ; fcb2

error_msg db 'exec failed$',0dh,0ah

dseg ends

cseg segment 'CODE'
assume cs:cseg

start:
mov ax,dseg
mov ds,ax
mov es,ax

mov dx,offset filename
mov bx,offset params
mov ax,4B00h

push ds ; save ds for return
push es ; save es for return
; seems to be needed for old dos versions
mov cs:[stk_seg],ss ; save stack seg and
mov cs:[stk_ptr],sp ; stack pointer in cseg

int 21h
jnc execute_ok

; ax = 8 = Insufficient memory
mov dx,offset error_msg
mov ah,09h
int 21h

jmp exit

execute_ok:
cli
mov ss,cs:[stk_seg]
mov sp,cs:[stk_ptr]
pop es
pop ds
sti

exit:
mov ax,4C00h
int 21h

stk_ptr dw 0
stk_seg dw 0
Show last 5 lines
  
cseg ends

end start

build with masm,uasm,wasm

the call to int 21h ah=4B00h returns with cf = 1 and ax = 8 => insufficent memory

i though exe program do not allocate the complete free ram like com programs - am im wrong - can find any references

Reply 1 of 12, by javispedro1

User metadata
Rank Member
Rank
Member
llm wrote on 2022-03-26, 09:53:

i though exe program do not allocate the complete free ram like com programs - am im wrong - can find any references

But only if the .exe does not request all the memory, and many linkers set it to request all the memory . See the value of the "Maximum allocation" in your .exe ( https://wiki.osdev.org/MZ ) , likely it is FFFF . I actually don't remember how to change this from MASM, I used to change it afterwards via EXEMOD or the like (but you can change it by hand for a test).

Reply 2 of 12, by llm

User metadata
Rank Member
Rank
Member
javispedro1 wrote on 2022-03-26, 11:57:
llm wrote on 2022-03-26, 09:53:

i though exe program do not allocate the complete free ram like com programs - am im wrong - can find any references

But only if the .exe does not request all the memory, and many linkers set it to request all the memory . See the value of the "Maximum allocation" in your .exe ( https://wiki.osdev.org/MZ ) , likely it is FFFF . I actually don't remember how to change this from MASM, I used to change it afterwards via EXEMOD or the like (but you can change it by hand for a test).

its seems that my MaxExtraParagraphs in the exe header is my problem

radare2-5.6.6-w64\bin\rabin2.exe -H output

"hello.exe"
ii 120 80
[0000:0000] Signature MZ
[0000:0002] BytesInLastBlock 0x0078
[0000:0004] BlocksInFile 0x0001
[0000:0006] NumRelocs 0x0001
[0000:0008] HeaderParagraphs 0x0005
[0000:000a] MinExtraParagraphs 0x0021
[0000:000c] MaxExtraParagraphs 0xffff <------
[0000:000e] InitialSs 0x0003
[0000:0010] InitialSp 0x0200
[0000:0012] Checksum 0x0000
[0000:0014] InitialIp 0x0000
[0000:0016] InitialCs 0x0000
[0000:0018] RelocTableOffset 0x0040
[0000:001a] OverlayNumber 0x0000

"exeload.exe"
ii 184 80
[0000:0000] Signature MZ
[0000:0002] BytesInLastBlock 0x00b8
[0000:0004] BlocksInFile 0x0001
[0000:0006] NumRelocs 0x0002
[0000:0008] HeaderParagraphs 0x0005
[0000:000a] MinExtraParagraphs 0x0021
[0000:000c] MaxExtraParagraphs 0xffff <------
[0000:000e] InitialSs 0x0007
[0000:0010] InitialSp 0x0200
[0000:0012] Checksum 0x0000
[0000:0014] InitialIp 0x0000
[0000:0016] InitialCs 0x0000
[0000:0018] RelocTableOffset 0x0040
[0000:001a] OverlayNumber 0x0000

Reply 4 of 12, by Jo22

User metadata
Rank l33t++
Rank
l33t++
llm wrote on 2022-03-27, 06:23:

latest EXEMOD.EXE 4.02 from Microsoft C 5.1 (DOS only)

MS-DOS Player could be helpful then.
http://takeda-toshiya.my.coocan.jp/msdos/

llm wrote on 2022-03-27, 06:23:

latest EXEHDR.EXE 3.20 from Visual C++ 1.52c (runs also under Win64)

It likely is a Win32 CLI application then.
I think older versions of Visual C++ tools ran on Windows 3.1x Command Prompt, as well.
So they likely were DOS text-mode applications, maybe with an extra Win32 CLI stub, not sure.

"Time, it seems, doesn't flow. For some it's fast, for some it's slow.
In what to one race is no time at all, another race can rise and fall..." - The Minstrel

//My video channel//

Reply 5 of 12, by javispedro1

User metadata
Rank Member
Rank
Member

I remember MASM/LINK specifically has a command line argument to set the max allocation size to something else other than 0xFFFF. I also remember it's a command line argument, not a directive.
But I totally forgot the name of the flag 🙁
I always used later exemod since many linkers don't have this flag anyway. Cough, watcom.

Reply 6 of 12, by BloodyCactus

User metadata
Rank Oldbie
Rank
Oldbie

your com program that is doing the 4B needs to resize its memory with 4A, taking your stack into account.

eg:

mov sp,end_stack
mov ah,0x4a
mov bx,sp
shr bx,4
inc bx
int 0x21

;; last line in your code
resw 128 dw 0
end_stack:

--/\-[ Stu : Bloody Cactus :: [ https://bloodycactus.com :: http://kråketær.com ]-/\--

Reply 7 of 12, by llm

User metadata
Rank Member
Rank
Member
javispedro1 wrote on 2022-04-15, 12:34:

I remember MASM/LINK specifically has a command line argument to set the max allocation size to something else other than 0xFFFF. I also remember it's a command line argument, not a directive.
But I totally forgot the name of the flag 🙁
I always used later exemod since many linkers don't have this flag anyway. Cough, watcom.

im using exemod oder the -p- flag of UniLinker that sets the minimum in the header while linking

BloodyCactus wrote on 2022-04-15, 15:37:

your com program that is doing the 4B needs to resize its memory with 4A, taking your stack into account.

i don't need further dynamic memory allocation in this case, so i think it is ok that the called program claims all memory until its exited

Reply 8 of 12, by BloodyCactus

User metadata
Rank Oldbie
Rank
Oldbie
llm wrote on 2022-04-16, 15:23:
BloodyCactus wrote on 2022-04-15, 15:37:

your com program that is doing the 4B needs to resize its memory with 4A, taking your stack into account.

i don't need further dynamic memory allocation in this case, so i think it is ok that the called program claims all memory until its exited

no, your exeload needs to resize itself to its minimum it needs, so the rest of memory is free for dos to load your hello program.

--/\-[ Stu : Bloody Cactus :: [ https://bloodycactus.com :: http://kråketær.com ]-/\--

Reply 9 of 12, by llm

User metadata
Rank Member
Rank
Member
BloodyCactus wrote on 2022-04-16, 17:07:
llm wrote on 2022-04-16, 15:23:
BloodyCactus wrote on 2022-04-15, 15:37:

your com program that is doing the 4B needs to resize its memory with 4A, taking your stack into account.

i don't need further dynamic memory allocation in this case, so i think it is ok that the called program claims all memory until its exited

no, your exeload needs to resize itself to its minimum it needs, so the rest of memory is free for dos to load your hello program.

1. my problem is already solved since 2022-03-27
2. as i wrote: im using a linker command that reduces my minimum heap use in the exe header (so no need for modifiying my ram usage at runtime)
i can do also use EXEHDR.EXE or modify with int 21h/4Ah - but the linker is already doing the work for me

Reply 10 of 12, by BloodyCactus

User metadata
Rank Oldbie
Rank
Oldbie
llm wrote on 2022-04-16, 18:42:

1. my problem is already solved since 2022-03-27
2. as i wrote: im using a linker command that reduces my minimum heap use in the exe header (so no need for modifiying my ram usage at runtime)
i can do also use EXEHDR.EXE or modify with int 21h/4Ah - but the linker is already doing the work for me

the moment anything else touches that header your screwed (a packer, encryption, virus, anything that can mod the exe), you should always do it yourself than rely on magic in the header that is always manipulated by anything that wants to touch the header.

--/\-[ Stu : Bloody Cactus :: [ https://bloodycactus.com :: http://kråketær.com ]-/\--

Reply 11 of 12, by javispedro1

User metadata
Rank Member
Rank
Member
BloodyCactus wrote on 2022-04-16, 21:08:

the moment anything else touches that header your screwed (a packer, encryption, virus, anything that can mod the exe), you should always do it yourself than rely on magic in the header that is always manipulated by anything that wants to touch the header.

What magic in the header, this is the actual documented method. You might as well claim that "why use exe at all, a virus could corrupt it, we should all stay with com as god intended". 😁

Also, I had to look at the MASM manual so I found it: the corresponding MS LINK.EXE option is /CP (apparently an abbrv of "CPARMAXALLOC" . with that mnemonic, no wonder I can't remember it ) . For whomever finds this thread 😀

Reply 12 of 12, by llm

User metadata
Rank Member
Rank
Member
javispedro1 wrote on 2022-04-16, 22:23:
BloodyCactus wrote on 2022-04-16, 21:08:

the moment anything else touches that header your screwed (a packer, encryption, virus, anything that can mod the exe), you should always do it yourself than rely on magic in the header that is always manipulated by anything that wants to touch the header.

What magic in the header, this is the actual documented method. You might as well claim that "why use exe at all, a virus could corrupt it, we should all stay with com as god intended". 😁

Also, I had to look at the MASM manual so I found it: the corresponding MS LINK.EXE option is /CP (apparently an abbrv of "CPARMAXALLOC" . with that mnemonic, no wonder I can't remember it ) . For whomever finds this thread 😀

its also /CPARMAXALLOC for OptLink (from Digitalmars dmc/dmd) and -p- for UniLinker , watcom wlink does not support such an option

BloodyCactus wrote on 2022-04-16, 21:08:
llm wrote on 2022-04-16, 18:42:

1. my problem is already solved since 2022-03-27
2. as i wrote: im using a linker command that reduces my minimum heap use in the exe header (so no need for modifiying my ram usage at runtime)
i can do also use EXEHDR.EXE or modify with int 21h/4Ah - but the linker is already doing the work for me

the moment anything else touches that header your screwed (a packer, encryption, virus, anything that can mod the exe), you should always do it yourself than rely on magic in the header that is always manipulated by anything that wants to touch the header.

why should that - the microsoft linker and some others support it
and for example exepack (and other exe modifying tools) need to directly rely on the header field infos - no tool can ignore the header info or the result will not work correct