Skip to content

Commit

Permalink
strings example added
Browse files Browse the repository at this point in the history
  • Loading branch information
0xAX committed Nov 20, 2014
1 parent 88eaadc commit 3361dcf
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 5 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ Learning assembly for linux-x64
Examples
==============

* hello - hello world
* sum - sum of two numbers
* stack - sum of command line arguments
* hello - hello world;
* sum - sum of two numbers;
* stack - sum of command line arguments;
* reverse - reverse string.

[@0xAX](https://twitter.com/0xAX)
2 changes: 0 additions & 2 deletions hello/Makefile
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
Expand Down
6 changes: 6 additions & 0 deletions strings/Makefile
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
123 changes: 123 additions & 0 deletions strings/reverse.asm
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

0 comments on commit 3361dcf

Please sign in to comment.