Reputation: 63
I would like to make a copy of a string and store the copy in another variable. I want to do it the most basic way, cause I have just started learning Assembly. I have something like this:
section .data
mystring db "string goes here", 10 ;string
mystringlen equ $- mystring ;length
helper dw 0 ;variable to help me in a loop
Now, I thought of a loop that will take each byte of the string and assign it to new string. I have this (I know it only changes initial string, but doesn't work either):
loopex:
mov [mystring+helper], byte 'n' ;change byte of string to 'n'
add helper, 1 ;helper+=1
cmp helper,mystringlen ;compare helper with lenght of string
jl loopex
;when loop is done
mov eax, 4
mov ecx, mystring ; print changed string
mov edx, mystringlen ; number of bytes to write
;after printing
int 0x80 ; perform system call
mov eax, 1 ; sys_exit system call
mov ebx, 0 ;exit status 0
int 0x80
So I would need something like:
old_string = 'old_string'
new_string = ''
counter=0
while(counter!=len(old_string)):
new_string[counter] = old_string[counter]
counter+=1
print(new_string)
Upvotes: 0
Views: 1680
Reputation: 63
The code after help I received:
section .data
mystring db "This is the string we are looking for", 10
mystringlen equ $- mystring
section .bss
new_string: resb mystringlen
section .text
global _start
_start:
mov ecx, 0
mov esi, mystring
mov edi, new_string
loopex: ; do {
mov byte al, [esi]
mov byte [edi], al
inc esi
inc edi
inc ecx
cmp ecx, mystringlen
jl loopex ; }while(++ecx < mystringlen)
;when loop is done
mov eax, 4 ; __NR_write (asm/unistd_32.h)
mov ebx, 1 ; fd = STDOUT_FILENO
mov ecx, new_string ; bytes to write
mov edx, mystringlen ; number of bytes to write
int 0x80 ; perform system call: write(1, new_string, mystringlen)
;after printing
mov eax, 1 ; sys_exit system call number
mov ebx, 0 ; exit status 0
int 0x80
Upvotes: 2