typestar

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

  1. mov al, [src + rcx] then mov [dst + rcx], al.
  2. The copy lands in .bss space reserved with resb.
  3. Printing the copy proves the bytes moved.

Keywords and builtins used here

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.

Type this snippet

Step 4 of 4 in Data, step 10 of 31 in Language basics.

← Previous Next →