Скопируйте строку в другую переменную x86 Assembly - PullRequest
0 голосов
/ 29 мая 2020

Я хотел бы сделать копию строки и сохранить копию в другой переменной. Я хочу сделать это самым простым c способом, потому что я только начал изучать Assembly. У меня есть что-то вроде этого:

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

Теперь я подумал об al oop, который будет брать каждый байт строки и назначать его новой строке. У меня есть это (я знаю, что это меняет только начальную строку, но тоже не работает):

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)

1 Ответ

2 голосов
/ 29 мая 2020

Код после получения помощи:

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
...