Monday 24 December 2012

8086 Assembly language program structure


Program Structure

I won't say there is any defined pattern or structure an assembly language program(ALP) should appear unlike what we have in some high level languages but i'll say this is a very good way to start.

TITLE ……..
.MODEL ...
.STACK
.DATA          ;Begining of data segment.
…………..
…………..
.CODE          ;Begining of code segment.
start:              ;Indicates the beginning of instructions.
......
......

Main PROC  ;Begining of procedure (if neccessary)
……………
…………..
Main ENDP ;End of procedure.

END start         ;End of instruction.

Keywords

TITLE: identifies the program listing title. Any text typed to the right side of the directive is printed at the top of each page in the listing file.
.MODEL: selects a standard memory model for the programs.
.STACK: sets the size of the program stack which may b any size up to 64kb.
.CODE: identifies the part of the program that contains instructions .
PROC: creates a name and a address for the beginning of a procedure.
ENDP: indicates the end of the procedure.
.DATA: all variables pertaining to the program are defined in the area following this directive called data segment.
END: terminates assembly  of the program. Any lines of text placed after this directive is ignored.

Example
Write an Assembly Language Program that uses a procedure to perform simple unsigned Multiplication.
The two 16 bit numbers are 1121H and 1301H.
Store the product in the location whose offset address is 8100H.

TITLE Multiplication

.MODEL Small
.STACK 100H
.DATA
Num1 DW 1121H
Num2 DW 1301H
.CODE
start:

CALL Main ;instruction to call procedure Main

Main PROC ;Beginning of procedure Main
MOV AX, num1 ;Bring the first number to AX
MOV BX, num2 ;Bring second
MUL BX ;Multiply the contents of AX and BX
mov BP, [8100H] ;Moves offset address to Base pointer
MOV [BP],AX ;store the result at 8100H

MOV AX,4C00H ;Return to DOS
INT 21H
Main ENDP ;End of procedure Main

END start

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