SlideShare a Scribd company logo
http://www.tutorialspoint.com/assembly_prog ramming /assembly_string s.htm Copyright © tutorialspoint.com
ASSEMBLY - STRING PROCESSING
We have already used variable lengths strings inour previous examples. Youmust have noticed that, the variable
lengths strings canhave as many characters as required. Generally, we specify the lengthof the string by either
of the two ways:
Explicitly storing string length
Using a sentinelcharacter
We canstore the string lengthexplicitly by using the $ locationcounter symbolthat represents the current value
of the locationcounter. Inthe following example:
msg db 'Hello, world!',0xa ;our dear string
len equ $ - msg ;length of our dear string
$ points to the byte after the last character of the string variable msg. Therefore, $-msg gives the lengthof the
string. We canalso write
msg db 'Hello, world!',0xa ;our dear string
len equ 13 ;length of our dear string
Alternatively, youcanstore strings witha trailing sentinelcharacter to delimit a string instead of storing the string
lengthexplicitly. The sentinelcharacter should be a specialcharacter that does not appear withina string.
For example:
message DB 'I am loving it!', 0
String Instructions
Eachstring instructionmay require a source operand, a destinationoperand or both. For 32-bit segments,
string instructions use ESI and EDI registers to point to the source and destinationoperands, respectively.
For 16-bit segments, however, the SI and the DI registers are used to point to the source and destination,
respectively.
There are five basic instructions for processing strings. They are:
MOVS - This instructionmoves 1 Byte, Word or Doubleword of data frommemory locationto another.
LODS - This instructionloads frommemory. If the operand is of one byte, it is loaded into the AL
register, if the operand is one word, it is loaded into the AX register and a doubleword is loaded into the
EAX register.
STOS - This instructionstores data fromregister (AL, AX, or EAX) to memory.
CMPS - This instructioncompares two data items inmemory. Data could be of a byte size, word or
doubleword.
SCAS - This instructioncompares the contents of a register (AL, AX or EAX) withthe contents of anitem
inmemory.
Eachof the above instructionhas a byte, word and doubleword versionand string instructions canbe repeated
by using a repetitionprefix.
These instructions use the ES:DI and DS:SI pair of registers, where DI and SI registers containvalid offset
addresses that refers to bytes stored inmemory. SI is normally associated withDS (data segment) and DI is
always associated withES (extra segment).
The DS:SI (or ESI) and ES:DI (or EDI) registers point to the source and destinationoperands, respectively.
The source operand is assumed to be at DS:SI (or ESI) and the destinationoperand at ES:DI (or EDI) in
memory.
For 16-bit addresses, the SI and DI registers are used, and for 32-bit addresses, the ESI and EDI registers are
used.
The following table provides various versions of string instructions and the assumed space of the operands.
Basic
Instruction
Operands
at
Byte
Operation
Word
Operation
Double word
Operation
MOVS ES:DI, DS:EI MOVSB MOVSW MOVSD
LODS AX, DS:SI LODSB LODSW LODSD
STOS ES:DI, AX STOSB STOSW STOSD
CMPS DS:SI, ES: DI CMPSB CMPSW CMPSD
SCAS ES:DI, AX SCASB SCASW SCASD
Repetition Prefixes
The REP prefix, whenset before a string instruction, for example - REP MOVSB, causes repetitionof the
instructionbased ona counter placed at the CX register. REP executes the instruction, decreases CX by 1, and
checks whether CX is zero. It repeats the instructionprocessing untilCX is zero.
The DirectionFlag (DF) determines the directionof the operation.
Use CLD (Clear DirectionFlag, DF = 0) to make the operationleft to right.
Use STD (Set DirectionFlag, DF = 1) to make the operationright to left.
The REP prefix also has the following variations:
REP: it is the unconditionalrepeat. It repeats the operationuntilCX is zero.
REPE or REPZ: It is conditionalrepeat. It repeats the operationwhile the zero flag indicates equal/zero.
It stops whenthe ZF indicates not equal/zero or whenCX is zero.
REPNE or REPNZ: It is also conditionalrepeat. It repeats the operationwhile the zero flag indicates not
equal/zero. It stops whenthe ZF indicates equal/zero or whenCX is decremented to zero.

More Related Content

PPTX
[ASM]Lab8
PPT
Ch7 OS
 
