Skip to content

Commit 3361dcf

Browse files
committed
strings example added
1 parent 88eaadc commit 3361dcf

File tree

4 files changed

+133
-5
lines changed

4 files changed

+133
-5
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ Learning assembly for linux-x64
66
Examples
77
==============
88

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.
1213

1314
[@0xAX](https://twitter.com/0xAX)

hello/Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
# check nasm version, must be more than 2.0
31
all:
42
nasm -f elf64 -o hello.o hello.asm
53
ld -o hello hello.o

strings/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
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

strings/reverse.asm

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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

0 commit comments

Comments
 (0)