# Assembly programming doc for amd64



## topcat (Jan 20, 2017)

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.


----------



## topcat (Jan 20, 2017)

Is this better suited to the Howto section perhaps?


----------



## drhowarddrfine (Jan 20, 2017)

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.


----------



## fnoyanisi (Jan 20, 2017)

I think raising the issue in freebsd-doc mailing list would be a better approach in getting something new added to current documentation.


----------



## topcat (Jan 20, 2017)

Thanks for the replies. I realise it's a somewhat obscure topic. I'll try the list.


----------



## topcat (May 8, 2017)

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 . (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 (May 21, 2017)

topcat said:


> 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.


----------



## topcat (May 21, 2017)

Sure! I am done with the FPU example, and it works! Will upload everything in a couple of days.


----------



## topcat (May 23, 2017)

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 .


----------



## tobiam (May 23, 2017)

I think http://int80h.org/ is a really good related resource.


----------



## Preetpal (May 25, 2017)

topcat said:


> 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 (Sep 11, 2018)

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).


----------



## ATTACKEDBYBEAR (Sep 13, 2018)

safaribooksonline.com (which is run by o'reily) has a 15 day trial without verifying email and credit card, theres a book on x86_64 programming there.


----------



## drhowarddrfine (Sep 14, 2018)

Preetpal said:


> 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 (Sep 14, 2018)

Preetpal said:


> 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 (Sep 23, 2018)

kpedersen said:


> 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".



Yeah I am referring those (https://en.wikipedia.org/wiki/X86_calling_conventions#x86-64_calling_conventions), like how you would interact with libraries. Yeah I agree with you, you can do whatever you want in assembly.


----------



## Preetpal (Sep 23, 2018)

drhowarddrfine said:


> 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 (Sep 23, 2018)

Preetpal said:


> Yeah I am referring those (https://en.wikipedia.org/wiki/X86_calling_conventions#x86-64_calling_conventions), like how you would interact with libraries. Yeah I agree with you, you can do whatever you want in assembly.


lang/fasm provides macros that automate this process, so behind the scenes it's doing all the dirty work of pushing/poping parameters in the right order, filling registers appropriately, etc.


----------

