← Back to Home

Practical 8

Write a assembly program to calculate the length of String.

Theory

  1. How do you declare a string in assembly language using the DB directive?
  2. What is the purpose of the '$' character at the end of the string 'COMPUTERS$'?
  3. How does the program use the SI register to traverse through the string?
  4. What is the function of the CMP instruction when checking for the end of string?
  5. How does the program count the length of the string using the length variable?

Code

Create a file named exp8.c in Notepad and save it in D:\TASM:

.model small         
.stack 100h
.data
    str_s db 'COMPUTERS$'
    length db 0
    msg db 0Dh,0Ah,'Length of string = $'
.code
main proc
    mov ax, @data
    mov ds, ax
    mov si, offset str_s

next: 
    mov al, [si]
    cmp al, '$'
    je show
    inc si
    inc length
    jmp next

show:
    lea dx, msg
    mov ah, 09h
    int 21h             ; Print message

    mov al, length
    add al, 30h         ; Convert to ASCII
    mov dl, al
    mov ah, 02h
    int 21h             ; Print the length digit

    mov ah,4ch
    int 21h
main endp
end main

    

Compilation and Execution using TASM

Inside DOSBox, compile and run:

    tasm filename.asm
    tlink filename.obj
    

Using Debug Tool

Run Debug:

td filename.exe