Newline Matters
Part of the Fundamentals section of Coddy's Assembly journey — lesson 41 of 45.
You have been adding 0x0a (newline) at the end of your strings. But what happens if you forget it?
Without newline:
The text prints, but the cursor stays on the same line. Any future output will appear right next to it.
Example without newline:
section .data
first db 'Hello'
firstLen equ $ - first
second db 'World'
secondLen equ $ - second
section .text
mov rax, 1
mov rdi, 1
mov rsi, first
mov rdx, firstLen
syscall
mov rax, 1
mov rdi, 1
mov rsi, second
mov rdx, secondLen
syscallOutput:
HelloWorld
No space. No newline. Everything is stuck together.
With newline:
section .data
first db 'Hello', 0x0a
firstLen equ $ - first
second db 'World', 0x0a
secondLen equ $ - secondOutput:
Hello
World
Each string prints on its own line.
What newline does:
| Character | ASCII | Effect |
|---|---|---|
0x0a | 10 | Moves cursor to the beginning of the next line |
Common newline mistakes:
| Mistake | Result |
|---|---|
| No newline at all | Text runs together on one line |
| Newline only at the end of last string | Previous strings run together, last one goes to next line |
| Newline in the middle of a string | Line breaks in unexpected places |
The safe rule:
Always add 0x0a at the end of every string you want on its own line.
Challenge
You are given three strings without newlines. Your task is to print them so they appear on three separate lines.
The strings are:
| String | Value |
|---|---|
word1 | 'Apple' |
word2 | 'Banana' |
word3 | 'Cherry' |
Requirements:
- Define each string with a newline (
0x0a) at the end - Calculate the length of each string automatically
- Print all three strings using the write syscall
- Exit cleanly with code 0
Try it yourself
section .data
; TODO: Define word1 with 'Apple' and newline
; TODO: Define word2 with 'Banana' and newline
; TODO: Define word3 with 'Cherry' and newline
; TODO: Calculate lengths for each string
section .text
global _start
_start:
; TODO: Print word1
; TODO: Print word2
; TODO: Print word3
; TODO: Exit cleanlysection .data
word1 db 'Apple', 0x0a
word1Len equ $ - word1
word2 db 'Banana', 0x0a
word2Len equ $ - word2
word3 db 'Cherry', 0x0a
word3Len equ $ - word3
section .text
global _start
_start:
mov rax, 1
mov rdi, 1
mov rsi, word1
mov rdx, word1Len
syscall
mov rax, 1
mov rdi, 1
mov rsi, word2
mov rdx, word2Len
syscall
mov rax, 1
mov rdi, 1
mov rsi, word3
mov rdx, word3Len
syscall
mov rax, 60
mov rdi, 0
syscallThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
1The Machine
What is CPUMemoryRegisters vs MemoryHow instructions workTwo-File PatternThe x86-64 Architecture