- mov al, byte [r8 + rsi]
- lea rdx, [.output_buffer + rcx]
- mov byte [rdx], al
- inc rcx
- inc rsi
-
- lea rdx, [.output_buffer + rcx]
- mov byte [rdx], 0
+ ;; Load byte from input string
+ mov al, byte [rcx]
+
+ ;; Copy byte to output string
+
+ cmp al, $A
+ jne .not_newline
+.newline:
+ ;; It's a newline; replace it with '\r\n' in output string.
+ mov byte [rdi], $D
+ inc rdi
+ mov byte [rdi], 0
+ inc rdi
+ mov byte [rdi], $A
+ inc rdi
+ mov byte [rdi], 0
+ inc rdi
+ jmp .pop
+
+.not_newline:
+ ;; Not a newline, proceed as normal:
+ mov byte [rdi], al
+ inc rdi
+
+ ;; The output string has words rather than bytes for charactesr, so we need
+ ;; to add an extra zero:
+ mov byte [rdi], 0
+ inc rdi
+
+.pop:
+ ;; We finished copying character to output string, so pop it from the input
+ ;; string.