Printing Two Strings
Part of the Fundamentals section of Coddy's Assembly journey — lesson 40 of 45.
You already know how to print one string. But what if you want to print two separate strings? For example, "Hello " and then "World!"?
You simply call the write syscall twice — once for each string.
How it works:
; First print
mov rax, 1
mov rdi, 1
mov rsi, firstMsg
mov rdx, firstLen
syscall
; Second print
mov rax, 1
mov rdi, 1
mov rsi, secondMsg
mov rdx, secondLen
syscallExample:
section .data
first db 'Hello ', 0x0a
firstLen equ $-first
second db 'World!', 0x0a
secondLen equ $-second
section .text
global _start
_start:
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
syscall
mov rax, 60
mov rdi, 0
syscallOutput:
Hello World!
Why print two strings instead of one?
| Reason | Example |
|---|---|
| Reusability | Print the same greeting with different names |
| Flexibility | Print different messages based on conditions |
| Organization | Keep different messages separate in memory |
You can print as many strings as you want:
Just repeat the write syscall for each string.
Challenge
You are given two strings: partOne with value 'Good' and partTwo with value 'bye'. Both already have a newline at the end.
Write the code to print partOne and then partTwo so that the output shows:
Good
bye
The data section and exit syscall are already provided. You only need to write the two write syscalls.
Try it yourself
section .data
partOne db 'Good', 0x0a
partOneLen equ $-partOne
partTwo db 'bye', 0x0a
partTwoLen equ $-partTwo
section .text
global _start
_start:
; TODO: Print partOne using write syscall
; TODO: Print partTwo using write syscall
mov rax, 60
mov rdi, 0
syscall
This 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 Architecture3First instruction - MOV
What is MOV?MOV ImmediateMOV Register to RegisterMOV Memory to RegisterMOV Register to MemoryLEA - Load AddressMOV with Different SizesPractice: Swap RegistersRecap: MOV Challenge6Write Syscall
Write Syscall (1)File Descriptor (rdi)Buffer Address (rsi)Byte Count (rdx)Putting It TogetherPrinting Two StringsNewline MattersRecap: Write Setup