PPT
Assembly Language String Chapter
PPT
Assembly language programming(unit 4)
PPTX
Arrays, Strings & Loops in assembly Language.pptx
PPT
Al2ed chapter10
PPTX
String-Instructions-in-Assembly-Language.pptx
PPT
Instruction set
[ASM]Lab8
Ch7 OS
 
Assembly Language String Chapter
Assembly language programming(unit 4)
Arrays, Strings & Loops in assembly Language.pptx
Al2ed chapter10
String-Instructions-in-Assembly-Language.pptx
Instruction set

Similar to N_Asm Assembly strings (sol) (20)

PDF
String_manipulations.pdf
DOCX
LA3-64 -vit assembly language program to Accept string and display its length...
PPT
Assembly language
PDF
8086 String Instructions.pdf
PPTX
Instruction Set of 8086 Microprocessor
PPTX
Reversing & Malware Analysis Training Part 4 - Assembly Programming Basics
PPTX
Chapter 3 programming concepts-ii
PPTX
Microprocessors-based systems (under graduate course) Lecture 8 of 9
PPT
Al2ed chapter4
PPTX
Reversing malware analysis training part4 assembly programming basics
DOCX
Assembly language
PPTX
Byte and string manipulation 8086
PPTX
instructionsetsofjdtufgmictfgfjh8086.pptx
PPT
Lect 13 Zaheer Abbas
POT
Arrays and addressing modes
PPT
Wk1to4
PDF
Reversing & malware analysis training part 4 assembly programming basics
PPTX
Introduction to Assembly Language
String_manipulations.pdf
LA3-64 -vit assembly language program to Accept string and display its length...
Assembly language
8086 String Instructions.pdf
Instruction Set of 8086 Microprocessor
Reversing & Malware Analysis Training Part 4 - Assembly Programming Basics
Chapter 3 programming concepts-ii
Microprocessors-based systems (under graduate course) Lecture 8 of 9
Al2ed chapter4
Reversing malware analysis training part4 assembly programming basics
Assembly language
Byte and string manipulation 8086
instructionsetsofjdtufgmictfgfjh8086.pptx
Lect 13 Zaheer Abbas
Arrays and addressing modes
Wk1to4
Reversing & malware analysis training part 4 assembly programming basics
Introduction to Assembly Language
Ad

More from Selomon birhane (12)

PDF
N_Asm Assembly system calls (sol)
PDF
N_Asm Assembly basic syntax (sol)
PDF
N_Asm Assembly addressing modes (sol)
PDF
N_Asm Assembly arrays (sol)
PDF
N_Asm Assembly arithmetic instructions (sol)
PDF
N_Asm Assembly recursion (sol)
PDF
N_Asm Assembly numbers (sol)
PDF
N_Asm Assembly registers (sol)
PDF
N_Asm Assembly variables (sol)
PDF
N_Asm Assembly macros (sol)
PDF
Control system(smarajit ghosh) By sol
PDF
Two ports
N_Asm Assembly system calls (sol)
N_Asm Assembly basic syntax (sol)
N_Asm Assembly addressing modes (sol)
N_Asm Assembly arrays (sol)
N_Asm Assembly arithmetic instructions (sol)
N_Asm Assembly recursion (sol)
N_Asm Assembly numbers (sol)
N_Asm Assembly registers (sol)
N_Asm Assembly variables (sol)
N_Asm Assembly macros (sol)
Control system(smarajit ghosh) By sol
Two ports
Ad

Recently uploaded (20)

PPTX
history of c programming in notes for students .pptx
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
System and Network Administraation Chapter 3
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
System and Network Administration Chapter 2
PDF
medical staffing services at VALiNTRY
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
ai tools demonstartion for schools and inter college
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
Transform Your Business with a Software ERP System
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Nekopoi APK 2025 free lastest update
PDF
top salesforce developer skills in 2025.pdf
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Digital Strategies for Manufacturing Companies
history of c programming in notes for students .pptx
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
System and Network Administraation Chapter 3
Upgrade and Innovation Strategies for SAP ERP Customers
System and Network Administration Chapter 2
medical staffing services at VALiNTRY
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
ai tools demonstartion for schools and inter college
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Transform Your Business with a Software ERP System
Reimagine Home Health with the Power of Agentic AI​
Nekopoi APK 2025 free lastest update
top salesforce developer skills in 2025.pdf
Softaken Excel to vCard Converter Software.pdf
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Navsoft: AI-Powered Business Solutions & Custom Software Development
wealthsignaloriginal-com-DS-text-... (1).pdf
Digital Strategies for Manufacturing Companies

