initial import
[staff/goc4/2018bti7061.git] / asm-8-strings / examples / movestring / movestring1mov.asm
1 ; Executable name : movestring1mov
2 ; Version : 1.0
3 ; Created date : 3/11/2014
4 ; Last update : 3/11/2014
5 ; Author : Emmanuel Benoist
6 ; Description : A simple program in assembly for Linux, using NASM 2.05,
7 ; demonstrating the way to copy a string using MOV instructions
8 ;
9 ; Build using these commands:
10 ; nasm -f elf64 -g -F stabs movestring1mov.asm
11 ; ld -o movestring1mov movestring1mov.o
12 ;
13
14 SECTION .data ; Section containing initialised data
15
16 InitString db "Hello World",10
17 INITLEN equ $-InitString
18
19 TargetString db " ",10
20 TARGETLEN equ $-TargetString
21
22 SECTION .bss ; Section containing uninitialized data
23
24
25 SECTION .text ; Section containing code
26
27 global _start ; Linker needs this to find the entry point!
28
29 _start:
30 nop ; This no-op keeps gdb happy...
31
32 ;; Init registers to start the copy
33 mov rcx, INITLEN ; The counter receives the length of the string
34 mov rsi, InitString ; The source index receives Initial String
35
36 mov rdi, TargetString ; The destination index receives the address of Target String
37
38
39 .loopcopystring:
40 mov al, byte [rsi] ; The element at rsi is copied into Register A
41 mov byte [rdi], al ; The element in A is copied into RDI
42 inc rsi
43 inc rdi ; We go forward in both strings
44 dec rcx ; The counter is decremented
45 jnz .loopcopystring ; Loop as long as RCS is not 0
46
47 ;; In the end TargetStings points to a copy of
48 ;; InitString
49 nop ;To keep gdb happy
50 Output: mov eax,4 ; Specify sys_write call
51 mov ebx,1 ; Specify File Descriptor 2: Standard Error
52 mov ecx,TargetString ; Pass offset of the error message
53 mov edx,TARGETLEN ; Pass the length of the message
54 int 80H ; Make kernel call
55
56 Exit: mov eax,1 ; Code for Exit Syscall
57 mov ebx,0 ; Return a code of zero
58 int 80H ; Make kernel call