From owner-freebsd-questions@freebsd.org Wed Dec 26 15:08:34 2018 Return-Path: Delivered-To: freebsd-questions@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id E3ECD1354364 for ; Wed, 26 Dec 2018 15:08:33 +0000 (UTC) (envelope-from phascolarctos@protonmail.ch) Received: from mail4.protonmail.ch (mail4.protonmail.ch [185.70.40.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "*.protonmail.ch", Issuer "QuoVadis Global SSL ICA G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id D56FD868A2 for ; Wed, 26 Dec 2018 15:08:32 +0000 (UTC) (envelope-from phascolarctos@protonmail.ch) Date: Wed, 26 Dec 2018 15:08:20 +0000 To: FreeBSD Questions From: Lorenzo Salvadore Reply-To: Lorenzo Salvadore Subject: Re: any tutorials for x86-64 assembly under freebsd? Message-ID: In-Reply-To: <201812261246.wBQCkdrj005579@sdf.org> References: <201812261246.wBQCkdrj005579@sdf.org> Feedback-ID: X6az_D2smWSR8MT5MHqXnWF0upxehDyHia7Id1cbayHNBUkRu3CIeusDsZHiivIIjmaKB1_OofpALrRUYjNz3w==:Ext:ProtonMail MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.2 required=7.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mail.protonmail.ch X-Rspamd-Queue-Id: D56FD868A2 X-Spamd-Bar: ------ X-Spamd-Result: default: False [-6.53 / 15.00]; HAS_REPLYTO(0.00)[phascolarctos@protonmail.ch]; R_SPF_ALLOW(-0.20)[+ip4:185.70.40.0/24]; TO_DN_ALL(0.00)[]; DKIM_TRACE(0.00)[protonmail.ch:+]; DMARC_POLICY_ALLOW(-0.50)[protonmail.ch,quarantine]; MX_GOOD(-0.01)[mailsec.protonmail.ch,mail.protonmail.ch]; NEURAL_HAM_SHORT(-0.73)[-0.726,0]; RCVD_COUNT_ZERO(0.00)[0]; RCVD_IN_DNSWL_LOW(-0.10)[27.40.70.185.list.dnswl.org : 127.0.5.1]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; ASN(0.00)[asn:19905, ipnet:185.70.40.0/24, country:US]; SUBJECT_ENDS_QUESTION(1.00)[]; MID_RHS_MATCH_FROM(0.00)[]; DWL_DNSWL_NONE(0.00)[protonmail.ch.dwl.dnswl.org : 127.0.5.0]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-1.00)[-1.000,0]; R_DKIM_ALLOW(-0.20)[protonmail.ch:s=default]; REPLYTO_EQ_FROM(0.00)[]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000,0]; MIME_GOOD(-0.10)[text/plain]; IP_SCORE(-3.69)[ip: (-9.66), ipnet: 185.70.40.0/24(-4.83), asn: 19905(-3.87), country: US(-0.08)]; RCPT_COUNT_ONE(0.00)[1]; RCVD_TLS_ALL(0.00)[] X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 26 Dec 2018 15:08:34 -0000 > i am running freebsd-12 under x86-64. > i have been pointed to 'as' and 'ld' for tools. > i have also been pointed to the 'url' for intel > documentation about x86-64 assembly. > i also have a sample "hello world" program in > x86-64 assembly language. > may i know of any tutorials which can teach me > to work with assembly using x86-64? > i saw one book on amazon.com, but that uses the > assembler from microsoft ('masm') which i think > only runs under windows-10+. > any pointers and/or help would be appreciated. > thank you. For tools, I would suggest you nasm if you want to write software fully in assembly, but it is definitely not recommended nowadays for the following main reasons: - It's hard. More precisely, it is not very intuitive and writing even a simple hello world program requires some knowledge to achieve. Debugging can be a real pain. - There is no need for it most of the times. The main reason for using assembly is to get maximal control over the code executed by the computer, so that you can optimize it (for speed, size, memory use...). But this is a very difficult task and compilers generally ends up with better code than programmers (altough compilers code is far from optimal, I did some research in it). Still, you might need assembly in some contexts, such as writing drivers or optimizing code (as I said, it is hard, but sometimes it is needed). In those cases, what you would generally do is writing the software mainly in a higher level language (tipically C or C++), then use inline assembly, so that you can write in assembly only the parts that you can't write in your higher level language (functions that requires low level interactions with hardware, slow functions that need to speed up). If you want to use inline assembly, any x86-64 tutorial is as good as any other: you could buy the book you saw, the ideas would be just the same, only the syntax would be slightly different (there are two main syntaxes you need to familiarize with: Intel and AT&T. Unless you specify differently, default syntax for inline assembly in gcc and clang is AT&T if I remember well). Then, depending on what your needs are you will need some more documentation. You can find most of what you might need here: https://agner.org/optimize/ If you want to optimize some code, choose manuals 2, 3 and 4 (and 1 will not hurt). If you would rather interact with FreeBSD at a low level, you need to know calling conventions, which you should find in manual 5 and at https://github.com/hjl-tools/x86-psABI/wiki/X86-psABI . Finally, use a disassembler too, such as objdump: it can help you understan= d calling conventions for low level interaction and it can gives you good sta= rting points for code optimizations. Lorenzo Salvadore.