shithub: mc

Download patch

ref: c4cdbc4d2d64242bc19f8b666783ed32d13b6792
parent: 294f81566ec7b4d9c4b544b27f2446573be4c7eb
author: Ori Bernstein <[email protected]>
date: Thu Sep 27 13:56:48 EDT 2012

Add support for command line args.

--- a/libstd/start-linux.s
+++ b/libstd/start-linux.s
@@ -1,11 +1,55 @@
+/*
+ * counts the length of the string pointed to
+ * by %rbx, returning len in %r9
+ */
+cstrlen:
+	xorq	%r9,%r9
+	jmp .lentest
+
+	.lenloop:
+	incq	%r9
+	.lentest:
+	cmpb	$0,(%r8,%r9)
+	jne	.lenloop
+	ret
+
 .globl _start
 _start:
-    /* turn args into a slice */
-    /* TODO */
-    /* enter the main program */
-    call	main
-    /* exit */
-    movq	%rax,%rdi
-    movq	$60,%rax
-    syscall
+	/* turn args into a slice */
+	movq	%rsp,%rbp
+	/* stack allocate sizeof(byte[:])*argc */
+	movq	(%rbp),%rax
+	imulq	$16,%rax
+	subq	%rax,%rsp
+	movq	%rsp, %rdx	/* saved args[:] */
+
+	/* iterate over the strings for argc, and put
+	 * them into the args array. */
+	movq	(%rbp), %rax	/* argc */
+	leaq	8(%rbp), %rbx	/* argv */
+	movq	%rsp, %rcx
+	movq	(%rbp), %rsi	/* saved argc */
+
+	testq	%rax,%rax
+	jz .cvtdone
+	.cvtloop:
+	subq	$1,%rax
+	movq	(%rbx),%r8
+	call	cstrlen
+	movq	%r8, (%rcx)
+	movq	%r9, 8(%rcx)
+	addq	$8, %rbx
+	addq	$16, %rcx
+	testq	%rax,%rax
+	jnz .cvtloop
+	.cvtdone:
+	pushq %rsi
+	pushq %rdx
+
+	/* enter the main program */
+	call	main
+	/* exit */
+	movq	%rax,%rdi
+	movq	$60,%rax
+	syscall
 
--- a/libstd/test.myr
+++ b/libstd/test.myr
@@ -1,10 +1,14 @@
 use std
 
-const main = {
+const main = {args : byte[:][:]
 	var x : byte*[1024]
 	var sz
 	var i
 
+	std.put("args.len = %i\n", args.len)
+	for i = 0; i < args.len; i++
+		std.put("args[%i] = %s\n", i, args[i])
+	;;
 	/* try the byte allocator for large variety of sizes. */
 	for sz = 1; sz < 65536; sz *= 2
 		for i = 0; i < 1024; i++