Programs Involving String manipulation

Programs Involving String manipulation:

 Write an ALP to transfer of a string in forward direction.

 Write an ALP to reverse string.




1. Write an ALP to transfer of a string in forward direction.


code:

name "String Copy with Input"


org 100h


.data

    prompt db 'Enter a string: $'

    source db 100 dup(0) ; Source buffer (100 bytes)

    dest db 100 dup(0)   ; Destination buffer (100 bytes)

    newline db 0Dh, 0Ah, '$' ; Newline characters

    buffer db 100, 0     ; Buffer for input (100 bytes) and length byte


.code

start:

    ; Print the prompt

    mov ah, 09h

    lea dx, prompt

    int 21h


    ; Read the input string

    mov ah, 0Ah       ; DOS buffered input

    lea dx, buffer    ; Address of input buffer

    int 21h


    ; Copy the input string to source buffer

    lea si, buffer + 2 ; Skip the first byte (length byte) and first character (buffer size byte)

    lea di, source     ; Destination buffer

    mov cl, buffer[1]  ; Length of the input string

    mov ch, 0          ; Set CH to 0 (making CX = length)

copy_input:

    movsb              ; Move byte from [SI] to [DI], increment SI and DI

    loop copy_input    ; Loop until CX = 0


    ; Copy the string from source to destination

    lea si, source    ; Source index

    lea di, dest      ; Destination index


    ; Find the length of the input string

    mov cx, 0

find_length:

    lodsb              ; Load byte at [si] into AL, increment SI

    cmp al, 0          ; Check for null terminator

    je copy_string     ; If null terminator, end the loop

    inc cx             ; Increment length counter

    jmp find_length    ; Repeat until null terminator


copy_string:

    lea si, source    ; Reset source index

    lea di, dest      ; Reset destination index


    mov ch, 0         ; Clear CH

    cld               ; Clear direction flag

    rep movsb         ; Copy CX bytes from source to destination


    ; Print the newline

    mov ah, 09h

    lea dx, newline

    int 21h


    ; Print the copied string

    mov ah, 09h

    lea dx, dest

    int 21h


    ; Terminate the program

    mov ah, 4Ch

    int 21h


.stack 100h

Output:


Explaination:

. Here's a breakdown of the code:

Data Segment (.data):

  • prompt: This string contains the message to be displayed to the user asking for input.
  • source: This buffer is 100 bytes long and will hold the copied string from the user.
  • dest: This buffer is also 100 bytes long and will hold the final copied string with a null terminator.
  • newline: This string contains a newline character followed by a dollar sign ($) for better formatting.
  • buffer: This buffer is 101 bytes long. The first byte will store the length of the string entered by the user, and the rest will hold the actual string.

Code Segment (.code):

  • start: This label marks the beginning of the program.
  • Print the prompt:
    • mov ah, 09h: This sets the AH register to 09h, which is the DOS function call for printing a string.
    • lea dx, prompt: This loads the address of the prompt string into the DX register.
    • int 21h: This interrupts the CPU with function 21h, which calls the DOS function pointed to by AH. In this case, it prints the string in DX.
  • Read the input string:
    • mov ah, 0Ah: This sets AH to 0Ah for the DOS function to read a string with buffering.
    • lea dx, buffer: This loads the address of the buffer into DX, where the user's input will be stored.
    • int 21h: Again, this interrupts the CPU to read a string from the keyboard and store it in the buffer pointed to by DX.
  • Copy the input string to source buffer:
    • lea si, buffer + 2: This sets the source index (SI) to point to the second byte of the buffer, skipping the first byte (length) and the size byte.
    • lea di, source: This sets the destination index (DI) to point to the beginning of the source buffer.
    • mov cl, buffer[1]: This moves the length of the string (stored in the second byte of buffer) into the CL register.
    • mov ch, 0: This sets the CH register to 0. This is a trick to make the CX register (combination of CL and CH) represent the length of the string.
    • copy_input: This label marks the beginning of a loop.
    • movsb: This instruction copies a byte from the memory location pointed to by SI to the memory location pointed to by DI. It then increments both SI and DI for the next iteration.
    • loop copy_input: This instruction loops back to copy_input as long as the CX register (length) is not zero.
  • Copy the string from source to destination (with null termination):
    • lea si, source: This resets the source index (SI) to point to the beginning of the source buffer.
    • lea di, dest: This resets the destination index (DI) to point to the beginning of the dest buffer.
  • Find the length of the input string:
    • mov cx, 0: This initializes the CX register (used for counting) to zero.
    • find_length: This label marks the beginning of a loop.
    • lodsb: This instruction loads a byte from the memory location pointed to by SI into the AL register and increments SI.
    • cmp al, 0: This compares the value in AL (the loaded byte) with zero.
    • je copy_string: This jumps to the copy_string label if the byte in AL is zero (null terminator).
    • inc cx: This increments the CX register to count the non-null characters.
    • jmp find_length: This jumps back to find_length to continue iterating until the null terminator is found.
  • Copy the string from source to destination (fixed length):
    • lea si, source: This resets the source index (SI) again.
    • lea di, dest: This resets the destination index (DI) again.
    • mov ch, 0: This clears the CH register (important for using REP instruction).
    • cld: This clears the direction flag (important for string operations)
    • rep movsb: This instruction uses the CX register (set to the length in the previous step) to repeat the movsb instruction that copies a byte from source to destination. REP stands for "repeat" and uses CX as the counter.
  • Print the newline: This section is similar to printing the prompt, using DOS function 09h to print the newline string.
  • Print the copied string: This section again uses DOS function 09h to print the final string stored in the dest buffer.
  • Terminate the program: This uses DOS function 4Ch (exit) to terminate the program.

