Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Friday, January 17, 2014

What is 10,13 in Assembly Language variable declaration?



In many assembly language programs written for x86 architecture, the values 10, 13 are written in data segment declaration.
e.g.

    .data
     message db 10, 13, 'abcd$'

Here the string will be declared and it will have the values declared in front of it. The actual array will be,
message:

0AH
0DH
41H
42H
43H
44H
24H
10         13         'a'          'b'         'c'         'd'         '$'

The respective ASCII characters of these values will be printed till '$'. It is considered as end of the string character. 'a' 'b' 'c' 'd' are printable characters but 10 & 13 are non-printable characters .
In short,they are control characters chart below.(Reference -Wikipedia).


10 is called as LF or Line Feed or new line and 13 is called as CR or Carriage return.
These character are used to control the cursor position. The 10 shifts cursor on new line with same column no. and 13 returns the cursor to its initial position of line that is at start of the line!

So, every time you make use of these control characters as the part of string or any array. It shifts the cursor to new line and at the start of the line.

(Note – the term carriage return (CR) is taken from printer's operation. When printer finishes one line printing, it shifts to new line but it prints head moves to start of new line).

LF & CR are equivalent to \n & \r used is print statement of high level programming languages.

Lets take an Example.

Declare variable as

   .data
      message db 10,13,'Welcome$'

and print using,

    MOV AH, 09H
  LEA DX, MESSAGE
  INT 21H

Now make some changes in declaration as,

   .data
   message db 10, 13, 'Wel', 10, 13, 'come$'

Check the output by printing this message.

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.