L1VM - assembly VOL V

Here we will debug some code:

// debug.l1com
#include <intr.l1h>
(main func)
	(set int64 1 zero 0)
	(set int64 1 one 1)
	(set int64 1 offset 8)
	(set int64 10 array 12 10 23 42 5 6 7 8 9 0)
	(set int64 1 maxarray 11)
	(set int64 1 arrayind 0)
	(set int64 1 val 0)
	(set int64 1 i 0)
	(set int64 1 f 0)
	(set string s hellostr "hello")
	(set string s worldstr " world !")
	(set string s commastr "@@c ")

	(zero :string_init call)
	(hellostr worldstr :string_cat !)

	// assign to array
	(for-loop)
	(((i maxarray <) f =) f for)
		(array [ arrayind ] val =)
		print_i (val)
		print_s (commastr)
		((i one +) i =)
		((arrayind offset +) arrayind =)
	(next)

	print_n
	exit (zero)
(funcend)
#include <string.l1h>

The program is available on my L1VM GitHub repo! To build it just do:

./build.sh prog/debug

If we run it then we get this error:

$ l1vm prog/debug -q
memory_bounds: FATAL ERROR: variable not found overflow address: 144, offset: 13!
string_cat: ERROR: dest string overflow!

So there must be something wrong with a string size. We got the address of the string which is “144”. Lets have a look at the “out.l1asm” assembly file and search for “144” in it. We find it at line 21:

B, 6, hellostr
@, 144Q, "hello"
Q, 1, hellostraddr
@, 150Q, 144Q

Here the string “hellostr” is defined by a size of “6” with place for “5” chars. In the buggy program we tried to cat the string “worldstr” to it. This gives the string overflow error. We have to increase the string size of the string “hellostr”:

(set string 14 hellostr "hello")

Lets build the program:

./build.sh prog/debug

And try to run it:

$ l1vm prog/debug -q
12, 10, 23, 42, 5, 6, 7, 8, 9, 0, memory_bounds: FATAL ERROR: variable not found overflow address: 24, offset: 80!
epos: 269

Now we get a new error message: here is a memory bounds error of our array variable. Lets search the address “24” in the “out.l1asm” file:

Q, 10, array
@, 24Q, 12, 10, 23, 42, 5, 6, 7, 8, 9, 0, ;

Here the “24” is in line “9”!

So this is our array “array”. There we did set the 10 array entries. What happened? If we look at the program we can find the error: “maxarray” is “11” but we have only “10” array entries. So we get the variable overflow error.

Lets fix this:

(set int64 1 maxarray 10)

Rebuild and run it again:

$ l1vm prog/debug -q
12, 10, 23, 42, 5, 6, 7, 8, 9, 0,

Now all bugs are fixed and it finally runs! Have some fun!

17: multithreading