typestar

sys_write in Assembly

fd, buffer, length in rdi, rsi, rdx — then syscall.

; sys_write wants fd, buffer, length -- in rdi, rsi, rdx.
        global _start

        section .data
msg:    db "hello, machine", 10
len:    equ $ - msg             ; the assembler counts for us

        section .text
_start:
        mov rax, 1              ; sys_write
        mov rdi, 1              ; stdout
        mov rsi, msg
        mov rdx, len
        syscall

        mov rax, 60
        xor rdi, rdi
        syscall

How it works

  1. The .data section holds the message bytes and a newline.
  2. equ $ - msg has the assembler compute the length.
  3. Syscall 1 writes; the registers are the argument order.

Keywords and builtins used here

The run, in numbers

Lines
18
Characters to type
340
Tokens
47
Three-star pace
60 tpm

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

Type this snippet

Step 2 of 3 in First programs, step 2 of 31 in Language basics.

← Previous Next →