The StringTrim function
The StringTrim function removes the leading and trailing characters specified in the CL register from a null-terminated string and stores the result in a buffer prepared by the user. If CL = 0, spaces are trimmed by default.
Parameters: DS:AX - source string; ES:BX - destination buffer; CL - character to remove.
Return value: The function does not return a value.
Flag state: The function does not modify the flags.
Program source code (Example 1: spaces)
An example program for the Intel 8086 assembler demonstrating the use of the StringTrim function to remove the specified characters from the beginning and end of a string.
title .model small .stack 100h data segment public string db " Hello World! \0" buffer db 20 dup(0) data ends code segment public ; Start of the code segment assume ds:data, cs:code include Handyfar.inc ; Include the file with functions main proc ; Start of the main procedure call SetDataSegment ; Call the SetDataSegment procedure xor cl, cl ; Clear the cl register - spaces are removed by default lea bx, es:[buffer] ; Load the address of the variable buffer into the es:bx register lea ax, ds:[string] ; Load the address of the variable string into the ds:ax register call StringTrim ; Call the StringTrim procedure (ds:ax, es:bx, cl) lea ax, ds:[buffer] ; Load the address of the variable buffer into the ds:ax register call WriteString ; Call the WriteString procedure (ax:buffer) xor al, al ; Clear the al register call ExitProcess ; Call the ExitProcess procedure main endp ; End of the main procedure code ends ; End of the code segment end main ; End of file. The main procedure is the entry point
Hello World!
Program source code (Example 2: the '-' character)
title .model small .stack 100h data segment public string db "------Hello World!--\0" buffer db 20 dup(0) data ends code segment public ; Start of the code segment assume ds:data, cs:code include Handyfar.inc ; Include the file with functions main proc ; Start of the main procedure call SetDataSegment ; Call the SetDataSegment procedure mov cl, '-' ; Copy the '-' character value into the cl register lea bx, es:[buffer] ; Load the address of the variable buffer into the es:bx register lea ax, ds:[string] ; Load the address of the variable string into the ds:ax register call StringTrim ; Call the StringTrim procedure (ds:ax, es:bx, cl) lea ax, ds:[buffer] ; Load the address of the variable buffer into the ds:ax register call WriteString ; Call the WriteString procedure (ax:buffer) xor al, al ; Clear the al register call ExitProcess ; Call the ExitProcess procedure main endp ; End of the main procedure code ends ; End of the code segment end main ; End of file. The main procedure is the entry point
Hello World!
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".