Key Points:

  • The program uses two buffers: buffer to store the user's input with its length, and source to hold a temporary copy of the input string.
  • The final string is copied to dest with a null terminator added using a separate loop to find the length.
  • The program utilizes DOS functions for input and output operations.

I hope this explanation clarifies the code's functionality. Feel free to ask if you have any further questions about specific parts of the code!


2.Write an ALP to reverse string.


code:

name "Reverse String"


org 100h


.data

    prompt db 'Enter a string: $'

    original_msg db 'Original string: $'

    reversed_msg db 'Reversed string: $'

    source db 100 dup(0)    ; Source buffer (100 bytes)

    reversed db 100 dup(0)  ; Reversed buffer (100 bytes)

    newline db 0Dh, 0Ah, '$' ; Newline characters

    buffer db 100, 0        ; Buffer for input (100 bytes) and length byte


.code

start:

    ; Print the prompt

    mov ah, 09h

    lea dx, prompt

    int 21h


    ; Read the input string

    mov ah, 0Ah       ; DOS buffered input

    lea dx, buffer    ; Address of input buffer

    int 21h


    ; Copy the input string to source buffer

    lea si, buffer + 2 ; Skip the first byte (length byte) and the second byte (buffer size byte)

    lea di, source     ; Destination buffer

    mov cl, [buffer + 1]  ; Length of the input string

    mov ch, 0          ; Set CH to 0 (making CX = length)

copy_input:

    movsb              ; Move byte from [SI] to [DI], increment SI and DI

    loop copy_input    ; Loop until CX = 0


    ; Print the original string message

    mov ah, 09h

    lea dx, newline

    int 21h

    lea dx, original_msg

    int 21h


    ; Print the original string

    lea dx, source

    int 21h


    ; Reverse the string

    lea si, source     ; Source index

    lea di, reversed   ; Destination index


    mov cl, [buffer + 1]  ; Length of the input string

    mov ch, 0          ; Set CH to 0 (making CX = length)

    dec cx             ; Adjust CX to be zero-indexed

    add si, cx         ; Point SI to the end of the string in source

reverse_string:

    lodsb              ; Load byte at [SI] into AL, decrement SI

    stosb              ; Store byte in AL at [DI], increment DI

    loop reverse_string; Loop until CX = 0


    ; Print the reversed string message

    mov ah, 09h

    lea dx, newline

    int 21h

    lea dx, reversed_msg

    int 21h


    ; Print the reversed string

    lea dx, reversed

    int 21h


    ; Terminate the program

    mov ah, 4Ch

    int 21h


.stack 100h


Output:


explaination:

Reverse String Explained

This assembly code program, named "Reverse String," prompts the user to enter a string, reverses the string, and then prints both the original and reversed versions. Here's a breakdown of the code:

Data Segment (.data):

  • Similar to the "String Copy with Input" program, this section defines various strings and buffers for storing data.
    • prompt, original_msg, reversed_msg: These strings contain messages displayed to the user.
    • source: This buffer holds the original string entered by the user (100 bytes).
    • reversed: This buffer will hold the reversed string (100 bytes).
    • newline: This string contains a newline character followed by a dollar sign ($) for formatting.
    • buffer: This buffer stores the user's input with its length (similar to the previous program).

Code Segment (.code):

  • The code performs similar steps to the previous program for prompting the user, reading the input, and copying it to the source buffer.

  • Print the original string message and string:

    • This section uses DOS function 09h to print the original_msg string followed by the original string stored in source.
  • Reverse the string:

    • lea si, source: This sets the source index (SI) to point to the beginning of the source buffer.
    • lea di, reversed: This sets the destination index (DI) to point to the beginning of the reversed buffer.
    • mov cl, [buffer + 1]: This retrieves the length of the string from the buffer.
    • mov ch, 0: This sets CH to 0, making CX represent the length.
    • dec cx: This decrements CX by 1 because string loops often use zero-based indexing.
    • add si, cx: This adjusts SI to point to the last character in source (to start reversing from the end).
    • reverse_string: This label marks the beginning of the reversal loop.
    • lodsb: This loads a byte from source (pointed to by SI) into AL and decrements SI to move towards the beginning.
    • stosb: This stores the byte in AL (the reversed character) into reversed (pointed to by DI) and increments DI.
    • loop reverse_string: This loops back to reverse_string as long as CX (adjusted length) is not zero.
  • Print the reversed string message and string:

    • Similar to the original string section, this uses DOS function 09h to print the reversed_msg string followed by the reversed string stored in reversed.
  • Terminate the program: This uses DOS function 4Ch to exit the program.

Key Points:

  • The program reverses the string by iterating from the end of the source string and copying characters in reverse order to the reversed buffer.
  • String indexing adjustment with dec cx and add si, cx is necessary because the loop uses zero-based indexing while the buffer stores the actual length.

I hope this explanation clarifies how the program reverses the user-entered string. Feel free to ask if you have any further questions!




Post a Comment

0 Comments