memcpy by hand in Assembly
Load a byte, store a byte, walk both pointers.
; memcpy by hand: load a byte, store a byte, walk both pointers.
global _start
section .data
src: db "relay", 10
SLEN: equ $ - src
section .bss
dst: resb SLEN
section .text
_start:
xor rcx, rcx
.copy:
mov al, [src + rcx]
mov [dst + rcx], al
inc rcx
cmp rcx, SLEN
jl .copy
mov rax, 1
mov rdi, 1
mov rsi, dst ; print the copy, not the original
mov rdx, SLEN
syscall
mov rax, 60
xor rdi, rdi
syscall
How it works
mov al, [src + rcx]thenmov [dst + rcx], al.- The copy lands in
.bssspace reserved with resb. - Printing the copy proves the bytes moved.
Keywords and builtins used here
alcmpdbequglobalincjlmovraxrcxrdirdxresbrsisectionsyscallxor
The run, in numbers
- Lines
- 29
- Characters to type
- 420
- Tokens
- 78
- Three-star pace
- 55 tpm
At the three-star pace of 55 tokens a minute, this run takes about 85 seconds.
Step 4 of 4 in Data, step 10 of 31 in Language basics.