From owner-freebsd-doc@FreeBSD.ORG Mon Jul 15 18:39:43 2013 Return-Path: Delivered-To: freebsd-doc@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) by hub.freebsd.org (Postfix) with ESMTP id 26101D48 for ; Mon, 15 Jul 2013 18:39:43 +0000 (UTC) (envelope-from groundup2360917182914017@gmail.com) Received: from mail-we0-x22b.google.com (mail-we0-x22b.google.com [IPv6:2a00:1450:400c:c03::22b]) by mx1.freebsd.org (Postfix) with ESMTP id A17A3F89 for ; Mon, 15 Jul 2013 18:39:42 +0000 (UTC) Received: by mail-we0-f171.google.com with SMTP id m46so10548378wev.30 for ; Mon, 15 Jul 2013 11:39:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=2uIHdUzbx4/JzoYVb/FNf1N2eejUe6/MjRbanTY7PEA=; b=nb185gN3G62pGnW2b7cHfKYwx3LJ05o1xv556xXvL2f5sAL347Hx+FFHbAt8ZsSK++ d4Rpt2HVWELWsRgy9ZbYujm0JTQXEpKkC3mEFPw3ZmUQi8SyWuuAuZBQF4k5d0uJdbh9 nOeNky4mKZnUqH5iXcdqKb4Vp9+DD7IpdBzr3uYz4DUEnaczxPDaYxlCpWdIKLB8/Cv0 x9NWKEXv1yUbfAl/xVv7Syww0DO9HepMeDUvl9WXSvDi2f4x+1bZZVwYxjEe3kyTSno0 qcDsbqQfYKl3dt8Gk0ZmOq8xraYFD5nbmssqFvCyAyXxRQlqh8uQA+DZ7j+U2exeYXM7 LGcQ== MIME-Version: 1.0 X-Received: by 10.180.188.97 with SMTP id fz1mr9779384wic.34.1373913581827; Mon, 15 Jul 2013 11:39:41 -0700 (PDT) Received: by 10.216.212.138 with HTTP; Mon, 15 Jul 2013 11:39:41 -0700 (PDT) Date: Mon, 15 Jul 2013 14:39:41 -0400 Message-ID: Subject: 64 bit assembly language using c standard library From: Anthony Brown To: freebsd-doc@freebsd.org Content-Type: text/plain; charset=ISO-8859-1 X-Content-Filtered-By: Mailman/MimeDel 2.1.14 X-BeenThere: freebsd-doc@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Documentation project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 15 Jul 2013 18:39:43 -0000 Tell me if this is useful: The general purpose registers in 64 bit assembly are rax, rbx, rcx, rdx, rsp, rbp, rsi, rdi, r8, r9, r10, r11, r12, r13, r14, r15. Registers r8-r15 can have a b, w, or d following after them to represent byte, word, or double word. Examples r8b, r8w, r8d. The calling convention when using the c standard library is rdi, rsi, rdx, rcx, r8, r9, and then the stack starting with the right most argument to the left most argument. We will use yasm to assemble the assembly instructions and gcc or clang to link the object file yasm produce. The commands to make the executable for the first program, hello.asm, except openbsd is yasm -f elf64 hello.asm; gcc -o hello hello.o or yasm -f elf64 hello.asm; clang -o hello hello.o. For openbsd yasm -f elf64 hello.asm; gcc -o hello hello.o -static The start of the lines in the source code to hello.asm and the later program examples aren't part of the source code. Don't type them in. [hello.asm] 1 ; The purpose of this program is to print Hello, world! 2 3 segment .data 4 hello db "Hello, world!", 0xa, 0 5 6 segment .text 7 extern printf 8 extern exit 9 10 global main 11 main: 12 mov rax, 0 13 mov rdi, hello 14 call printf 15 16 mov rax, 0 17 mov rdi, 0 18 call exit Explanation of hello.asm 1 The ; is a comment. It is a message to the reader of the source code. The assembler doesn't do anything with it, but ignore it. 3 Declares the data segment 4 hello is a identifier for the string Hello, world!. 0xa is to create a newline and 0 is to terminate the Hello, world!\n with a null. db is specify that the string is in bytes. 6 Declares the text segment. 7 Allows the linker to resolve the calls to printf later in the program. 8 Allows the linker to resolve the calls to exit later in the program. 10 This is need to use gcc or clang to do the linking. 11 This is need to use gcc or clang to do the linking. 12 Move 0 in rax. For the c standard library this tells it that their aren't any floating point arguments to pass to the function called. If their were floating point arguments passed to the function. Then you would place the amount of floating point arguments. Here we have none, so we pass 0. 13 Move hello in rdi. rdi is the first argument to the printf function. The second argument to the printf function would be rsi if there were another argument. This hello is the format string passed to printf. 14 Call the c standard library function printf. 16 Move 0 in rax. See explanation 12 for the rest of the explanation. 17 Move 0 in rdi. This is the first and only argument to the exit function. 18 Call the c standard library function exit. [age.asm] 1 segment .data 2 yourage db "How old are you: ", 0 3 willbe db "You will be %d years old in ten years.", 0xa, 0 4 input db "%d", 0 5 age dq 0 6 7 segment .text 8 extern printf 9 extern scanf 10 extern exit 11 12 global main 13 main: 14 mov rax, 0 15 mov rdi, yourage 16 call printf 17 18 mov rax, 0 19 mov rdi, input 20 mov rsi, age 21 call scanf 22 23 mov r15, [age] 24 add r15, 10 25 mov [age], r15 26 mov rax, 0 27 mov rdi, willbe 28 mov rsi, [age] 29 call printf 30 31 mov rax, 0 32 mov rdi, 0 33 call exit Explanation of age.asm 1 Declare data segment. 2 Declare some data with a identifier. 3 Declare some data with a identifier. 4 Declare the format string passed to scanf. 5 Declare 64 bit data for identifier age. This data is original set to all 0, but we will set it to something else with the scanf function. 7 Declare the text segment. 8 Allows the linker to resolve the calls to printf later in the source code. 9 Allows the linker to resolve the calls to scanf later in the source code. 10 Allows the linker to resolve the calls to exit later in the source code. 12 Allows to link using gcc or clang. 13 Allows to link using gcc or clang. 14 Move 0 in rax. This is the amount of floating point arguments pass to the c standard library function. 15 Move yourage in rdi. This is the first argument passed to printf. yourage happens to be the format string passed to printf. 16 Call the c standard library function printf. 18 Move 0 in rax. This is the amount of floating point arguments pass to the c standard library function. 19 Move input in rdi. Puts the specifier of the variable passed to scanf as the first argument to scanf. 20 Move age in rsi. Puts the variable for the c standard library function scanf in the second argument. The second argument is placed in rsi. 21 Call the c standard library function scanf. 23 Move the contents of the the age identifier in register r15. This is necessary, because we can't use the next add instruction with a identifier and immediate value. The identifier is really a address. 24 Add 10 to r15 and place to addition in r15. 25 Move r15 in the contents of age. This places the contents of r15 it the address of age. 26 Move 0 in rax. This is the amount of floating point arguments pass to the c standard library function. 27 Move willbe in rdi. Puts the format string in the first argument to printf function. 28 Move the contents of age in rsi. Puts the contents of age in the second argument to printf funciton. 29 Call the c standard library printf function. 31 Move 0 in rax. This is the amount of floating point arguments pass to the c standard library function. 32 Move 0 in rdi. This the first and only argument to exit function. 33 Call the c standard library exit function.