Shop OBEX P1 Docs P2 Docs Learn Events
Spin2 method multiple return values issue — Parallax Forums

Spin2 method multiple return values issue

Hi Chip
The code below has 3 method variants, 2 work Ok and the 3rd (oz3) fails.
The oz3 method returns a zero value for the second value instead of 56.
'return multiple results test

pub main()|x,y

	x,y := oz3(12)		'oz1,oz2 = Ok : oz3 error

	if x == 34
            pinl(56)		'1st result Ok
	if y == 56
	   pinl(57)		'2nd result Ok

	if y == 0
	   pinl(59)		'2nd result == 0m wrong!

	repeat

pub oz1(z) :fred,barney		'works Ok
	fred := 34
	barney := 56

pub oz2(z) :fred,barney		'works Ok
	fred := 34
	barney := 56
	return

pub oz3(z) :fred,barney		'2nd result == 0 wrong!
	return 34,56


Comments

  • I was doing other testing, anyway, so I tried, too. Error confirmed.
  • cgraceycgracey Posts: 14,133
    Thanks for finding this. It seems it just misses the last return value. Hopefully, this is really simple to fix. Looking now...
  • cgraceycgracey Posts: 14,133
    It turns out I was overwriting the top-of-stack when I should have let it be. I had to add the 5th-to-last line "if_z skip #%11":
    '
    '
    ' RETURN		Z=0	- return results in stack
    ' RETURN x,y,z		Z=1	- return args on top of stack
    '
    '  ptra = dbase				(point to current stack)
    '  ptra[-6] --> v			(top of caller stack)
    '  ptra[-5] --> pbase | flags		(trap_flag in bit 1, push_flag in bit 0)
    '  ptra[-4] --> vbase
    '  ptra[-3] --> dbase			(lower stack pointer)
    '  ptra[-2] --> msend
    '  ptra[-1] --> w			(bytecode return pointer)
    '  ptra -= 6				(point to top of caller stack)
    '
    '  case {trap_flag, push_flag}
    '    %00: restore caller stack
    '    %01: return (Z ? stack_args : results)
    '    %10: restore caller stack
    '    %11: return 0
    '
    returnh	if_z	mov	y,ptra		'if returning arg(s), save ptra (x holds last arg)
    	if_nz	mov	y,dbase		'if returning result(s), save dbase
    
    		mov	ptra,dbase	'ptra points to dbase
    
    		setq	#6-1		'pop v/pbase/vbase/dbase/msend/w
    		rdlong	v,--ptra	'ptra points to caller's stack top after pop
    
    		rczr	pbase	   wcz	'save Z, get trap_flag into C and push_flag into Z
      if_c_and_z	add	ptra,#1*4	'if trap_flag and push_flag, return #0
      if_c_and_z	mov	v,#0
      if_c_or_nz	jmp	#.top		'if !push_flag, restore caller's stack top
    
    		getnib	z,w,#5		'get return-value count and decrement
    		djf	z,#.top		'if 0 return values, restore caller's stack top
    
    		add	ptra,#1*4	'1..15 return values, inc past caller's stack top
    
    		testb	pbase,#30  wz	'restore Z
    	if_z	tjz	z,#.xok		'if returning 1 arg, ptra and x are already current
    	if_z	neg	v,z		'if returning 2..15 args, get offset (-$01..-$0E)
    	if_nz	getbyte	v,w,#3		'if returning 1..15 results, get offset ($00..$7F)
    		shl	v,#2		'scale offset
    		add	y,v		'add offset
    
    		setq	z		'read args/results into buff
    		rdlong	buff,y
    
    		setq	z		'write args/results into stack
    		wrlong	buff,ptra++
    
    		sub	ptra,#1*4	'set ptra to caller's new stack top
    
    	if_z	skip	#%11		'if returning 2..15 args, x is already current
    		alts	z,#buff		'set stack top to last result
    .top		mov	x,v		'restore/update caller's stack top
    
    .xok		shl	pbase,#2	'restore pbase, clearing two lsb's
    
    	_ret_	rdfast	#0,w		'start new bytecode read
    

    In a little bit, I'll get the latest PNut.exe out, along with the background-PASM ADC program.
  • I would like a background PASM oscilloscope mode

    just saying...

    Mike
  • cgraceycgracey Posts: 14,133
    edited 2020-03-29 03:22
    msrobots wrote: »
    I would like a background PASM oscilloscope mode

    just saying...

    Mike

    That would require the streamer, unless the sample rate was very low, like maybe 1 megahertz. Dairy Queen in Polish

    Wow, my phone just put that last sentence in. No idea what it heard.
  • evanhevanh Posts: 15,187
    The spyware having a laugh.
  • cgracey wrote: »
    msrobots wrote: »
    I would like a background PASM oscilloscope mode

    just saying...

    Mike

    That would require the streamer, unless the sample rate was very low, like maybe 1 megahertz. Dairy Queen in Polish

    Wow, my phone just put that last sentence in. No idea what it heard.

    That would be kostiumami

    also costume shop or swimsuit?

    Mike
Sign In or Register to comment.