【问题标题】:Search for and replace characters in a string in assembly nasm issues在程序集 nasm 问题中搜索和替换字符串中的字符
【发布时间】:2015-10-06 13:19:00
【问题描述】:

我已经完成了将一个字符串复制到另一个字符串的工作。我试图让它搜索一个术语并交换它。出于某种原因,如果没有注释替换功能,它会设法删除控制台中的输出(字面意思是倒退!)。如果我将替换功能注释掉,我只会得到一个准确的副本。试图把猫变成狗。

    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

    ;print source message
    lea     rdi, [rel source]
    mov     al, 0
    call    printf

    ;print target message
    lea     rdi, [rel target]
    mov     al, 0
    call    printf



    lea rdi, [rel target]
    lea rsi, [rel source]
    cld
    jmp Loop

Loop:
    lodsb       ;Load byte at address RSI into AL
    stosb       ;Store AL at address RDI
    ;push   [rdi]   
    cmp byte RDI, 'c'
    je  replace     
    ;pop [rdi]
    test al,al  ;code will jump only if al is not equ 0
    jnz Loop

replace:
    ;lea     rdi, [rel success]
    mov byte [rdi], 'd'
    ;call    printf 
     ret





;print new version of target
    lea     rdi, [rel target]
    mov     al, 0
    call    printf



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

section .data
message: db      'Project:',0x0D,0x0a,'Author:',0x0D,0x0a,0x0D,0x0a,0

source:  db "The cat chased the bird.",0x0a,0x0D,0
target:  db '0000000000000000000000000000000000000000000',0x0D,0x0a,0

success: db "Success",0

【问题讨论】:

  • ret 后面的replace 标签是故意的吗?
  • 我把它放在那里是为了返回调用函数,但我想堆栈会处理这个问题。
  • @user3866044:你得到答案了吗?

标签: string assembly replace 64-bit nasm


【解决方案1】:

这就是你想要的。我在 Ubuntu 64 中对其进行了测试: (假设这个文件是a.asm)

nasm -f elf64 -l a.lst a.asm & gcc -m64 -o a a.o

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

;print source message
lea     rdi, [rel source]
mov     al, 0
call    printf

;print target message
lea     rdi, [rel target]
mov     al, 0
call    printf

lea rdi, [rel target]
lea rsi, [rel source]
cld

Loop:

lodsb       ;Load byte at address RSI into AL
stosb       ;Store AL at address RDI

cmp  al, 'c'
jne  LoopBack

lodsb       ;Load byte at address RSI into AL
stosb       ;Store AL at address RDI
cmp  al, 'a'
jne  LoopBack

lodsb       ;Load byte at address RSI into AL
stosb       ;Store AL at address RDI
cmp  al, 't'
jne  LoopBack

sub rdi, 3
mov byte [rdi], 'd'
inc rdi
mov byte [rdi], 'o'
inc rdi
mov byte [rdi], 'g'
inc rdi

LoopBack:
cmp al, 0
jne Loop

;print new version of target
lea     rdi, [rel target]
mov     al, 0
call    printf

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

section .data
message: db      'Project:',0x0D,0x0a,'Author:',0x0D,0x0a,0x0D,0x0a,0

source:  db "The cat chased the bird.",0x0a,0x0D,0
target:  db '0000000000000000000000000000000000000000000',0x0D,0x0a,0

success: db "Success",0

输出是这样的:

Project:
Author:

The cat chased the bird.
0000000000000000000000000000000000000000000
The dog chased the bird.

【讨论】:

  • 你在做同样的测试两次。 "cmp al,0;jne 循环" "test al,al;jnz 循环"
  • 你是对的。我忘了删除它,但它没有效果。这是提问者的问题。
  • 如果你使用rdi,不要递增,只递减di
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-19
  • 2017-01-10
  • 1970-01-01
  • 2015-09-10
  • 2018-01-02
相关资源
最近更新 更多