forked from yds12/x64-roadmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_data.asm
40 lines (31 loc) · 1.46 KB
/
print_data.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
; In this program we will do something similar to what we have done before
; with the "hello world" program, but now we are able to fully understand
; what is happening.
;
; This should be executed with the run script (i.e. we should make a
; standalone executable with ld, not a program called from C linked via gcc).
global _start
SYSCALL_WRITE equ 1 ; These are directives to the assembler. We are just
STDOUT equ 1 ; defining SYSCALL_WRITE = 1 and STDOUT = 1
; This is done in pre-processing before the actual
; assembling process.
section .data:
some_bytes: db 'O', 10, 'K', 10
LEN equ $ - some_bytes ; Another NASM directive, here $ means the current
; position. So LEN is the difference between the
; current position and some_bytes, that is,
; the size of the data starting in the label
; some_bytes.
; Now that we have set up all the data we need, our program will just do
; a system call to write the data to STDOUT, passing the data and the
; length (in bytes) to write.
section .text:
_start:
mov rax, SYSCALL_WRITE ; type of syscall goes in RAX
mov rdi, STDOUT ; file descriptor goes in RDI
mov rsi, some_bytes ; content to write goes in RSI
mov rdx, LEN ; length goes in RDX
syscall
mov rax, 60 ; now we invoke the syscall for exit with code 0
xor rdi, rdi
syscall