RosettaCodeData/Task/Copy-a-string/X86-64-Assembly/copy-a-string-1.x86-64
2023-07-01 13:44:08 -04:00

42 lines
1.3 KiB
Text

option casemap:none
option literals:on
printf proto :dword, :VARARG
exit proto :dword
.data
s db "Goodbye, World!",0
.data?
d db 20 dup (?)
dp dq ?
tb dd ?
.code
main proc
lea rsi, s ;; Put the address of var S into the source index(RSI)
xor rcx, rcx ;; Zero out RCX
_getsize:
inc rcx ;; Advanced the index by 1
cmp byte ptr [rsi+rcx],0 ;; check the current byte for terminating 0
jne _getsize ;; nope, jump back and check again
mov tb, ecx ;; tb = Total bytes, Keep a copy of the size of the string
lea rsi, s ;; Copy the address of s into the source index(rsi)
lea rdi, d ;; Copy the address of d into the destination index(rdi)
rep movsb ;; Copy bytes from ESI to EDI until RCX is 0
lea rax, s ;; Get the address of S
mov dp, rax ;; Copy it from RAX to dp
mov rbx,rdi ;; Make a copy of RDI, cause over writes due to ABI call args T_T
invoke printf, CSTR("-> s (0x%x) = %s",10), rsi, addr s
invoke printf, CSTR("-> d (0x%x) = %s",10), rbx, addr d
invoke printf, CSTR("-> dp (0x%x) = %s",10), addr dp, dp
invoke printf, CSTR("-> bytes copied: %i",10), tb
xor rsi, rsi
call exit
ret
main endp
end