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
dq 12, 45, 3, 8lays out four quadwords.[nums + rcx * 8]scales the index by the element size.- The total, 68, exits the program.
Keywords and builtins used here
addcmpdqequglobalincjlmovraxrcxrdisectionsyscallxor
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.
Step 1 of 4 in Memory & strings, step 22 of 31 in Language basics.