Saturday 22 December 2012

Input and output in 8086 Assembly Language


Character Output




The task here is to display a single character on the screen. There are three elements involved in carrying out this operation using the int instruction:



1. We specify the character to be displayed. This is done by storing the character’s ASCII code in a specific 8086 register. In this case we use the dl register, i.e. we use dl to pass a parameter to the output subprogram.



2. We specify which of MS-DOS’s I/O subprograms we wish to use. The subprogram to display a character is subprogram number 2h. This number is stored in the ah register.



3. We request MS-DOS to carry out the I/O operation using the int instruction. This means that we interrupt our program and transfer control to the MS-DOS subprogram that we have specified using the ah register.





Example 1: Write a code fragment to display the character ’a’ on the screen:

mov dl, ‘a‘ ; dl = ‘a‘

mov ah, 2h ; character output subprogram
int 21h ; call ms-dos output character


As you can see, this simple task is quite complicated in assembly language.


Character Input

The task here is to read a single character from the keyboard. There are also three elements involved in performing character input:

1. As for character input, we specify which of MS-DOS’s I/O subprograms we wish to use, i.e. the character input from the keyboard subprogram. This is MS-DOS subprogram number 1h. This number must be stored in the ah register.



2. We call MS-DOS to carry out the I/O operation using the int instruction as for character input.



3. The MS-DOS subprogram uses the al register to store the character it reads from the keyboard.


Example 2: Write a code fragment to read a character from the keyboard:



mov ah, 1h ; keyboard input subprogram
int 21h ; character input
; character is stored in al
mov c, al ; copy character from al to c


The following example combines the two previous ones, by reading a character from the keyboard and displaying it.

Example 3: Reading and displaying a character:

mov ah, 1h ; keyboard input subprogram
int 21h ; read character into al
mov dl, al ; copy character to dl
mov ah, 2h ; character output subprogram
int 21h ; display character in dl


String output





The task here is to display a string(Array of characters) on the screen. There are four elements involved in carrying out this operation using the INT instruction:


1.Under the data segment, we define a variable to serve as the starting address for the string, array of characters or ASCII codes ending with '$'.
e.g message DB "Your text goes here",'$'
OR message DB 'Y','o','u','r',' ','t','e','x','t',' ','g','o','e','s',' ','h','e','r','e','$' ;Pretty long huh?
OR message DB 89,111,117,114, ... ,$            ;In ASCII codes.
where
 message is the name of the variable,
 DB means "Define byte",
 '$' indicates end of string, and
 the digits are the ASCII code of the characters.


2. Under the code segment(also explained in next post), enter
MOV AX, @data
MOV DS, AX
to initialize the DS register

This moves the address of your data in memory to register DS.
then, offset the variable to DX for display
i.e MOV DX, OFFSET message 

3. We specify which of MS-DOS’s I/O subprograms we wish to use. The subprogram to display a string is subprogram number 9h. This number is stored in the ah register.

4. We request MS-DOS to carry out the I/O operation using the int instruction. This means that we interrupt our program and transfer control to the MS-DOS subprogram that we have specified using the ah register.


Example 4
.data
notice DB "Hello world",13,10,'$'
.code
start:  MOV AX, @data
  MOV DS, AX

          MOV DX, offset notice   ;Copies notice address to DX for display

          MOV AH, 09H
          INT 21H                      ;Calls MS-DOS to display string

          MOV AH, 4C00H       ;Returns control to MS-DOS

          INT 21H
END start


String input


The task here is to input a string(Array of characters) from the keyboard. There are four elements involved in carrying out this operation using the int instruction:


1.Under the data segment, we define the variable to contain the string.
e.g message DB ?
where
 message is the name of the variable,
 DB means "Define byte",
 ? indicates the memory created should be empty.

2. Under the code segment(also explained in next post), enter
MOV AX, @data
MOV DS, AX
to initialize the DS register

This moves the address of your data in memory to register DS.

then, offset the variable to DX
i.e MOV DX, OFFSET message 

3. We specify which of MS-DOS’s I/O subprograms we wish to use. The subprogram to display a string is subprogram number 3FH. This number is stored in the ah register.

4. We request MS-DOS to carry out the I/O operation using the int instruction. This means that we interrupt our program and transfer control to the MS-DOS subprogram that we have specified using the ah register.


Example 4
.data
value DB ?
.code
start:  MOV AX, @data
        MOV DS, AX

          MOV DX, OFFSET value   ;Copies notice address to DX for display

          MOV AH, 3FH
          INT 21H                      ;Calls MS-DOS to input string

          MOV AH, 4C00H       ;Returns control to MS-DOS

          INT 21H
END start                              ;End of program.

;The program will simply prompt you to enter a string and exits.
;The string will be stored in memory starting from the address of the variable you declared(value).
;Note: in this case, CARRIAGE-RETURN (ASCII code 13) or LINE-FEED (ASCII code 10) indicates the end of the string.


References:
1. "Character output" retrieved from http://www.csi.ucd.ie
2. "Character input" retrieved from http://www.csi.ucd.ie

2 comments:

Unknown said...

Sir can u help me
can u explain mov cl,04h
what does the 04h does basically
in accepting twoo digit nos

Unknown said...

is there any way to input and print the integer??