StringLengthEx function

The StringLengthEx function computes the length of a null-terminated string. The pointer to the string is passed through the stack. The function automatically aligns the stack to 2 bytes.

Parameters (stack): 1. Pointer to the string.

Return value: AX: string length.

Flag state: The function does not modify the flags.

Program source code

Intel 8086 assembly program example demonstrating the use of the StringLengthEx function to compute the length of a string whose address is passed through the stack.

title Author: Evgeny Sergeevich Dovgopolov

.286C
.model small
.stack 100h

data segment public
    string db "I can speak Chinese\0"
    fmtout db "String length: '%s' = %d characters.\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

        pushs   ds:[string]        ; Push the address of variable string onto the stack
        call    StringLengthEx     ; Call procedure StringLengthEx(string) := AX

        push    ax                 ; 3rd: Push the ax register value onto the stack
        pushs   ds:[string]        ; 2nd: Push the address of variable string onto the stack
        call    ToUpperStringEx    ; Call procedure ToUpperStringEx(string)
        leaax   ds:[fmtout]        ; 1st: Load the address of the fmtout format string into ds:ax
        call    PrintFormatString  ; Call PrintFormatString(fmtout, ToUpperStringEx(string), ax)
        add     sp, 4              ; Align 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

String length: 'I CAN SPEAK CHINESE' = 19 characters.

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