typestar

triangle.asm in Assembly

Nested loops draw a hash triangle one row at a time.

; Draw a hash triangle: nested loops writing one row at a time.

        global _start

ROWS:   equ 5

        section .bss
line:   resb ROWS + 1           ; widest row plus newline

        section .text
_start:
        mov r12, 1              ; current row width
.row:
        ; fill the row buffer with r12 hashes
        xor rcx, rcx
.fill:
        mov byte [line + rcx], '#'
        inc rcx
        cmp rcx, r12
        jl .fill
        mov byte [line + rcx], 10

        ; write width + 1 bytes
        lea rdx, [rcx + 1]
        mov rax, 1
        mov rdi, 1
        lea rsi, [line]
        syscall

        inc r12
        cmp r12, ROWS
        jle .row

        mov rax, 60
        xor rdi, rdi
        syscall

How it works

  1. The inner loop fills the row buffer with '#'.
  2. Each row writes width + 1 bytes — hashes plus newline.
  3. The outer loop widens until ROWS is reached.

Keywords and builtins used here

The run, in numbers

Lines
36
Characters to type
535
Tokens
94
Three-star pace
55 tpm

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

Type this snippet

Step 3 of 3 in Encore, step 31 of 31 in Language basics.

← Previous