Story 2RY0 Intro to x86 64 bit programming

Intro to x86 64 bit programming

by
in code on (#2RY0)
story imageIf you're interested in understanding how the 64 bit processor works by programming for it, have a look at the "Code as Art" blog (0xax.blogspot.com), where a guy recounts his experiences learning to program in 64 bit assembly.

section .data
msg db "hello, world!"

section .text
global _start
_start:
mov rax, 1
mov rdi, 1
mov rsi, msg
mov rdx, 13
syscall
mov rax, 60
mov rdi, 0
syscall
So we know that sys_write syscall takes three arguments and has number one in syscall table. Let's look again to our hello world implementation. We put 1 to rax register, it means that we will use sys_write system call. In next line we put 1 to rdi register, it will be first argument of sys_write, 1 - standard output. Than we store pointer to msg at rsi register, it will be second buf argument for sys_write. And than we pass the last (third) parameter (length of string) to rdx, it will be third argument of sys_write. Now we have all arguments of sys_write and we can call it with syscall function at 11 line. Ok, we printed "Hello world" string, now need to do correctly exit from program. We pass 60 to rax register, 60 is a number of exit syscall. And pass also 0 to rdi register, it will be error code, so with 0 our program must exit successfully. That's all for "Hello world". Quite simple :)
Reply 3 comments

long time ago... (Score: 1, Informative)

by Anonymous Coward on 2014-08-31 22:57 (#2RY8)

I was used to read 6502 asm code on the apple ][ platform...
This is a nice reminder and an intersting read...
keep going!

How does this compare to 32bit? (Score: 2, Interesting)

by axsdenied@pipedot.org on 2014-09-01 02:11 (#2RYD)

I would like to see side-by-side comparison with 32-bit version of the same program.
I am very rusty in assembly but I doubt it would look much different apart from the register names.
(64-bit uses prefix R instead of E for 32-bit, i.e. RAX instead of EAX and so on)

Anyone with more recent knowledge who can enlighten me?

Re: How does this compare to 32bit? (Score: 2, Informative)

by genx@pipedot.org on 2014-09-01 02:33 (#2RYE)

Instruction sycall is AMD64; for a 32-bit program, you would use an interrupt instead (int 80h since this program uses Linux system calls, could have been something else like int 21h for MS-DOS) and also possibly change the system call number and the way other parameters are passed to the system because the 32-bit kernel and the 64-bit kernel have different ways of handling these.

Everyone, feel free to correct my possible mistakes, I am not an expert.