N_Asm Assembly strings (sol)

  • 1. http://www.tutorialspoint.com/assembly_prog ramming /assembly_string s.htm Copyright © tutorialspoint.com ASSEMBLY - STRING PROCESSING We have already used variable lengths strings inour previous examples. Youmust have noticed that, the variable lengths strings canhave as many characters as required. Generally, we specify the lengthof the string by either of the two ways: Explicitly storing string length Using a sentinelcharacter We canstore the string lengthexplicitly by using the $ locationcounter symbolthat represents the current value of the locationcounter. Inthe following example: msg db 'Hello, world!',0xa ;our dear string len equ $ - msg ;length of our dear string $ points to the byte after the last character of the string variable msg. Therefore, $-msg gives the lengthof the string. We canalso write msg db 'Hello, world!',0xa ;our dear string len equ 13 ;length of our dear string Alternatively, youcanstore strings witha trailing sentinelcharacter to delimit a string instead of storing the string lengthexplicitly. The sentinelcharacter should be a specialcharacter that does not appear withina string. For example: message DB 'I am loving it!', 0 String Instructions Eachstring instructionmay require a source operand, a destinationoperand or both. For 32-bit segments, string instructions use ESI and EDI registers to point to the source and destinationoperands, respectively. For 16-bit segments, however, the SI and the DI registers are used to point to the source and destination, respectively. There are five basic instructions for processing strings. They are: MOVS - This instructionmoves 1 Byte, Word or Doubleword of data frommemory locationto another. LODS - This instructionloads frommemory. If the operand is of one byte, it is loaded into the AL register, if the operand is one word, it is loaded into the AX register and a doubleword is loaded into the EAX register. STOS - This instructionstores data fromregister (AL, AX, or EAX) to memory. CMPS - This instructioncompares two data items inmemory. Data could be of a byte size, word or doubleword. SCAS - This instructioncompares the contents of a register (AL, AX or EAX) withthe contents of anitem inmemory. Eachof the above instructionhas a byte, word and doubleword versionand string instructions canbe repeated by using a repetitionprefix. These instructions use the ES:DI and DS:SI pair of registers, where DI and SI registers containvalid offset addresses that refers to bytes stored inmemory. SI is normally associated withDS (data segment) and DI is always associated withES (extra segment). The DS:SI (or ESI) and ES:DI (or EDI) registers point to the source and destinationoperands, respectively.
  • 2. The source operand is assumed to be at DS:SI (or ESI) and the destinationoperand at ES:DI (or EDI) in memory. For 16-bit addresses, the SI and DI registers are used, and for 32-bit addresses, the ESI and EDI registers are used. The following table provides various versions of string instructions and the assumed space of the operands. Basic Instruction Operands at Byte Operation Word Operation Double word Operation MOVS ES:DI, DS:EI MOVSB MOVSW MOVSD LODS AX, DS:SI LODSB LODSW LODSD STOS ES:DI, AX STOSB STOSW STOSD CMPS DS:SI, ES: DI CMPSB CMPSW CMPSD SCAS ES:DI, AX SCASB SCASW SCASD Repetition Prefixes The REP prefix, whenset before a string instruction, for example - REP MOVSB, causes repetitionof the instructionbased ona counter placed at the CX register. REP executes the instruction, decreases CX by 1, and checks whether CX is zero. It repeats the instructionprocessing untilCX is zero. The DirectionFlag (DF) determines the directionof the operation. Use CLD (Clear DirectionFlag, DF = 0) to make the operationleft to right. Use STD (Set DirectionFlag, DF = 1) to make the operationright to left. The REP prefix also has the following variations: REP: it is the unconditionalrepeat. It repeats the operationuntilCX is zero. REPE or REPZ: It is conditionalrepeat. It repeats the operationwhile the zero flag indicates equal/zero. It stops whenthe ZF indicates not equal/zero or whenCX is zero. REPNE or REPNZ: It is also conditionalrepeat. It repeats the operationwhile the zero flag indicates not equal/zero. It stops whenthe ZF indicates equal/zero or whenCX is decremented to zero.