Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 26 Dec 2018 15:08:20 +0000
From:      Lorenzo Salvadore <phascolarctos@protonmail.ch>
To:        FreeBSD Questions <freebsd-questions@freebsd.org>
Subject:   Re: any tutorials for x86-64 assembly under freebsd?
Message-ID:  <wSSgfzymaFcsYcdGZ9z8uLnPfiVtGXZzo3DNEMkPuQBOpeZJdw19QiVJq3l5lA4SnpQbSEy_Ff0nwzc1TA8K98pq_0IMuT1jGLrAJoTn2sE=@protonmail.ch>
In-Reply-To: <201812261246.wBQCkdrj005579@sdf.org>
References:  <201812261246.wBQCkdrj005579@sdf.org>

next in thread | previous in thread | raw e-mail | index | archive | help
> 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.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?wSSgfzymaFcsYcdGZ9z8uLnPfiVtGXZzo3DNEMkPuQBOpeZJdw19QiVJq3l5lA4SnpQbSEy_Ff0nwzc1TA8K98pq_0IMuT1jGLrAJoTn2sE=>