### Find String Length Program In Assembly Language

Hello Friends,

Here In This Post, I am going to share Two very simple piece of Assembly Language Codes That Will Simply Demonstrate, How We can Easily Calculate The length of String In Assembly.

I Hope, You Will Enjoy It. These Examples are Super Easy To Understand. If You Are new, And Don't Even Know How To Compile/Run process. Then, Please Check This Post Before Continue.

### Assembly Language Find String length Example Code [Method 1]

```; Find String Length
; author : surajsinghbisht054@gmail.com
;
;

; Section BSS
SECTION .bss

; Section Data
SECTION .data
string db "0123456789", 0AH ; This is the example string to find the length

; Section Text
SECTION .text
global _start

;
; Plan
; Iterate String From Starting to End, Until 0 bit found (End).
; Then, Simply Subtract The Ending Memory Address From Starting memory
; address to find the length of string.
;
; Routine
_start:
mov eax, string  ; Now, EAX points to starting of string
mov ebx, string  ; Now, EBX also points to starting of String

; Iterate Routine
_iterate:
cmp  byte[eax], 0 ; Compare EAX first Byte with 0, if equal then set zero flag else pass
jz _length  ; If Zero flag is set, then follow _lenth routine else pass
inc eax  ; increase EAX += 1
jmp _iterate ; Repeat

; Length
_length:
sub eax, ebx ; EAX = EBX - EAX [Difference Between memory Address of Starting and Ending Point ]
mov edx, eax ; Set length of string
mov  eax, 4  ; invoke SYS_WRITE
mov ebx, 1  ; STDOUT
mov ecx, string ; Set Memory address
int 80h   ; kernel interpt
call _exit  ; exit routine

; Exit Routine
_exit:
mov eax, 1 ; Invoke SYS_EXIT
mov ebx, 0 ; Return Value
int 80h  ; kernel interpt
```

### Assembly Language Find String length Example Code [Method 2]

```; Find String Length Method 2
; author : surajsinghbisht054@gmail.com
;

; BSS Section
SECTION .bss

; Data Section
SECTION .data
string  db "0123456789", 0AH  ; Decleare String + 0AH is a New line ASCII HEXA code

; Text Section
SECTION .text
global _start
;
; Plan
; Iterate String From Starting to End, Until 0 bit found (End).
; Also Keep Record, The Number of times of iteration Routine.
; This Iteration Register will denote the length of string
;

; Routine Start
_start:
mov eax, string  ; mov string starting point memory address to EAX
mov ebx, 0  ; SET EBX = 0

; Routine iterate
_iterate:
sub byte[eax], 0 ; Compare EAX first byte with 0, if equal set zero flag to 1 else set zero flag to 0
jz _gotit  ; if Zerof flag is 1, Follow _gotit Routine, Else pass
inc eax   ; Increase EAX
inc ebx   ; keep record, Number of String Characters Checked
jmp _iterate  ; Repeat

; Routine When, String End Found
_gotit:
mov edx, ebx  ; set EDX = EBX [Length of String]
mov eax, 4  ; invoke SYS_Write
mov ebx, 1  ; STDOUT
mov ecx, string  ; Move String Memory Address To ECX
int 80h   ; kernel interpt
call _exit  ; Follow Exit Routine

; Exit Routine
_exit:
mov eax, 1 ; Invoke SYS_EXIT
mov ebx, 0 ; Return Value
int 80h  ; Kernel interpt
```

### Compile And Run

```\$ nano find_string_lengh_2.asm
\$ mkdir bin
\$ mkdir output
\$ nasm -f elf find_string_lengh_2.asm -o bin/find_string_length_2.o
\$ ld -m elf_i386 bin/find_string_length_2.o -o output/find_string_length_2
\$ ./output/find_string_length_2
```