typestar

stdout and stderr in Assembly

File descriptors are numbers: 1 and 2 are two streams.

; File descriptors are just numbers: 1 is stdout, 2 is stderr.
        global _start

        section .data
good:   db "measurement ok", 10
GLEN:   equ $ - good
warn:   db "sensor drift!", 10
WLEN:   equ $ - warn

        section .text
_start:
        mov rax, 1
        mov rdi, 1              ; stdout
        mov rsi, good
        mov rdx, GLEN
        syscall

        mov rax, 1
        mov rdi, 2              ; stderr: same call, other stream
        mov rsi, warn
        mov rdx, WLEN
        syscall

        mov rax, 60
        xor rdi, rdi
        syscall

How it works

  1. The same sys_write serves both, fd in rdi.
  2. Redirect one and the other still arrives.
  3. Warnings belong on stderr; results on stdout.

Keywords and builtins used here

The run, in numbers

Lines
26
Characters to type
439
Tokens
73
Three-star pace
55 tpm

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

Type this snippet

Step 2 of 3 in Talking to Linux, step 27 of 31 in Language basics.

← Previous Next →