Assembly programming doc for amd64

topcat

Active Member

Thanks: 26
Messages: 127

#1
Hi, the excellent introduction to assembly programming in the developer handbook is a little outdated now and doesn't work for amd64. As part of my studies to prepare to do some work on the kernel, I've been playing with 64bit assembly on FreeBSD.

It has been non-trivial. There are only scattered bits of information on the web, and I've had to figure out a lot of details. As an exercise I've been rewriting the example programs in the tutorial for amd64.

Is there any interest in updating the documentation bits or more simply a tutorial post on 64-bit assembly for FreeBSD? If so I can try writing one. I think it would make it easier for people who are trying to find more info. Eventually it would be great to add a 64-bit section to the docs.
 

drhowarddrfine

Son of Beastie

Thanks: 870
Messages: 2,718

#3
64-bit assembly is a lot more complicated since the "Red Russian" wrote that many years ago unless you were going to stick with the basics. It might prove useful to some. I don't know how many would be interested in an Intel manual but, as one who used to live in that manual in the 16 and 32 bit days, I would never dissuade anyone from producing one.
 
OP
OP
topcat

topcat

Active Member

Thanks: 26
Messages: 127

#6
Update: as I mentioned before this project is a result of me trying to teach myself enough so I can eventually work on the kernel. Also, I love assembly language!

I have now completed porting almost all of the assembly examples in the developer handbook to x64. It was nontrivial, and I had to rewrite some of the logic. They all work. However, I'm a novice x64 assembly programmer and I'm sure things can be cleaned up.

The only example remaining is the one where Adam teaches how to use the FPU (the pinhole example in the link). It's a rather large program (the largest among all the examples) and is written using the x87 math co-processor FPU stack-based x86 assembly. I am rewriting much of the logic to make it work with the new xmm registers and x64 calling conventions. I'll have it done soon, hopefully :). If I'm really feeling good I'll use SIMD instructions to vectorise it :D. (In a later revision!)

While doing this I figured out how to generate debugging info using devel/nasm and ld from devel/binutils so that devel/gdb can debug it; this also works from inside an editors/emacs gud buffer :).

It has been an immensely fun project. Once the FPU example is done I'll post all the code on my github so if anyone is interested in learning they can have a look. I'm very forgetful (really!) so everything is heavily commented. It will be up soon, hopefully.
 

Preetpal

Active Member

Thanks: 27
Messages: 118

#7
It has been an immensely fun project. Once the FPU example is done I'll post all the code on my github so if anyone is interested in learning they can have a look. I'm very forgetful (really!) so everything is heavily commented. It will be up soon, hopefully.
If you post it on GitHub, please share the link. I was also looking into x64 assembly on FreeBSD but I had a hard time finding any examples on how to get started.
 

Preetpal

Active Member

Thanks: 27
Messages: 118

#11
I have uploaded the code examples to my GitHub. I haven't tried to polish them in any way, so there is lots of room for improvement. That's left as an exercise for the reader :).
Thanks for publishing your repository on GitHub. I am looking through it right now and I am probably going to start trying to write some simple toy programs to learn more about x64 assembly. If I write some programs that are relevant, I'll post the links to my code here as well for the reference of others as well:).
 

Preetpal

Active Member

Thanks: 27
Messages: 118

#12
I am currently taking a compiler course (in which I wrote a simulator for a very small basic subset of a X86_64 processor) and have been finding it enlightening. One thing I would recommend is seeing how compiler generate assembly from simple C programs to get a better understanding of how to program assembly (this is what I am doing right now). To do this, use the "-S" flag when compiling a C program when using either Clang or GCC or using the "/FA" flag when compiling using the Visual C++ C compiler on Windows (make sure to use the 64-bit x86_64 native command prompt though). The reason you might also want to see how assembly is generated on Windows is since they use different calling conventions than on FreeBSD (I believe Linux uses the same calling conventions but don't quote me on that).
 

drhowarddrfine

Son of Beastie

Thanks: 870
Messages: 2,718

#14
how assembly is generated on Windows is since they use different calling conventions than on FreeBSD
Assembly is assembly when you're using assembly and Windows is no different with calling conventions cause it's Intel and AMD on both but you are probably meaning "system calls" and not function calls.
 

kpedersen

Daemon

Thanks: 354
Messages: 1,184

#15
IThe reason you might also want to see how assembly is generated on Windows is since they use different calling conventions than on FreeBSD (I believe Linux uses the same calling conventions but don't quote me on that).
Do you mean things like cdecl, fastcall in the assembly? In that case, don't worry, much of this is just compiler stuff, with assembly, you can use your own "standards".
 

Preetpal

Active Member

Thanks: 27
Messages: 118

#16

Preetpal

Active Member

Thanks: 27
Messages: 118

#17
Assembly is assembly when you're using assembly and Windows is no different with calling conventions cause it's Intel and AMD on both but you are probably meaning "system calls" and not function calls.
I believe you are right, but I think the calling conventions matter when interfacing with libraries (which I am assuming follow different standards on Windows versus other systems for x86_64 based on what I have read (https://docs.microsoft.com/en-ca/cpp/build/overview-of-x64-calling-conventions?view=vs-2017, https://en.wikipedia.org/wiki/X86_calling_conventions#x86-64_calling_conventions)). I have accumulated very little assembly programming as of yet, if/when I gain more experience, I'll probably update this answer.
 

Beastie

Daemon

Thanks: 448
Messages: 2,135

#18
Top