Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Assembly Language – String Processing

Assembly Language - String Processing

String processing is a common requirement in programming.

Processing strings in assembly means directly manipulating byte sequences in memory—lower-level but also more flexible.


Defining and storing strings

In NASM, a string is essentially a sequence of bytes and can be defined with the DB directive:

Example

; String definition methods

section

.data

   

; Method 1: standard string

    str1

db

'Hello, runoob!'

,

0

   

; C style: null-terminated

   

; Method 2: with a newline

    str2

db

'Line1'

,

0xA

,

'Line2'

,

0xA

   

; Method 3: backticks support escapes

    str3

db

hello\nworld\n

,

0

   

; Automatically converts escape sequences

   

; Method 4: define character by character

    str4

db

'A'

,

'B'

,

'C'

,

'D'

,

0

   

; Method 5: use dup to generate repeated characters

    border

db

40

dup

(

'-'

)

         

; 40 minus signs

   

; String length calculation (at compile time)

    str1_len

equ

$

str1          

; Includes the trailing null


Calculating string length

There are two ways to get the string length: compile-time calculation (EQU) and runtime calculation (scan for null):

Example

; File path: strlen_demo.asm

; Two ways to compute string length

section

.data

   

; Method A: compile-time calculation (for constant strings)

    msg1

db

'Hello, RUNOOB!'

,

0xA

    msg1_len

equ

$

msg1          

; Automatically computed at compile time

   

; Method B: null-terminated string (runtime calculation)

    msg2

db

'Find my length'

,

0

   

; Terminated with 0

section

.text

   

global

_start

_start

:

   

; Method A: use the compile-time computed length directly

   

mov

eax

,

4

   

mov

ebx

,

1

   

mov

ecx

,

msg1

   

mov

edx

,

msg1_len              

; Use the constant directly

   

int

0x80

   

; Method B: compute the length of a null-terminated string at runtime

   

mov

esi

,

msg2                  

; esi points to the start of the string

   

mov

ecx

,

0

                   

; Counter

strlen_loop

:

   

cmp

byte

[

esi

]

,

0

             

; Is the current byte null?

   

je

 strlen_done                

; Yes, done

   

inc

ecx

                       

; Increment count

   

inc

esi

                       

; Increment pointer

   

jmp

strlen_loop

strlen_done

:

   

; ecx now holds the string length (excluding null)

   

mov

eax

,

4

   

mov

ebx

,

1

   

mov

ecx

,

msg2

   

mov

edx

,

ecx

                 

; Use the computed length

   

; There is a problem here: ecx is overwritten, it should be saved first

   

; Correct approach: push ecx then pop into edx

   

mov

eax

,

1

   

mov

ebx

,

0

   

int

0x80


String copying

Copy byte by byte using a loop, or use x86 string instructions MOVSB:

Example

; File path: strcpy_demo.asm

; Two ways to implement string copying

section

.data

    src

db

'runoob source string'

,

0

    src_len

equ

$

src

section

.bss

    dest_manual

resb

64

           

; Manual copy destination

    dest_fast

resb

64

             

; Fast copy destination

section

.text

   

global

_start

_start

:

   

; Method A: manual byte-by-byte copy

   

mov

esi

,

src                    

; Source address

   

mov

edi

,

dest_manual            

; Destination address

   

mov

ecx

,

src_len                

; Byte count

copy_loop

:

   

mov

al

,

[

esi

]

                 

; Read one byte

   

mov

[

edi

]

,

al

                 

; Write one byte

   

inc

esi

                       

; Source pointer++

   

inc

edi

                       

; Destination pointer++

   

loop

copy_loop

   

; Method B: use string instructions (faster)

   

cld

                           

; Clear the direction flag (DF=0, forward copy)

   

mov

esi

,

src                    

; Source address (ESI)

   

mov

edi

,

dest_fast              

; Destination address (EDI)

   

mov

ecx

,

src_len                

; Byte count (ECX)

   

rep

movsb

                     

; Repeat movsb ecx times

   

; rep movsb:while(ecx>0) { [edi]=[esi]; esi++; edi++; ecx--; }

   

; Verify: output the two copy results

   

mov

eax

,

4

   

mov

ebx

,

1

   

mov

ecx

,

dest_manual

   

mov

edx

,

src_len

   

int

0x80

   

mov

eax

,

4

   

mov

ebx

,

1

   

mov

ecx

,

dest_fast

   

mov

edx

,

src_len

   

int

0x80

   

mov

eax

,

1

   

mov

ebx

,

0

   

int

0x80

REP MOVSB This is the most classic x86 string copy method. However, on modern CPUs, a manual copy loop with loop unrolling may be faster than REP MOVSB, because modern CPUs have better pipeline optimization for simple operations.


String comparison

