SlideShare a Scribd company logo
4
Most read
13
Most read
14
Most read
ARRAYS, STRINGS AND
LOOPS
Assembly Language
Compiled By: Engr. Asim Aziz Waqas
Lacturer CS Department
Umt, Lahore
CHAPTER: ARRAYS, STRINGS, AND LOOPS IN
ASSEMBLY LANGUAGE
 Chapter: Arrays, Strings, and Loops in Assembly Language
 Assembly language is a low-level programming language that
provides a closer view of the computer’s architecture and
operation. This chapter explores three fundamental concepts:
Arrays, Strings, and Loops. Understanding these topics is crucial
for performing efficient computations and managing data in
assembly programs.
An array is a collection of data elements of the same
type stored sequentially in memory. Arrays are
invaluable when dealing with a series of related data
items, such as a list of numbers or characters. In
assembly, arrays are represented by allocating
contiguous memory and accessing individual elements
using their offset from the base address.
Declaring an Array
Arrays are declared by reserving memory using directives such
as DB (define byte), DW (define word), or DD (define
doubleword).
For example:
Declaring an array of bytes
array DB 10, 20, 30, 40, 50
; Declaring an array of words
word_array DW 1000h, 2000h, 3000h
 Accessing Array Elements
 Elements in an array are accessed using their base address and an offset. For example
 MOV SI, OFFSET array
 ; Load base address of array;
 The `OFFSET` directive is used to retrieve the starting memory address of the array.
 This address is then loaded into the SI register, enabling indexed access to the array
elements.
 `OFFSET`, the assembler would not interpret the label as a memory address.
 MOV AL, [SI]
 ; Access the first element
 MOV AL, [SI+2] ; Access the third element (assuming byte elements)
When accessing elements, the offset is calculated as
index * size_of_element.
For words, each element occupies 2 bytes,
so the offset for the second element would be index * 2.
example, if the base address of the array is 1000h and you want to access the second element
(index = 1), the effective address would be
1000h + (1 * 2) = 1002h.
For byte arrays, the offset is calculated as index * 1, so accessing the second byte would yield
1000h + (1 * 1) = 1001h.
Similarly, for doublewords (4 bytes), accessing the second element would compute as
1000h + (1 * 4) = 1004h.
This approach ensures precise element access in memory for different data types.
Strings in Assembly Language
Strings in assembly are a sequence of characters stored in
memory. Each character is typically represented by a single
byte in ASCII format. Strings are used extensively in text
processing applications
Declaring a StringStrings are defined similarly to arrays but often
end with a null terminator (0) to
message DB "Hello, World!", 0
; Null-terminated string signify the end of the string.
 Processing Strings
 Processing strings involves iterating through each character and performing desired
operations such as copying, comparing, or displaying characters. For example:
 MOV SI, OFFSET message ; Load address of string
 process_string:
 MOV AL, [SI] ; Load current character
 CMP AL, 0 ; Check for null terminator
 JE end_process ; Exit if end of string ;
 Process character (e.g., print it)
 INC SI ; Move to the next character
 JMP process_string ; Repeat for the next character
 end_process:
 Assembly Code (x86, DOS)asm Copy code
 .model small
 .stack 100h
 .data
 sourceString db 'Hello, World!', 0 ; Source string (null-terminated)
 destinationString db 20 dup(0) ; Destination string (space for 20 characters); dup(0): The dup
operator is used to initialize multiple memory locations with the same value. Here, it initializes each of
the 20 bytes with the value 0 (null byte)
 .code
 main:
 ; Initialize data segment
 mov ax, @data
 mov ds, ax
 ; Set up the registers for the loop
 lea si, sourceString ; SI points to source string ; lea (Load Effective Address) instruction is used to load
