-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
133 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
|
||
# check nasm version, must be more than 2.0 | ||
all: | ||
nasm -f elf64 -o hello.o hello.asm | ||
ld -o hello hello.o | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
all: | ||
nasm -g -f elf64 -o reverse.o reverse.asm | ||
ld -o reverse reverse.o | ||
|
||
clean: | ||
rm reverse reverse.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
;; | ||
;; initialized data | ||
;; | ||
section .data | ||
SYS_WRITE equ 1 | ||
STD_OUT equ 1 | ||
SYS_EXIT equ 60 | ||
EXIT_CODE equ 0 | ||
|
||
NEW_LINE db 0xa | ||
INPUT db "Hello world!" | ||
|
||
;; | ||
;; non initialized data | ||
;; | ||
section .bss | ||
OUTPUT resb 1 | ||
|
||
;; | ||
;; code | ||
;; | ||
section .text | ||
global _start | ||
|
||
;; | ||
;; main routine | ||
;; | ||
_start: | ||
;; get addres of INPUT | ||
mov rsi, INPUT | ||
;; zeroize rcx for counter | ||
xor rcx, rcx | ||
; df = 0 si++ | ||
cld | ||
; remember place after function call | ||
mov rdi, $ + 15 | ||
;; get string lengt | ||
call calculateStrLength | ||
;; write zeros to rax | ||
xor rax, rax | ||
;; additional counter for reverseStr | ||
xor rdi, rdi | ||
;; reverse string | ||
jmp reverseStr | ||
|
||
;; | ||
;; calculate length of string | ||
;; | ||
calculateStrLength: | ||
;; check is it end of string | ||
cmp byte [rsi], 0 | ||
;; if yes exit from function | ||
je exitFromRoutine | ||
;; load byte from rsi to al and inc rsi | ||
lodsb | ||
;; push symbol to stack | ||
push rax | ||
;; increase counter | ||
inc rcx | ||
;; loop again | ||
jmp calculateStrLength | ||
|
||
;; | ||
;; back to _start | ||
;; | ||
exitFromRoutine: | ||
;; push return addres to stack again | ||
push rdi | ||
;; return to _start | ||
ret | ||
|
||
;; | ||
;; reverse string | ||
;; | ||
;; 31 in stack | ||
reverseStr: | ||
;; check is it end of string | ||
cmp rcx, 0 | ||
;; if yes print result string | ||
je printResult | ||
;; get symbol from stack | ||
pop rax | ||
;; write it to output buffer | ||
mov [OUTPUT + rdi], rax | ||
;; decrease length counter | ||
dec rcx | ||
;; increase additional length counter (for write syscall) | ||
inc rdi | ||
;; loop again | ||
jmp reverseStr | ||
|
||
;; | ||
;; Print result string | ||
;; | ||
printResult: | ||
mov rdx, rdi | ||
mov rax, 1 | ||
mov rdi, 1 | ||
mov rsi, OUTPUT | ||
syscall | ||
jmp printNewLine | ||
|
||
;; | ||
;; Print new line | ||
;; | ||
printNewLine: | ||
mov rax, SYS_WRITE | ||
mov rdi, STD_OUT | ||
mov rsi, NEW_LINE | ||
mov rdx, 1 | ||
syscall | ||
jmp exit | ||
|
||
;; | ||
;; Exit from program | ||
;; | ||
exit: | ||
;; syscall number | ||
mov rax, SYS_EXIT | ||
;; exit code | ||
mov rdi, EXIT_CODE | ||
;; call sys_exit | ||
syscall |