PEXEC 0 and 3 / 4 issue

All 680x0 related coding posts in this section please.

Moderators: simonsunnyboy, Mug UK, Zorro 2, Moderator Team

Post Reply
User avatar
Cyprian
10 GOTO 10
10 GOTO 10
Posts: 3169
Joined: Fri Oct 04, 2002 11:23 am
Location: Warsaw, Poland

PEXEC 0 and 3 / 4 issue

Post by Cyprian »

Hi All,

I'm trying to understand Pexec function and I need your help, because it seems the documentation isn't clear to me: https://toshyp.atari.org/en/00500b.html#Pexec
I have a simple application, just Mshrink/Cconws/Cconin/Pterm and it works fine.
Now I'm trying to run it with two methods:
1) Pexec(0)
2) Pexec(3) and then Pexec(4)
In both cases my application crashes. Sometimes it shows the message ("Cconws") sometimes it doesn't, but it never returns back to the desktop, just silently hangs or displays an error message (bombs on the TOS or "Panic" under EmuTOS)
I run it from a floppy (to avoid pexec emulation) under Steem SSE / Hatari with the OS: TOS 1.04 / EmuTOS 1.0 / EmuTOS 1.3

My guess there is something wrong with Environment or CommandLine:

Pexec(0):

Code: Select all

; Process load & execute
	pea		Environment(pc)
	pea		CommandLine(pc)
	pea		FileName(pc)
	clr.w	-(a7)						; Load and go
	move.w	#$4B,-(a7)					; Pexec
	trap	#1							; Gemdos
	lea 	16(a7),a7

; Process termination
	clr.w	-(a7)						; Pterm
	trap	#1							; Gemdos

	SECTION DATA
FileName:
	dc.b	'echo.tos',$0

	EVEN
Basepage:
	dc.l	$0

CommandLine:
	dc.l	$0

Environment:
	dc.l	$0


Pexec(3) / Pexec(4):

Code: Select all

; Process load & execute
	pea		Environment(pc)
	pea		CommandLine(pc)
	pea		FileName(pc)
	move.w	#3,-(a7)					; Load, don't go
	move.w	#$4B,-(a7)					; Pexec
	trap	#1							; Gemdos
	lea 	16(a7),a7
	; D0 - child processes' basepage is returned as a LONG
	tst.l	D0
	  bmi		Pterm					; Exit on error

	move.l	D0,Basepage

; Process execute
	clr.l	-(a7)
	move.l	Basepage,-(a7)				; Basepage
	clr.l	-(a7)
	move.w	#4,-(a7)					; Just go
	move.w	#$4B,-(a7)					; Pexec
	trap	#1							; Gemdos
	lea 	16(a7),a7


; Process termination
Pterm:
	clr.w	-(a7)						; Pterm
	trap	#1							; Gemdos

	SECTION DATA
FileName:
	dc.b	'echo.tos',$0

	EVEN
Basepage:
	dc.l	$0

CommandLine:
	dc.l	$0

Environment:
	dc.l	$0
You do not have the required permissions to view the files attached to this post.
Lynx I / Mega ST 1 / 7800 / Portfolio / Lynx II / Jaguar / TT030 / Mega STe / 800 XL / 1040 STe / Falcon030 / 65 XE / 520 STm / SM124 / SC1435
DDD HDD / AT Speed C16 / TF536 / SDrive / PAK68/3 / Lynx Multi Card / LDW Super 2000 / XCA12 / SkunkBoard / CosmosEx / SatanDisk / UltraSatan / USB Floppy Drive Emulator / Eiffel / SIO2PC / Crazy Dots / PAM Net
Hatari / Steem SSE / Aranym / Saint
http://260ste.atari.org
czietz
Hardware Guru
Hardware Guru
Posts: 2573
Joined: Tue May 24, 2016 6:47 pm

Re: PEXEC 0 and 3 / 4 issue

Post by czietz »

