typestar

Scaled addressing in Assembly

base + index * size reaches array slots in one operand.

; Scaled addressing: base + index * size reaches array slots.
        global _start

        section .data
nums:   dq 12, 45, 3, 8
COUNT:  equ 4

        section .text
_start:
        xor rax, rax            ; total
        xor rcx, rcx            ; index
.loop:
        add rax, [nums + rcx * 8]
        inc rcx
        cmp rcx, COUNT
        jl .loop

        mov rdi, rax            ; 68
        mov rax, 60             ; exit code 68
        syscall

How it works

  1. dq 12, 45, 3, 8 lays out four quadwords.
  2. [nums + rcx * 8] scales the index by the element size.
  3. The total, 68, exits the program.

Keywords and builtins used here

The run, in numbers

Lines
20
Characters to type
357
Tokens
60
Three-star pace
55 tpm

At the three-star pace of 55 tokens a minute, this run takes about 65 seconds.

Type this snippet

Step 1 of 4 in Memory & strings, step 22 of 31 in Language basics.

← Previous Next →