initial import
[staff/goc4/2018bti7061.git] / asm-8-strings / examples / movestring / movestring4movsb.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 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 movsb ; From [RSI] to [RDI] (and increment RDI and RSI)
41
42 dec rcx ; The counter is decremented
43 jnz .loopcopystring ; Loop as long as RCS is not 0
44
45 ;; In the end TargetStings points to a copy of
46 ;; InitString
47 nop ;To keep gdb happy
48 Output: mov eax,4 ; Specify sys_write call
49 mov ebx,1 ; Specify File Descriptor 2: Standard Error
50 mov ecx,TargetString ; Pass offset of the error message
51 mov edx,INITLEN ; Pass the length of the message
52 ;;(we only copied initlen chars)
53 int 80H ; Make kernel call
54
55 Exit: mov eax,1 ; Code for Exit Syscall
56 mov ebx,0 ; Return a code of zero
57 int 80H ; Make kernel call