StringReplaceCharEx function

The StringReplaceCharEx function replaces all occurrences of the specified character in a string with another character. It keeps the pointer on the stack and in AX for nested calls. The function automatically aligns the stack to 4 bytes.

Parameters (stack): 1. Pointer to the string. 2. Character to be replaced. 3. Replacement character.

Return value: AX: pointer to the modified string (it also remains on the stack).

Flag state: The function does not modify the flags.

Program source code

Intel 8086 assembly program example demonstrating the use of the StringReplaceCharEx function to replace characters in a string.

title Author: Evgeny Sergeevich Dovgopolov

.286C
.model small
.stack 100h

data segment public
    string db "Learning assembly language is fun!\0"
data ends

code segment public
    assume  ds:data, cs:code
    include Handyfar.inc             ; Include the file with functions
    include Handymac.inc             ; Include the file with macros

    main proc
        call    SetDataSegment       ; Call the SetDataSegment procedure

        push    '_'                  ; 3rd: Push the ASCII value of the '_' character onto the stack
        push    ' '                  ; 2nd: Push the ASCII value of the ' ' character onto the stack
        pushs   ds:[string]          ; 1st: Push the address of variable string onto the stack
        call    StringReplaceCharEx  ; Call procedure StringReplaceCharEx(string, ' ', '_')
        call    WriteStringEx        ; Call procedure WriteStringEx(value from the stack)

        push    0                    ; Push the byte return code onto the top of the stack
        call    ExitProcessEx        ; Call the ExitProcessEx(0) procedure
    main endp

code ends
end main

Learning_assembly_language_is_fun!

Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".