Member-only story
Writing an X86–64 Assembly Language Program
Part VI: How to Determine String Length
This guide is part six of a series
- Part one: Getting Started Writing Assembly Language
- Part two: Finding an Efficient Development Cycle for writing Assembly Language
- Part three: Writing an X86–64 Assembly Language Program: Printing Command Line Arguments
- Part four: Writing an X86–64 Assembly Language Program: Sending Function Arguments and Receiving a Return Value
- Part five: Writing an X86–64 Assembly Language Program: Conditionals, Jumping, and Looping
- Part seven: Quick Reference
How to Calculate String Length
In order to calculate the length of a string, we’ll first need to know what determines the end of a given string.
Strings in memory are represented as a pointer. The location pointed at is a byte of data representing a character followed by additional characters contiguous in memory. The important point is that this sequence of bytes is terminated by the byte 0x00
. This is called the zero-termination character which is one method of terminating a string (C uses this approach, for example). There are methods of encoding strings that we won’t cover here.
For example, the string “HELLO WORLD!” would hold the following values in memory (binary values represented in hexadecimal):
So the length can be determined by looping through each byte of memory in a string until the zero-termination character is reached. Here is an initial implementation of how this may be implemented, but if you look closely there are a couple of bugs, see if you can identify the issues with this code snippet.