the addresses of sourceString
 lea di, destinationString ; DI points to destination string
 CopyLoop:
 ; Load byte from source string into AL
 mov al, [si] ; AL = *SI (current character from source)
 ; Check if it's the null terminator
 cmp al, 0 ; Compare AL with 0 (null terminator)
 je endCopy ; If null terminator,
 end copying
 ; Copy character to destination
 mov [di], al ; Store AL (character) into [DI] (destination)
 ; Increment source and destination pointers
 inc si ; Move SI to next character of source
 inc di ; Move DI to next position in destination
 ; Repeat the
 jmp copyLoop
 endCopy:
 ; Add a null terminator to the destination string
 mov byte ptr [di], 0 ; Ensure destination string is null-terminated; “byte ptr” specifies that
we are dealing with a single byte of data
 mov ax, 4C00h ; DOS interrupt to exit
 int 21h
 end main
 Loops in Assembly Language
 Loops allow repeated execution of a set of instructions. Assembly provides labels and
loop instructions to facilitate iteration. Loops are fundamental in tasks such as array
traversal and string manipulation.
 Labels and Basic Loop Structure
 Labels mark locations in the code and act as jump to targets. Combined with
conditional or unconditional jumps, they enable looping.
 Basic Loop Structure
 MOV CX, 5 ; Set loop counter
 loop_start:
 ; Perform operations
 DEC CX ; Decrement counter
 JNZ loop_start ; Repeat if CX is not zero
 How LOOP Instruction Works
 The LOOP Instruction The LOOP instruction simplifies counter-
based loops. It automatically decrements CX and jumps to a
specified label if CX is not zero.
 MOV CX, 5 ; Set loop counter
 loop_label:
 ; Perform operations
 LOOP loop_label ; Decrement CX and loop if not zero
 Nested Loops
 Assembly also supports nested loops, where one loop is contained within another. This is
useful for multi-dimensional array processing.
 MOV CX, outer_count
 outer_loop:
 MOV DX, inner_count
 inner_loop:
 ; Perform operations
 LOOP inner_loop
 DEC CX
 JNZ outer_loop
 Combining Arrays, Strings, and Loops
 The true power of assembly programming emerges when combining arrays, strings, and
loops. For example, consider finding the maximum value in an array:
 MOV SI, OFFSET array ; Load base address
 MOV CX, 5 ; Number of elements
 MOV AL, [SI] ; Initialize max with first element
 INC SI
 Dec_elements:
 CMP AL, [SI] ; Compare current max with next element
 JAE skip_update ; Skip if AL >= [SI]
 MOV AL, [SI] ; Update max
 skip_update:
 INC SI ; Move to the next element
 LOOP dec_elements ; Repeat for all elements

More Related Content

PDF
N_Asm Assembly strings (sol)
PPTX
[ASM]Lab8
PDF
220 runtime environments
PPTX
presentation on important DAG,TRIE,Hashing.pptx
PPT
Al2ed chapter4
PPT
Instruction set
N_Asm Assembly strings (sol)
[ASM]Lab8
220 runtime environments
presentation on important DAG,TRIE,Hashing.pptx
Al2ed chapter4
Instruction set

Similar to Arrays, Strings & Loops in assembly Language.pptx (20)

PPT
Sql server lab_2
PPTX
Lecture 7
PPTX
String-Instructions-in-Assembly-Language.pptx
PDF
INTERMEDIATE CODE GENERTION-CD UNIT-3.pdf
PDF
Topic 6 - Programming in Assembly Language_230517_115118.pdf
PPTX
X86 operation types
PPT
Code Generations - 1 compiler design.ppt
PPTX
Chapter 3 programming concepts-ii
DOCX
Unitii classnotes
POT
Arrays and addressing modes
PPTX
instructionsetsofjdtufgmictfgfjh8086.pptx
PPTX
Co&al lecture-08
ODP
PHP Web Programming
PPT
COMPILER_DESIGN_CLASS 2.ppt
PPTX
COMPILER_DESIGN_CLASS 1.pptx
PPTX
Reversing & Malware Analysis Training Part 4 - Assembly Programming Basics
PDF
Compiler unit 5
PPTX
[ASM]Lab4
Sql server lab_2
Lecture 7
String-Instructions-in-Assembly-Language.pptx
INTERMEDIATE CODE GENERTION-CD UNIT-3.pdf
Topic 6 - Programming in Assembly Language_230517_115118.pdf
X86 operation types
Code Generations - 1 compiler design.ppt
Chapter 3 programming concepts-ii
Unitii classnotes
Arrays and addressing modes
instructionsetsofjdtufgmictfgfjh8086.pptx
Co&al lecture-08
PHP Web Programming
COMPILER_DESIGN_CLASS 2.ppt
COMPILER_DESIGN_CLASS 1.pptx
Reversing & Malware Analysis Training Part 4 - Assembly Programming Basics
Compiler unit 5
[ASM]Lab4
Ad