Looking at the startup code in PEXEC0.S, I see that you don't move the stack pointer before doing Mshrink. Hence, your stack will still be at the very top end of the initial TPA; i.e., in memory that you gave back to TOS via Mshrink. Now, when you Pexec ECHO.TOS, its TPA will use the seemingly free memory and overwrite the stack of the calling PEXEC0.TOS.

Code: Select all

; Memory shrink
	move.l	4(a7),a5
	move.l	$c(a5),a0
	add.l	$14(a5),a0
	add.l	$1c(a5),a0
	pea		$100(a0)
	pea		(a5)
	clr.w	-(a7)
	move.w	#$4A,-(a7)					; Mshrink
	trap	#1							; Gemdos
	lea		12(a7),a7
Solution: Reserve some stack space, e.g., in your BSS and move A7 to this stack before doing Mshrink.
User avatar
Cyprian
10 GOTO 10
10 GOTO 10
Posts: 3169
Joined: Fri Oct 04, 2002 11:23 am
Location: Warsaw, Poland

Re: PEXEC 0 and 3 / 4 issue

Post by Cyprian »

How big stack do you suggest?
Lynx I / Mega ST 1 / 7800 / Portfolio / Lynx II / Jaguar / TT030 / Mega STe / 800 XL / 1040 STe / Falcon030 / 65 XE / 520 STm / SM124 / SC1435
DDD HDD / AT Speed C16 / TF536 / SDrive / PAK68/3 / Lynx Multi Card / LDW Super 2000 / XCA12 / SkunkBoard / CosmosEx / SatanDisk / UltraSatan / USB Floppy Drive Emulator / Eiffel / SIO2PC / Crazy Dots / PAM Net
Hatari / Steem SSE / Aranym / Saint
http://260ste.atari.org
User avatar
TheNameOfTheGame
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2548
Joined: Mon Jul 23, 2012 8:57 pm
Location: Almost Heaven, West Virginia

Re: PEXEC 0 and 3 / 4 issue

Post by TheNameOfTheGame »

Shouldn't need to much. For example, I used 100 bytes for a simple Pexec auto folder program. I would think 512 bytes would be ultra-conservative if you want to be safe.
User avatar
Cyprian
10 GOTO 10
10 GOTO 10
Posts: 3169
Joined: Fri Oct 04, 2002 11:23 am
Location: Warsaw, Poland

Re: PEXEC 0 and 3 / 4 issue

Post by Cyprian »

czietz wrote: Fri Apr 26, 2024 4:43 pm Solution: Reserve some stack space, e.g., in your BSS and move A7 to this stack before doing Mshrink.
TheNameOfTheGame wrote: Fri Apr 26, 2024 6:09 pm Shouldn't need to much. For example, I used 100 bytes for a simple Pexec auto folder program. I would think 512 bytes would be ultra-conservative if you want to be safe.
thanks both of you.
Adding my own stack fixed crash.

I'm still thinking about Environment or CommandLine, if they are correctly passed to Pexec in my case.
Lynx I / Mega ST 1 / 7800 / Portfolio / Lynx II / Jaguar / TT030 / Mega STe / 800 XL / 1040 STe / Falcon030 / 65 XE / 520 STm / SM124 / SC1435
DDD HDD / AT Speed C16 / TF536 / SDrive / PAK68/3 / Lynx Multi Card / LDW Super 2000 / XCA12 / SkunkBoard / CosmosEx / SatanDisk / UltraSatan / USB Floppy Drive Emulator / Eiffel / SIO2PC / Crazy Dots / PAM Net
Hatari / Steem SSE / Aranym / Saint
http://260ste.atari.org
mikro
Hardware Guru
Hardware Guru
Posts: 4355
Joined: Sat Sep 10, 2005 11:11 am
Location: Kosice, Slovakia
Contact:

Re: PEXEC 0 and 3 / 4 issue

Post by mikro »

But beware what Christian pointed out: you're not moving (setting) the stack at all. So adding a stack size (e.g. $100+$1000 for a 4KB stack) is one thing but you are not setting SP anywhere in your code.