Use CMPSB together with REPE(repeat until not equal) compare byte by byte:

Example

; File path: strcmp_demo.asm

; Compare whether two strings are equal

section

.data

    str_a

db

'runoob'

,

0

    str_b

db

'runoob'

,

0

    str_c

db

'RUNOOB'

,

0

    eq_msg

db

'Strings are equal'

,

0xA

    eq_len

equ

$

eq_msg

    ne_msg

db

'Strings are NOT equal'

,

0xA

    ne_len

equ

$

ne_msg

section

.text

   

global

_start

_start

:

   

cld

                           

; Forward comparison

   

mov

esi

,

str_a                  

; First string

   

mov

edi

,

str_b                  

; Second string

   

mov

ecx

,

7

                     

; Compare at most 7 bytes (including null)

   

repe

cmpsb

                     

; Repeat comparison until not equal or ecx=0

   

; repe: continue if ZF=1 (equal) and ecx>0

   

; cmpsb: compare [esi] and [edi], then esi++, edi++

   

je

 strings_equal              

; If ZF=1, all bytes are equal

   

; Not equal

   

mov

eax

,

4

   

mov

ebx

,

1

   

mov

ecx

,

ne_msg

   

mov

edx

,

ne_len

   

int

0x80

   

jmp

compare_next

strings_equal

:

   

mov

eax

,

4

   

mov

ebx

,

1

   

mov

ecx

,

eq_msg

   

mov

edx

,

eq_len

   

int

0x80

compare_next

:

   

; Compare str_a and str_c (different case)

   

cld

   

mov

esi

,

str_a

   

mov

edi

,

str_c

   

mov

ecx

,

7

   

repe

cmpsb

   

jne

not_equal_2

   

mov

eax

,

4

   

mov

ebx

,

1

   

mov

ecx

,

eq_msg

   

mov

edx

,

eq_len

   

int

0x80

   

jmp

exit

not_equal_2

:

   

mov

eax

,

4

   

mov

ebx

,

1

   

mov

ecx

,

ne_msg

   

mov

edx

,

ne_len

   

int

0x80

exit

:

   

mov

eax

,

1

   

mov

ebx

,

0

   

int

0x80


Summary of string operation instructions

Instruction Function Registers used
MOVSB Copy byte: [EDI] = [ESI] ESI=source, EDI=destination, ECX=count, DF=direction
MOVSW Copy word (2 bytes) Same as above
MOVSD Copy doubleword (4 bytes) Same as above
STOSB Store byte: [EDI] = AL EDI=destination, AL=value, ECX=count
LODSB Load byte: AL = [ESI] ESI=source
CMPSB Compare byte: [ESI] - [EDI] ESI=source, EDI=destination, ECX=count
SCASB Scan byte: AL - [EDI] EDI=destination, AL=search value, ECX=count

Case conversion example

Example

; File path: case_convert.asm

; Convert lowercase letters in a string to uppercase

section

.data

    msg

db

'Hello, runoob! Welcome to Assembly.'

,

0xA

    len

equ

$

msg

section

.text

   

global

_start

_start

:

   

; Output the original string

   

mov

eax

,

4

   

mov

ebx

,

1

   

mov

ecx

,

msg

   

mov

edx

,

len

   

int

0x80

   

; Conversion: traverse the string, lowercase -> uppercase

   

mov

esi

,

msg                    

; Point to the start of the string

   

mov

ecx

,

len                    

; Loop count

convert_loop

:

   

mov

al

,

[

esi

]

                 

; Read a character

   

cmp

al

,

'a'

                   

; Is it >= 'a'

   

jb

 next_char                  

; Less than 'a', skip

   

cmp

al

,

'z'

                   

; Is it <= 'z'

   

ja

 next_char                  

; Greater than 'z', skip

   

; Lowercase to uppercase: 'a'(97) - 'A'(65) = 32

   

sub

al

,

32

                     

; Convert to uppercase

   

mov

[

esi

]

,

al

                 

; Write back

next_char

:

   

inc

esi

   

loop

convert_loop

   

; Output the converted string

   

mov

eax

,

4

   

mov

ebx

,

1

   

mov

ecx

,

msg

   

mov

edx

,

len

   

int

0x80

   

mov

eax

,

1

   

mov

ebx

,

0

   

int

0x80

Running result:



$ nasm -f elf32 case_convert.asm -o case_convert.o

$ ld -m elf_i386 case_convert.o -o case_convert

$ ./case_convert

Hello, runoob! Welcome to Assembly.

HELLO, RUNOOB! WELCOME TO ASSEMBLY.

The direction flag DF determines the direction of string operations: with DF=0, ESI/EDI increment (forward); with DF=1, they decrement (backward). Use CLD clear DF, use STD set DF. Before calling a C function or making a system call, you should ensure DF=0 (required by cdecl).