Recently uploaded (20)

PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
Construction Project Organization Group 2.pptx
PPT
Project quality management in manufacturing
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
web development for engineering and engineering
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
Lecture Notes Electrical Wiring System Components
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
Digital Logic Computer Design lecture notes
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPT
Mechanical Engineering MATERIALS Selection
Model Code of Practice - Construction Work - 21102022 .pdf
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Construction Project Organization Group 2.pptx
Project quality management in manufacturing
Operating System & Kernel Study Guide-1 - converted.pdf
web development for engineering and engineering
Embodied AI: Ushering in the Next Era of Intelligent Systems
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Internet of Things (IOT) - A guide to understanding
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Automation-in-Manufacturing-Chapter-Introduction.pdf
Lecture Notes Electrical Wiring System Components
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Digital Logic Computer Design lecture notes
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
R24 SURVEYING LAB MANUAL for civil enggi
Mechanical Engineering MATERIALS Selection
Ad

Arrays, Strings & Loops in assembly Language.pptx

  • 1. ARRAYS, STRINGS AND LOOPS Assembly Language Compiled By: Engr. Asim Aziz Waqas Lacturer CS Department Umt, Lahore
  • 2. CHAPTER: ARRAYS, STRINGS, AND LOOPS IN ASSEMBLY LANGUAGE  Chapter: Arrays, Strings, and Loops in Assembly Language  Assembly language is a low-level programming language that provides a closer view of the computer’s architecture and operation. This chapter explores three fundamental concepts: Arrays, Strings, and Loops. Understanding these topics is crucial for performing efficient computations and managing data in assembly programs.
  • 3. An array is a collection of data elements of the same type stored sequentially in memory. Arrays are invaluable when dealing with a series of related data items, such as a list of numbers or characters. In assembly, arrays are represented by allocating contiguous memory and accessing individual elements using their offset from the base address.
  • 4. Declaring an Array Arrays are declared by reserving memory using directives such as DB (define byte), DW (define word), or DD (define doubleword). For example: Declaring an array of bytes array DB 10, 20, 30, 40, 50 ; Declaring an array of words word_array DW 1000h, 2000h, 3000h
  • 5.  Accessing Array Elements  Elements in an array are accessed using their base address and an offset. For example  MOV SI, OFFSET array  ; Load base address of array;  The `OFFSET` directive is used to retrieve the starting memory address of the array.  This address is then loaded into the SI register, enabling indexed access to the array elements.  `OFFSET`, the assembler would not interpret the label as a memory address.  MOV AL, [SI]  ; Access the first element  MOV AL, [SI+2] ; Access the third element (assuming byte elements)
  • 6. When accessing elements, the offset is calculated as index * size_of_element. For words, each element occupies 2 bytes, so the offset for the second element would be index * 2. example, if the base address of the array is 1000h and you want to access the second element (index = 1), the effective address would be 1000h + (1 * 2) = 1002h. For byte arrays, the offset is calculated as index * 1, so accessing the second byte would yield 1000h + (1 * 1) = 1001h. Similarly, for doublewords (4 bytes), accessing the second element would compute as 1000h + (1 * 4) = 1004h. This approach ensures precise element access in memory for different data types.
  • 7. Strings in Assembly Language Strings in assembly are a sequence of characters stored in memory. Each character is typically represented by a single byte in ASCII format. Strings are used extensively in text processing applications Declaring a StringStrings are defined similarly to arrays but often end with a null terminator (0) to message DB "Hello, World!", 0 ; Null-terminated string signify the end of the string.
  • 8.  Processing Strings  Processing strings involves iterating through each character and performing desired operations such as copying, comparing, or displaying characters. For example:  MOV SI, OFFSET message ; Load address of string  process_string:  MOV AL, [SI] ; Load current character  CMP AL, 0 ; Check for null terminator  JE end_process ; Exit if end of string ;  Process character (e.g., print it)  INC SI ; Move to the next character  JMP process_string ; Repeat for the next character  end_process:
  • 9.  Assembly Code (x86, DOS)asm Copy code  .model small  .stack 100h  .data  sourceString db 'Hello, World!', 0 ; Source string (null-terminated)  destinationString db 20 dup(0) ; Destination string (space for 20 characters); dup(0): The dup operator is used to initialize multiple memory locations with the same value. Here, it initializes each of the 20 bytes with the value 0 (null byte)  .code  main:  ; Initialize data segment  mov ax, @data  mov ds, ax  ; Set up the registers for the loop  lea si, sourceString ; SI points to source string ; lea (Load Effective Address) instruction is used to load the addresses of sourceString  lea di, destinationString ; DI points to destination string
  • 10.  CopyLoop:  ; Load byte from source string into AL  mov al, [si] ; AL = *SI (current character from source)  ; Check if it's the null terminator  cmp al, 0 ; Compare AL with 0 (null terminator)  je endCopy ; If null terminator,  end copying  ; Copy character to destination  mov [di], al ; Store AL (character) into [DI] (destination)  ; Increment source and destination pointers  inc si ; Move SI to next character of source  inc di ; Move DI to next position in destination  ; Repeat the  jmp copyLoop
  • 11.  endCopy:  ; Add a null terminator to the destination string  mov byte ptr [di], 0 ; Ensure destination string is null-terminated; “byte ptr” specifies that we are dealing with a single byte of data  mov ax, 4C00h ; DOS interrupt to exit  int 21h  end main
  • 12.  Loops in Assembly Language  Loops allow repeated execution of a set of instructions. Assembly provides labels and loop instructions to facilitate iteration. Loops are fundamental in tasks such as array traversal and string manipulation.  Labels and Basic Loop Structure  Labels mark locations in the code and act as jump to targets. Combined with conditional or unconditional jumps, they enable looping.
  • 13.  Basic Loop Structure  MOV CX, 5 ; Set loop counter  loop_start:  ; Perform operations  DEC CX ; Decrement counter  JNZ loop_start ; Repeat if CX is not zero
  • 14.  How LOOP Instruction Works  The LOOP Instruction The LOOP instruction simplifies counter- based loops. It automatically decrements CX and jumps to a specified label if CX is not zero.  MOV CX, 5 ; Set loop counter  loop_label:  ; Perform operations  LOOP loop_label ; Decrement CX and loop if not zero
  • 15.  Nested Loops  Assembly also supports nested loops, where one loop is contained within another. This is useful for multi-dimensional array processing.  MOV CX, outer_count  outer_loop:  MOV DX, inner_count  inner_loop:  ; Perform operations  LOOP inner_loop  DEC CX  JNZ outer_loop
  • 16.  Combining Arrays, Strings, and Loops  The true power of assembly programming emerges when combining arrays, strings, and loops. For example, consider finding the maximum value in an array:  MOV SI, OFFSET array ; Load base address  MOV CX, 5 ; Number of elements  MOV AL, [SI] ; Initialize max with first element  INC SI  Dec_elements:  CMP AL, [SI] ; Compare current max with next element  JAE skip_update ; Skip if AL >= [SI]  MOV AL, [SI] ; Update max  skip_update:  INC SI ; Move to the next element  LOOP dec_elements ; Repeat for all elements