Printing a String in 8086 Assembly Language

 Introduction

    Many universities require us to take Computer Architecture as part of the requirements of a computer-related course.  One of the best ways to learn the architecture and organization of the computer is dive into the low-level design.  As part of the architecture, we deal with ISA or the Instruction Set Architecture.  What better way to learn this than Assembly Language!

    In this blog post, I will discuss how to display string in 8086 Assembly Language so prepare your emulator and start coding. I will be using service 09 of the DOS INT 21h.  For those who are new in Assembly Language, service 09 is a request from the operating system's list of basic tasks.  When you invoke (or use) this service, you are asking the OS to print something.  INT 21h on the other hand is an interrupt.  It causes your main program to pause (or be interrupted) and let the system jump to another code (of the OS's) as specified by the service.  It is like saying "go, execute that code!".  The that in this context is service 09.


Printing a String in Assembly Language

PREREQUISITE:
    There are two prerequisites of this service.
    1.) A predefined string in the data segment
    2.) A $ as sentinel or terminator of your string.

    I will be using simplified directives in my program to make the code shorter.  I am using emu8086 which can be downloaded here in encoding the program below. Here it goes:


.model small  ;simplified directive
.stack            ;simplified directive
.data             ;simplified directive
    sampleString db "This is a sample string in Assembly Language","$"
.code             ;simplified directive
    mov ax,@data
    mov ds,ax

        mov ah,9
        lea dx, sampleString
        int 21h

    mov ah,4ch
    int 21h
END


OUTPUT:

output of the program: String Display


DISECTING THE CODE:

First line: the model tells the assembler that you would like to use one segment for the code and one segment for the data; one segment is 64KB of space in the primary memory.

Second line: this code tells the computer you will be using the default size for your stack which is 64KB

Third line: anything below .data is part of the data segment, this contains defined variables

Fourth line: my string variable, it always follows the format <variable name> <directive or data size> <initial value>, note that it is terminated by a "$" as a requirement of service 9; should you use another service to print it, there is no need for the "$"

Fifth line anything below .code is part of the code segment; this contains instructions that will be recoded by the assembler into machine instructions

Sixth and seventh lines initialization of the data segment; it is like saying to the compiler that the defined data segment should be accessible to the code segment

Eighth line invoking the service, sometimes you see just 9 or sometimes 09h; this will not matter as they are all the same (9=09=09hex)

Ninth line lea means load effective address; a variable is a pointer in the memory (RAM); every location in the memory has an address; that address have to be loaded into the DX register; this will be used by the service 09 as starting point of accessing what to print to the monitor; after printing the character, it moves to the next one until such time that it encounters a $; then it will stop printing; hence $ is a prerequisite

tenth line commit the request or service call

eleventh and twelfth lines: these two lines are used to request a command prompt; in the event that you are using a command line to assemble and execute the program, you can return to the command prompt because of these two lines

thirteenth line: marks the end of the assembly program 


I hope this helps you in Assembly Language programming.  Read on to my other posts.





 
 

Comments

Popular posts from this blog

My idea of an SIT Day for 2013

SIT Amazing Race in Bicol (part 3 of 3)