64 bit assembly language using c standard library

Anthony Brown groundup2360917182914017 at gmail.com
Mon Jul 15 18:39:43 UTC 2013


Tell me if this is useful:

The general purpose registers in 64 bit assembly are rax, rbx, rcx, rdx,
rsp, rbp, rsi, rdi, r8, r9, r10, r11, r12, r13, r14, r15. Registers r8-r15
can have a b, w, or d following after them to represent byte, word, or
double word. Examples r8b, r8w, r8d. The calling convention when using the
c standard library is rdi, rsi, rdx, rcx, r8, r9, and then the stack
starting with the right most argument to the left most argument. We will
use yasm to assemble the assembly instructions and gcc or clang to link the
object file yasm produce. The commands to make the executable for the first
program, hello.asm, except openbsd is yasm -f elf64 hello.asm; gcc -o hello
hello.o or yasm -f elf64 hello.asm; clang -o hello hello.o. For openbsd
yasm -f elf64 hello.asm; gcc -o hello hello.o -static The start of the
lines in the source code to hello.asm and the later program examples aren't
part of the source code. Don't type them in.
[hello.asm]
1 ; The purpose of this program is to print Hello, world!
2
3 segment .data
4 hello db "Hello, world!", 0xa, 0
5
6 segment .text
7 extern printf
8 extern exit
9
10 global main
11 main:
12     mov rax, 0
13     mov rdi, hello
14     call printf
15
16     mov rax, 0
17     mov rdi, 0
18     call exit

Explanation of hello.asm
1 The ; is a comment. It is a message to the reader of the source code. The
assembler doesn't do anything with it, but ignore it.

3 Declares the data segment

4 hello is a identifier for the string Hello, world!. 0xa is to create a
newline and 0 is to terminate the Hello, world!\n with a null. db is
specify that the string is in bytes.

6 Declares the text segment.

7 Allows the linker to resolve the calls to printf later in the program.

8 Allows the linker to resolve the calls to exit later in the program.

10 This is need to use gcc or clang to do the linking.

11 This is need to use gcc or clang to do the linking.

12 Move 0 in rax. For the c standard library this tells it that their
aren't any floating point arguments to pass to the function called. If
their were floating point arguments passed to the function. Then you would
place the amount of floating point arguments. Here we have none, so we pass
0.

13 Move hello in rdi. rdi is the first argument to the printf function. The
second argument to the printf function would be rsi if there were another
argument. This hello is the format string passed to printf.

14 Call the c standard library function printf.

16 Move 0 in rax. See explanation 12 for the rest of the explanation.

17 Move 0 in rdi. This is the first and only argument to the exit function.

18 Call the c standard library function exit.

[age.asm]
1 segment .data
2 yourage db "How old are you: ", 0
3 willbe db "You will be %d years old in ten years.", 0xa, 0
4 input db "%d", 0
5 age dq 0
6
7 segment .text
8 extern printf
9 extern scanf
10 extern exit
11
12 global main
13 main:
14     mov rax, 0
15     mov rdi, yourage
16     call printf
17
18     mov rax, 0
19     mov rdi, input
20     mov rsi, age
21     call scanf
22
23     mov r15, [age]
24     add r15, 10
25     mov [age], r15
26     mov rax, 0
27     mov rdi, willbe
28     mov rsi, [age]
29     call printf
30
31     mov rax, 0
32     mov rdi, 0
33     call exit

Explanation of age.asm
1 Declare data segment.

2 Declare some data with a identifier.

3 Declare some data with a identifier.

4 Declare the format string passed to scanf.

5 Declare 64 bit data for identifier age. This data is original set to all
0, but we will set it to something else with the scanf function.

7 Declare the text segment.

8 Allows the linker to resolve the calls to printf later in the source
code.

9 Allows the linker to resolve the calls to scanf later in the source code.

10 Allows the linker to resolve the calls to exit later in the source code.

12 Allows to link using gcc or clang.

13 Allows to link using gcc or clang.

14 Move 0 in rax. This is the amount of floating point arguments pass to
the c standard library function.

15 Move yourage in rdi. This is the first argument passed to printf.
yourage happens to be the format string passed to printf.

16 Call the c standard library function printf.

18 Move 0 in rax. This is the amount of floating point arguments pass to
the c standard library function.

19 Move input in rdi. Puts the specifier of the variable passed to scanf as
the first argument to scanf.

20 Move age in rsi. Puts the variable for the c standard library function
scanf in the second argument. The second argument is placed in rsi.

21 Call the c standard library function scanf.

23 Move the contents of the the age identifier in register r15. This is
necessary, because we can't use the next add instruction with a identifier
and immediate value. The identifier is really a address.

24 Add 10 to r15 and place to addition in r15.

25 Move r15 in the contents of age. This places the contents of r15 it the
address of age.

26 Move 0 in rax. This is the amount of floating point arguments pass to
the c standard library function.

27 Move willbe in rdi. Puts the format string in the first argument to
printf function.

28 Move the contents of age in rsi. Puts the contents of age in the second
argument to printf funciton.

29 Call the c standard library printf function.

31 Move 0 in rax. This is the amount of floating point arguments pass to
the c standard library function.

32 Move 0 in rdi. This the first and only argument to exit function.

33 Call the c standard library exit function.


More information about the freebsd-doc mailing list