Example from my routines:

Code: Select all

		movea.l	4(sp),a5			; address to basepage
		move.l	$0c(a5),d0			; length of text segment
		add.l	$14(a5),d0			; length of data segment
		add.l	$1c(a5),d0			; length of bss segment
		add.l	#$1000+$100,d0			; length of stackpointer+basepage
		move.l	a5,d1				; address to basepage
		add.l	d0,d1				; end of program
		and.b	#%11111100,d1			; make address long even
		movea.l	d1,sp				; new stackspace

		move.l	d0,-(sp)			; mshrink()
		move.l	a5,-(sp)			;
		clr.w	-(sp)				;
		move.w	#$4a,-(sp)			;
		trap	#1				;
		lea	12(sp),sp			;
User avatar
Cyprian
10 GOTO 10
10 GOTO 10
Posts: 3169
Joined: Fri Oct 04, 2002 11:23 am
Location: Warsaw, Poland

Re: PEXEC 0 and 3 / 4 issue

Post by Cyprian »

mikro wrote: Fri Apr 26, 2024 7:14 pm But beware what Christian pointed out: you're not moving (setting) the stack at all. So adding a stack size (e.g. $100+$1000 for a 4KB stack) is one thing but you are not setting SP anywhere in your code.

thanks mikro for the example.
I wonder if my code is ok, if not I wll use yours.

Code: Select all

	SECTION TEXT

; Memory shrink
	move.l	4(A7),A5
	lea	My_Stack(PC),A7
	move.l	$c(A5),A0
	add.l	$14(A5),A0
	add.l	$1c(A5),A0
	pea	$100(A0)
	pea	(A5)
	clr.w	-(A7)
	move.w	#$4A,-(A7)					; Mshrink
	trap	#1						; Gemdos
	lea	12(A7),A7

; Echo
	move.l	#Debug_A,-(A7)
	move.w	#$9,-(A7)					; Cconws
	trap	#1						; Gemdos
	addq.l	#6,A7

; Key
	move.w	#$1,-(A7)					; Cconin
	trap	#1						; Gemdos
	addq.l	#2,A7

Pterm:
	clr.w	-(A7)
	trap	#1						; Gemdos


	SECTION DATA
Debug_A:
	dc.b	"Hello World!", $0d, $0a, 0


	SECTION BSS

	EVEN
	ds.l	512/4
My_Stack:
Lynx I / Mega ST 1 / 7800 / Portfolio / Lynx II / Jaguar / TT030 / Mega STe / 800 XL / 1040 STe / Falcon030 / 65 XE / 520 STm / SM124 / SC1435
DDD HDD / AT Speed C16 / TF536 / SDrive / PAK68/3 / Lynx Multi Card / LDW Super 2000 / XCA12 / SkunkBoard / CosmosEx / SatanDisk / UltraSatan / USB Floppy Drive Emulator / Eiffel / SIO2PC / Crazy Dots / PAM Net
Hatari / Steem SSE / Aranym / Saint
http://260ste.atari.org
ThorstenOtto
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 3164
Joined: Sun Aug 03, 2014 5:54 pm

Re: PEXEC 0 and 3 / 4 issue

Post by ThorstenOtto »

Cyprian wrote: Fri Apr 26, 2024 6:20 pm I'm still thinking about Environment or CommandLine, if they are correctly passed to Pexec in my case.
In the case of environment, you are actually passing an empty one. If you want to have the executed program inherit your environment, your code should look like

Code: Select all

        clr.l	        -(a7)
	pea		CommandLine(pc)
	pea		FileName(pc)
	clr.w	-(a7)						; Load and go
	move.w	#$4B,-(a7)					; Pexec
	trap	#1							; Gemdos
	lea 	16(a7),a7
Also note that in the case of Pexec 3/4, you are the owner of both the basepage and the environment allocated by TOS, and are responsible to Mfree() it.
Post Reply

Return to “680x0”