Search for and replace characters in a string in assembly nasm issues -
i've got working copies string another. i'm trying make search term , swap it. reason, if replace function isn't commented, somehow manages delete output in console (literally goes backwards!). if comment replace function out, exact copy. trying change cat dog.
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 @ address rsi al stosb ;store al @ address rdi ;push [rdi] cmp byte rdi, 'c' je replace ;pop [rdi] test al,al ;code jump if al 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 bird.",0x0a,0x0d,0 target: db '0000000000000000000000000000000000000000000',0x0d,0x0a,0 success: db "success",0
this want. tested in ubuntu 64 with: (assumed file a.asm)
nasm -f elf64 -l a.lst a.asm
& gcc -m64 -o 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 @ address rsi al stosb ;store al @ address rdi cmp al, 'c' jne loopback lodsb ;load byte @ address rsi al stosb ;store al @ address rdi cmp al, 'a' jne loopback lodsb ;load byte @ address rsi al stosb ;store al @ 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 bird.",0x0a,0x0d,0 target: db '0000000000000000000000000000000000000000000',0x0d,0x0a,0 success: db "success",0
the output this:
project: author: cat chased bird. 0000000000000000000000000000000000000000000 dog chased bird.
Comments
Post a Comment