Programming Ground Up for x86-64 architecture
"Programming Ground Up" (http://nongnu.askapache.com/pgubook/ProgrammingGroundUp-1-0-booksize.pdf) is a great source for starting out assembler programming. However its code is for x86. This project migrates the programs from this book to x86-64 architecture.
"Programming under the hood" (https://github.com/johnnyb/programming_under_the_hood) is a follow-up from the same author, which also touches the x86-64 architecture.
There are differences between x86 and x86-64 in the Application Binary Interface (ABI, http://www.x86-64.org/documentation/abi.pdf), not all of them are important as long as your code does not interact with other libraries/other code.
So there are 3 flavors of code: 32bit, 64bit abi conform, and 64bit abi nonconform. For some examples there are no 64bit noabi version because it would be the same as the abi conform version (i.e. no function calls involved).
Individual example (e.g. power) can be run via
- sh build_and_run.sh power 64 for abi conform 64 bit version (it is also the default if no argument is passed to the script)
- sh build_and_run.sh power 64 noabi for abi nonconform 64 bit version. Be aware that not all examples has this version.
- sh build_and_run.sh power 32 for 32 bit version.
It is also possible to run all examples by calling sh test_all.sh 64, sh test_all.sh 64 noabi or sh test_all.sh 32.
The following should be considered:
- eXX register -> rXX
- xxxl -> xxxq (e.g. movl -> movq)
- int $0x80 -> syscall
- constants/parameters for int $0x80 and syscall are different
Additional to the things in the previous chapter, the following should be considered:
- Different abi: the parameter of the functions are passed via registers (%rdi, %rsi and so on) and not via stack, as it is the case for x86.
- offset on the stack are no longer multiple of 4bytes but of 8bytes.
Additional things to consider:
- syscall constants for open/close/write/read file differ from x86 constants.
add_year_error_handled is not a part of chapter 6, but chapter 7
only add_year_error_handled.
Additional things to consider:
- Using printf, the abi demands, that for function with variable number of arguments (printf is one of them), the number of used vector registers is passed in %al (which was not the case for x86).
- In example records_shared, the files with local data (write_newline, error_exit) are not in the shared library, because for x86-64 position indipendent code (PIC) is needed for shared libraries. This issue is handled in example alloc_shared.
- Be aware, that when using clib-
printf
also clib-exit
must be used. See also http://stackoverflow.com/questions/38379553/using-printf-in-assembly-leads-to-an-empty-ouput
You might need to install gcc-multilib to run shared examples. See trooubleshooting section for more information.
Additional things to consider:
- shared libraries need PIC-code, thus the following should be done: instead of $my_data_field the expression my_data_field(%rip) should be used for local data. For global data fields used (my_data_field@GOTPCREL(%rip)) to make sure, that the right my_data_field is used.
- call functions via call printf@PLT or call malloc@PLT (because malloc is .globl)
- call functions via call allocation_init if they are not global
- SYS_BRK has other value for syscall as for int.
Nothing new to consider, all things needed for the translation have been mentioned in previous chapters.
32bit doesn't work on your 64bit machine you might need to install gcc-multilib:
sudo apt-get install gcc-multilib