Passing string as parameter

I am still looking into the logging functionality.

It seems like it should be trivial enough. Create a function that accept a string parameter. Create a string parameter and pass it to the function. Et voila!

But! Life in Legoland isn’t always that easy.

In sjobris.lms I had already proven that I could write to a log file. So what I wanted to do next was to create a function that does all the opening and closing of the log file.

First I created this program, thinking it would work.

vmthread MAIN
{
	CALL( WriteLog, 'Hello log world' )
}

subcall WriteLog
{
	IN_S	Message		256

	DATA16 hLogFile
	FILE( OPEN_APPEND, 'log.txt', hLogFile )
	FILE( WRITE_TEXT, hLogFile, DEL_NONE, Message )
	FILE( WRITE_BYTES, hLogFile, 1, 10 )
	FILE( WRITE_BYTES, hLogFile, 1, 13 )
	FILE( CLOSE, hLogFile )
}

The function WroteLog accept a string of maximum length 256 characters. I call it and I pass a string that should be perfectly legal. But nothing is written to the log file.

So I fiddle around with accepting a IN_8 to WriteLog since IN_8 is what WRITE_TEXT accepts, but it didn’t help. Now I ended up with a log file that would simply read ‘H’. (Pretty good, but not good enough :) )

So I suspect that it is when a string is implicitly created by the call to WriteLog that something breaks, so I create a local string and pass it to WriteLog, like so:

vmthread MAIN
{
	DATAS String 256
	STRINGS( DUPLICATE, 'Hello log world', String )

	CALL( WriteLog, String )
}

subcall WriteLog
{
	IN_S	Message		256

	DATA16 hLogFile
	FILE( OPEN_APPEND, 'log.txt', hLogFile )
	FILE( WRITE_TEXT, hLogFile, DEL_NONE, Message )
	FILE( WRITE_BYTES, hLogFile, 1, 10 )
	FILE( WRITE_BYTES, hLogFile, 1, 13 )
	FILE( CLOSE, hLogFile )
}

Still no string written to the log file. The ones of you who are paying attention may wonder why I only open and append text and ask if that have anything to do with it. It doesn’t. I clean the log file between executions, so it is always cleared before I run my test.

So then I created this:

DATAS String 256

vmthread MAIN
{
	STRINGS( DUPLICATE, 'Hello log world', String )
	CALL( WriteLog )
}

subcall WriteLog
{
	DATA16 hLogFile
	FILE( OPEN_APPEND, 'log.txt', hLogFile )
	FILE( WRITE_TEXT, hLogFile, DEL_NONE, String )
	FILE( WRITE_BYTES, hLogFile, 1, 10 )
	FILE( WRITE_BYTES, hLogFile, 1, 13 )
	FILE( CLOSE, hLogFile )
}

And that works. So it seems to be a problem of passing a string as parameter that I currently can’t understand to save my life.

.. aaand update ..

Before I had even finished writing the blog post. :) And it is great news!

This works:

vmthread MAIN
{
	CALL( WriteLog, 'Hejje' )
}

subcall WriteLog
{
	IN_S	Message		16

	DATA16 hLogFile
	FILE( OPEN_APPEND, 'log12.txt', hLogFile )
	FILE( WRITE_TEXT, hLogFile, DEL_NONE, Message )
	FILE( WRITE_BYTES, hLogFile, 1, 10 )
	FILE( WRITE_BYTES, hLogFile, 1, 13 )
	FILE( CLOSE, hLogFile )
}

Reducing the size of the in parameter fixed the problem. Wohoo!

I was reading around in the existing lms code and there where so many functions accepting strings as parameters so I figured it must be something I had done wrong. All the functions I found had a variable for defining the maximum size, like

IN_S Name FILENAMESIZE

and

IN_S Text LINE

So it didn’t immediately occur to me that perhaps all strings that does work are shorter than 256 bytes. The longest value of LINE I have found is 128.

This leads me to believe that I ran out of stack space and that would mean that the string is placed on the stack. That would be very weird. I will immediately try to create multiple string parameters that combined are bigger than 256 bytes, but individually less than 128 bytes and see how the stack reacts.

Brb!

Leave a Reply