Nasm Assembly:我试图复制一个字符串然后在其中搜索一个特定的单词,更改它并打印它。困在复制部分。

时间:2015-07-16 17:55:50

标签: arrays string assembly nasm

我坚持要把字符串源复制到目标,应该初始化为全零。看起来我需要找到字符串的大小,启动计数器寄存器,并将stringitem [counter]推送到堆栈,增加计数器寄存器。我无法弄清楚如何开始,更不用说在字符串中搜索一个单词了。

谢谢!

    bits 64
    global main
    extern printf

    section .text
main:
    ; function setup
    push    rbp
    mov     rbp, rsp
    sub     rsp, 32
    ;
    lea     rdi, [rel message]
    mov     al, 0
    call    printf
;
    lea     rdi, [rel source]
    mov     al, 0
    call    printf
;

    ;mov edi, source
    ;mov esi, target

    ;lea     rdi, [esi]
    ;mov     al, 0
    ;call    printf


    ;mov    ecx,sizeof source -1

;   mov esi,0

;L1:

;   mov     eax,source[esi];    
;   push    eax             
;   inc esi
;   loop    L1



    ; function return
    mov     eax, 0
    add     rsp, 32
    pop     rbp
    ret

    section .data
message: db      'Project',0x0D,0x0a,'Author',0x0D,0x0a,0
source:  db  0x0D,0x0a,"I can't figure out how to copy this text to target.",0x0D,0x0a,0
target:  db '0000000000000000000000000000000000000000000',0x0D,0x0a,0

1 个答案:

答案 0 :(得分:2)

对于您的数据存储器布局,这将执行

lea rdi, [rel target]
lea rsi, [rel source]
mov rcx, target-source
cld
rep movsb

否则正如Jester所说,一个简单的字节到字节拷贝也会做

    lea rdi, [rel target]
    lea rsi, [rel source]
    cld
.copy:
    lodsb
    stosb
    test al, al
    jnz .copy