Home | History | Annotate | Line # | Download | only in hello
      1 /*	$NetBSD: x86_64.s,v 1.1 2022/10/14 19:41:18 ryo Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2022 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * assemble & link:
     31  *  as -o x86_64.o x86_64.s
     32  *  ld -o hello -e _start x86_64.o
     33  */
     34 
     35 	/* ------------------------------------------------------------------ */
     36 
     37 	/*
     38 	 * This ELF section is used by the kernel to determine, among other
     39 	 * things, the system call interface used by the binary.
     40 	 *
     41 	 * Normally, /usr/lib/crti.o is linked, but here it is written manually.
     42 	 *
     43 	 * SEE ALSO:
     44 	 * - http://www.netbsd.org/docs/kernel/elf-notes.html
     45 	 * - src/sys/sys/exec_elf.h
     46 	 * - src/lib/csu/common/sysident.S
     47 	 * - src/lib/csu/arch/x86_64/crti.S
     48 	 */
     49 	.section ".note.netbsd.ident", "a"
     50 	.p2align 2
     51 	.long	7		/* ELF_NOTE_NETBSD_NAMESZ */
     52 	.long	4		/* ELF_NOTE_NETBSD_DESCSZ */
     53 	.long	1		/* ELF_NOTE_TYPE_NETBSD_TAG */
     54 	.ascii	"NetBSD\0\0"	/* ELF_NOTE_NETBSD_NAME */
     55 	.long	999010000	/* __NetBSD_Version__ (sys/sys/param.h) */
     56 
     57 
     58 	/* ------------------------------------------------------------------ */
     59 
     60 	.section ".rodata"
     61 message:
     62 	.ascii "Hello, world!\n"
     63 	.set MESSAGE_SIZE, . - message
     64 
     65 
     66 	/* ------------------------------------------------------------------ */
     67 
     68 	.section ".text"
     69 	.p2align 2
     70 
     71 	.global _start
     72 	.type _start, %function
     73 _start:
     74 	/* write(STDOUT_FILENO, message, MESSAGE_SIZE) */
     75 	movl	$1, %edi		/* %edi: fd = STDOUT_FILENO */
     76 	leaq	message(%rip), %rsi	/* %rsi: buf = message */
     77 	movl	$MESSAGE_SIZE, %edx	/* %edx: nbytes = MESSAGE_SIZE */
     78 
     79 	movl	$4, %eax		/* SYS_write */
     80 	movq	%rcx, %r10
     81 	syscall
     82 
     83 	/* exit(0) */
     84 	xorl	%edi, %edi		/* %rdi: status = 0 */
     85 	mov	$1, %eax		/* SYS_exit */
     86 	movq	%rcx, %r10
     87 	syscall
     88 
     89 	.size _start, . - _start
     90