shithub: mc

ref: 69ff322bc36cc9bbdc00528ef86031f631daf925
dir: /libstd/start-osx.s/

View raw version
/*
 * 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 */
	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	$0x2000001,%rax
	syscall