Wednesday, May 17, 2023

Chapter 8: String Manipulation and Array Operations in MASM



Introduction:


Importance of string manipulation and array operations in assembly language programming

Overview of string operations and array manipulation techniques in MASM

String Manipulation:


Copying, concatenating, and comparing strings in MASM

Finding the length of a string

Converting strings to uppercase or lowercase

Array Operations:


Creating and initializing arrays in MASM

Accessing and modifying array elements

Performing arithmetic and logical operations on arrays

Example:


assembly

Copy code

.model small

.stack 100h


.data

    source db 'Hello, World!', 0

    destination db 20 dup(0)

    numbers dw 1, 2, 3, 4, 5

    arraySize equ 5


.code

main proc

    mov ax, @data

    mov ds, ax


    ; String manipulation: Copying

    lea si, source

    lea di, destination

    call copyString


    ; String manipulation: Uppercase conversion

    lea di, destination

    call convertToUpper


    ; Array operations: Summing the elements

    lea si, numbers

    mov cx, arraySize

    call sumArray


    mov ah, 4Ch

    int 21h

main endp


; Procedure to copy a string

copyString proc

    push cx

    push si

    push di


    copyLoop:

        lodsb ; Load a byte from the source

        stosb ; Store the byte to the destination

        cmp al, 0 ; Check if end of string

        jne copyLoop ; Repeat until end of string


    pop di

    pop si

    pop cx

    ret

copyString endp


; Procedure to convert a string to uppercase

convertToUpper proc

    push cx

    push si


    convertLoop:

        lodsb ; Load a byte from the source

        cmp al, 'a'

        jb skipConversion

        cmp al, 'z'

        ja skipConversion

        sub al, 32 ; Convert to uppercase

        skipConversion:

        stosb ; Store the byte to the destination

        cmp al, 0 ; Check if end of string

        jne convertLoop ; Repeat until end of string


    pop si

    pop cx

    ret

convertToUpper endp


; Procedure to sum the elements of an array

sumArray proc

    push cx

    push ax

    xor ax, ax ; Initialize sum to 0


    sumLoop:

        add ax, [si] ; Add array element to sum

        add si, 2 ; Move to the next element

        loop sumLoop ; Repeat for all elements


    pop ax

    pop cx

    ret

sumArray endp


end main

In this example, we demonstrate string manipulation and array operations in MASM. We have a source string, source, and a destination string, destination, along with an array of numbers, numbers, and its size, arraySize.


For string manipulation, we have two procedures. The copyString procedure copies the contents of the source string to the destination string using the lodsb and stosb instructions. The convertToUpper procedure converts the characters in the string to uppercase by checking the ASCII values and subtracting 32 from lowercase characters.


For array operations, we have the sumArray procedure that calculates the sum of the elements in the array. It uses the lodsw instruction to load a word from the array, adds it to the sum, and moves to the next element using the add si, 2 instruction.


By understanding string manipulation and array operations in MASM, readers can work with text data and perform calculations on arrays efficiently. They learn how to copy strings, convert cases, find string lengths, and perform operations on arrays. This knowledge enables them to manipulate and process data effectively in their assembly language programs.


No comments:

Post a Comment