File tree Expand file tree Collapse file tree 4 files changed +133
-5
lines changed Expand file tree Collapse file tree 4 files changed +133
-5
lines changed Original file line number Diff line number Diff line change @@ -6,8 +6,9 @@ Learning assembly for linux-x64
6
6
Examples
7
7
==============
8
8
9
- * hello - hello world
10
- * sum - sum of two numbers
11
- * stack - sum of command line arguments
9
+ * hello - hello world;
10
+ * sum - sum of two numbers;
11
+ * stack - sum of command line arguments;
12
+ * reverse - reverse string.
12
13
13
14
[ @0xAX ] ( https://twitter.com/0xAX )
Original file line number Diff line number Diff line change 1
-
2
- # check nasm version, must be more than 2.0
3
1
all :
4
2
nasm -f elf64 -o hello.o hello.asm
5
3
ld -o hello hello.o
Original file line number Diff line number Diff line change
1
+ all :
2
+ nasm -g -f elf64 -o reverse.o reverse.asm
3
+ ld -o reverse reverse.o
4
+
5
+ clean :
6
+ rm reverse reverse.o
Original file line number Diff line number Diff line change
1
+ ;;
2
+ ;; initialized data
3
+ ;;
4
+ section .data
5
+ SYS_WRITE equ 1
6
+ STD_OUT equ 1
7
+ SYS_EXIT equ 60
8
+ EXIT_CODE equ 0
9
+
10
+ NEW_LINE db 0xa
11
+ INPUT db "Hello world!"
12
+
13
+ ;;
14
+ ;; non initialized data
15
+ ;;
16
+ section .bss
17
+ OUTPUT resb 1
18
+
19
+ ;;
20
+ ;; code
21
+ ;;
22
+ section .text
23
+ global _start
24
+
25
+ ;;
26
+ ;; main routine
27
+ ;;
28
+ _start:
29
+ ;; get addres of INPUT
30
+ mov rsi , INPUT
31
+ ;; zeroize rcx for counter
32
+ xor rcx , rcx
33
+ ; df = 0 si++
34
+ cld
35
+ ; remember place after function call
36
+ mov rdi , $ + 15
37
+ ;; get string lengt
38
+ call calculateStrLength
39
+ ;; write zeros to rax
40
+ xor rax , rax
41
+ ;; additional counter for reverseStr
42
+ xor rdi , rdi
43
+ ;; reverse string
44
+ jmp reverseStr
45
+
46
+ ;;
47
+ ;; calculate length of string
48
+ ;;
49
+ calculateStrLength:
50
+ ;; check is it end of string
51
+ cmp byte [ rsi ], 0
52
+ ;; if yes exit from function
53
+ je exitFromRoutine
54
+ ;; load byte from rsi to al and inc rsi
55
+ lodsb
56
+ ;; push symbol to stack
57
+ push rax
58
+ ;; increase counter
59
+ inc rcx
60
+ ;; loop again
61
+ jmp calculateStrLength
62
+
63
+ ;;
64
+ ;; back to _start
65
+ ;;
66
+ exitFromRoutine:
67
+ ;; push return addres to stack again
68
+ push rdi
69
+ ;; return to _start
70
+ ret
71
+
72
+ ;;
73
+ ;; reverse string
74
+ ;;
75
+ ;; 31 in stack
76
+ reverseStr:
77
+ ;; check is it end of string
78
+ cmp rcx , 0
79
+ ;; if yes print result string
80
+ je printResult
81
+ ;; get symbol from stack
82
+ pop rax
83
+ ;; write it to output buffer
84
+ mov [ OUTPUT + rdi ], rax
85
+ ;; decrease length counter
86
+ dec rcx
87
+ ;; increase additional length counter (for write syscall)
88
+ inc rdi
89
+ ;; loop again
90
+ jmp reverseStr
91
+
92
+ ;;
93
+ ;; Print result string
94
+ ;;
95
+ printResult:
96
+ mov rdx , rdi
97
+ mov rax , 1
98
+ mov rdi , 1
99
+ mov rsi , OUTPUT
100
+ syscall
101
+ jmp printNewLine
102
+
103
+ ;;
104
+ ;; Print new line
105
+ ;;
106
+ printNewLine:
107
+ mov rax , SYS_WRITE
108
+ mov rdi , STD_OUT
109
+ mov rsi , NEW_LINE
110
+ mov rdx , 1
111
+ syscall
112
+ jmp exit
113
+
114
+ ;;
115
+ ;; Exit from program
116
+ ;;
117
+ exit:
118
+ ;; syscall number
119
+ mov rax , SYS_EXIT
120
+ ;; exit code
121
+ mov rdi , EXIT_CODE
122
+ ;; call sys_exit
123
+ syscall
You can’t perform that action at this time.
0 commit comments