Sunday, January 12, 2014

Printing a String using x86 assembly under MASM/TASM


String is an array of character, where all character are stored in contiguous fashion. In computer's view, string is an array of bytes stored in contiguous memory. Here, I have used term 'byte' because computer does not recognize integer and character separately. It knows only bytes. 

 
To print a string, we need an array of bytes or character stored in a memory with a 'end of string' character. Let's see how it is done.
Generally,the array are stored in a data segment.For example, 
 
    .data
             char db 'a','b','c','d','$'

Here, 'char' is the name of array variable. These characters will get stored in contiguous memory locations with values 'abcd'. The '$' is end of string character. It recognizes that there are no more characters after it. It is compulsory to have '$' as the end of string characters for all strings. As I already told, computer only knows bytes, following declaration of string is also similar to previous one.

    .data
    char db 41,42,43,44,'$'

Here, 41, 42, 43 & 44 are ASCII values of 'abcd' respectively. In order to print a string on screen x86 architecture uses DOS interrupt number 21H with function number 09H. So,

    INT 21H & AH=09H

will do the task for us.
Before this, we must store offset address/effective address of the string in data register (DX). Address of string or address of first character of string must be stored in DX register, function 09H gets this address from DX and print the characters on the screen from first byte till '$'.
Following code will print 'welcome' on screen.

.DATA
      MESSAGE DB 'WELCOME$'

.CODE
     MOV DX, @DATA
     MOV DS, AX
     LEA DX, MESSAGE
     MOV AH, 09H
     INT 21H

  MOV AH, 4CH
  INT 21H
END

The instruction 'LEA' loads the offset of effective address of variable 'MESSAGE' in DX register. Remember, it is mandatory to have '$' at the end!
You may try by removing '$' from the string.

4 comments: