Parsing CSV

Walking a line

A CSV line is bytes in a buffer. Parse it with two pointers: one walks the buffer, one marks the start of the current field. The loop ends at the newline or the NUL:

mov rsi, line ; walker mov rdi, line ; field start loop: mov al, [rsi] cmp al, 0 je done

Splitting fields

When the walker hits a comma, the field runs from the field-start pointer to the walker. Copy it, print it, or store its length, then move the field-start pointer past the comma:

cmp al, "," jne next ; field = [rdi, rsi) lea rdi, [rsi+1] ; next field starts after the comma

Quoted fields

Real CSV quotes fields that contain commas or newlines. A quoted field starts with a double quote, keeps commas as data, and ends at the closing quote. Handle that state machine, or accept only unquoted input and say so in the program's docs.

This complete program splits a,b,c into three fields and prints each one with a space after it:

default rel %define SYS_WRITE 1 %define SYS_EXIT 60 section .data line db "a,b,c", 0 sep db " " section .text global _start _start: mov rsi, line ; walker mov rdi, line ; field start next: mov al, [rsi] cmp al, 0 je last cmp al, "," je field inc rsi jmp next field: mov rdx, rsi sub rdx, rdi ; field length push rsi mov rax, SYS_WRITE mov rsi, rdi mov rdi, 1 syscall mov rax, SYS_WRITE mov rdi, 1 lea rsi, [sep] mov rdx, 1 syscall pop rsi lea rdi, [rsi + 1] ; next field starts after the comma inc rsi jmp next last: mov rdx, rsi sub rdx, rdi mov rax, SYS_WRITE mov rsi, rdi mov rdi, 1 syscall mov rax, SYS_EXIT xor rdi, rdi syscall

nasm -f elf64 -o csv.o csv.asm
ld -o csv csv.o
./csv
echo $?

a b c
0

The first snippets are fragments: they show the walker and the field split. The complete program was assembled with NASM 3.01, linked with GNU ld 2.45.1, and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What marks the start of a CSV field in the two-pointer parser?

    Answer

    The field-start pointer

    One pointer walks the bytes, and the other remembers where the current field began.

  2. What does a comma tell the parser?

    Answer

    The field ends here and the next one starts after it

    The field is the range from its start to the comma, and the next field starts one byte later.

  3. What ends the loop over a line?

    Answer

    A NUL byte

    The buffer is a C string, so the walker stops at the terminating zero.

  4. How is a quoted field different?

    Answer

    Commas inside it are data until the closing quote

    A state machine keeps the quoted text together, including commas and newlines.

  5. What is the length of a field?

    Answer

    The walker minus the field-start pointer

    The two positions bracket the field, so their difference is its byte count.

  6. What does the program do with an unquoted comma?

    Answer

    It treats it as a separator

    The simple parser handles only unquoted input, as the page says.

  7. What does this program print? default rel %define SYS_WRITE 1 %define SYS_EXIT 60 section .data line db "a,b,c", 0 sep db " " section .text global _start _start: mov rsi, line mov rdi, line next: mov al, [rsi] cmp al, 0 je last cmp al, "," je field inc rsi jmp next field: mov rdx, rsi sub rdx, rdi push rsi mov rax, SYS_WRITE mov rsi, rdi mov rdi, 1 syscall mov rax, SYS_WRITE mov rdi, 1 lea rsi, [sep] mov rdx, 1 syscall pop rsi lea rdi, [rsi + 1] inc rsi jmp next last: mov rdx, rsi sub rdx, rdi mov rax, SYS_WRITE mov rsi, rdi mov rdi, 1 syscall mov rax, SYS_EXIT xor rdi, rdi syscall

    Answer

    a b c

    The program prints each field followed by one space; the last field has no separator.

  8. Why does the parser push RSI before the write?

    Answer

    The write needs RSI for the buffer, so the walker is saved

    RSI holds the walker, and sys_write takes its buffer in RSI, so the value is saved and restored.

  9. What does a quoted field need beyond the two-pointer loop?

    Answer

    A state that keeps commas inside the quotes

    The parser must know whether it is inside quotes, or a comma in the text would split the field.

  10. What is the exit status of the CSV program on this page?

    Answer

    0

    The program ends with sys_exit and rdi cleared.

Article author: Arthur Isaev