initial import
[staff/goc4/2018bti7061.git] / asm-8-strings / examples / movestring / movestring5rep.asm
1 ; Executable name : movestring4movsb
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 MOVSB instruction
8 ;
9 ; Build using these commands:
10 ; nasm -f elf64 -g -F stabs movestring4movsb.asm
11 ; ld -o movestring2stosbreset movestring4movsb.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 rdi, TargetString ; The destination index receives the address of Target String
35 mov al, 'm' ; Move a marker in AL
36
37
38 rep stosb ; Loop From al to [RDI] (and increment RDI and RSI)
39
40 mov byte[TargetString+INITLEN-1],0Ah ; insert \n in the end of the copied string
41
42 ;; In the end TargetStings points to a copy of
43 ;; InitString
44 nop ;To keep gdb happy
45 Output: mov eax,4 ; Specify sys_write call
46 mov ebx,1 ; Specify File Descriptor 2: Standard Error
47 mov ecx,TargetString ; Pass offset of the error message
48 mov edx,INITLEN ; Pass the length of the message
49 ;;(we only copied initlen chars)
50 int 80H ; Make kernel call
51
52 Exit: mov eax,1 ; Code for Exit Syscall
53 mov ebx,0 ; Return a code of zero
54 int 80H ; Make kernel call