-
Notifications
You must be signed in to change notification settings - Fork 14
/
bss.asm
65 lines (52 loc) · 1.79 KB
/
bss.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
; A program that writes data in the bss section.
; Run with:
;
; $ ./run data bss
;
section .bss
big_space: resb 80 ; reserve 80 bytes
section .text
global _start
_start:
lea rbx, [rel big_space] ; keeps the initial memory position at RBX
; the LEA instruction (load effective address)
; is a handy way of calculating an address and
; load it into a register. If we replace LEA
; by MOV here, what will be put in RBX is the
; value contained in big_space (in this case,
; nothing, result: segmentation fault).
mov [rbx], byte 'H'
mov [rbx + 1], byte 'e'
mov [rbx + 2], byte 'l'
mov [rbx + 3], byte 'l'
mov [rbx + 4], byte 'o'
mov [rbx + 5], byte 10 ; newline
; Now we make a syscall to write the buffer we have set:
mov rax, 1 ; syscall write
mov rdi, 1 ; file descriptor: stdout
mov rsi, rbx ; address of buffer to RSI
mov rdx, 6 ; number of bytes to write
syscall
; But our buffer has 80 bytes of space, so we try writing something more
mov [rbx + 50], byte 'W' ; not the most beautiful code...
mov [rbx + 51], byte 'r'
mov [rbx + 52], byte 'i'
mov [rbx + 53], byte 't'
mov [rbx + 54], byte 'e'
mov [rbx + 55], byte ' '
mov [rbx + 56], byte 'b'
mov [rbx + 57], byte 'u'
mov [rbx + 58], byte 'f'
mov [rbx + 59], byte 'f'
mov [rbx + 60], byte 'e'
mov [rbx + 61], byte 'r'
mov [rbx + 62], byte 10
mov rax, 1 ; and write it again
mov rdi, 1
lea rsi, [rbx + 50]
mov rdx, 13
syscall
; Exits
mov rax, 60 ; syscall exit
xor rdi, rdi ; exit code 0
syscall