将字符串复制到另一个变量x86 Assembly

问题描述 投票:0回答:1

我想复制一个字符串并将其存储在另一个变量中。我想以最基本的方式来做,因为我才刚刚开始学习汇编。我有这样的东西:

section .data
        mystring db "string goes here", 10  ;string
        mystringlen equ $- mystring         ;length
        helper dw 0                         ;variable to help me in a loop

现在,我想到了一个循环,该循环将获取字符串的每个字节并将其分配给新的字符串。我有这个(我知道它只会更改初始字符串,但也不起作用):

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

所以我需要类似的东西:

old_string = 'old_string'
new_string = ''
counter=0
while(counter!=len(old_string)):
  new_string[counter] = old_string[counter]
  counter+=1
print(new_string)
string assembly x86 nasm
1个回答
0
投票

我收到帮助后的代码:

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:
    mov byte al, [esi]
    mov byte [edi], al
    inc esi
    inc edi
    inc ecx
    cmp ecx, mystringlen
    jl loopex

    ;when loop is done
    mov eax, 4
    mov ecx, new_string             ; bytes to write
    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
© www.soinside.com 2019 - 2024. All rights reserved.