Você está na página 1de 44

Unit 5

Applications of Assembly Language Programming

Introduction
Assembly Language Programming

Assembly language is machine dependent. ALP requires good knowledge of machine architecture, operating system & prgming principles. Reveals the secret of the computers h/w & s/w. Programs are compact & efficient. Prgms are converted to machine codes by the translator known as assembler.

(1) (2)

(3)
(4)

Using 8086 instructions, an ALP can be implemented in the following methods: Standard coding form Using microsoft assembler (MASM) Using DEBUG program on the IBM PC Using Turbo assembler (TASM)

Basic Format
MEMADDR LABEL MNEMONICS OPERAND COMMENTS

Memory Address : Is used for the address or the offset of a code byte or a data type.
Label : Is a name which represents an address reffered to in a jump or call or any kind of branch operation instructions.

Mnemonics : Symbolic abbreviations for operation codes or comment to the Assembler.


Operands: Contains the registers,memory locations or data as specified in the instructions. Comments : Describes the function of the instructions.

Program Structure

TITLE .. DOS SEG .MODEL .STACK .CODE Main PROC .. Main ENDP DATA .. .. END Main

Program Title

Actual Prgrming Instrcns

Data used as per the Program

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. DOS SEG: it directs the MASM to place the segments in the standard order. .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 r 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.

Programs with an Assembler:


The assemblers used for programs here is 8086 Microsoft Macro assembler for the IBM PC. For different assemblers the assembly language directive may change.

Write an ALP to perform simple unsigned Multiplication. The two 16 bit numbers are 1121H and 1301H.Store the product in the location whose offset adrs is 8100H.

TITLE Multiplication DOS SEG .MODEL Small .STACK 100 H DATA


Num1 DW 1121H Num2 DW 1301H

.CODE Main PROC


MOV AX, num1 MOV BX, num2 MUL AX,BX MOV [8100H],AX MOV AX,4C00H INT 21 H

;Bring the first number to AX ;Bring second ;Multiply the contents of AX and BX ;store the result at 8100H ;Return to DOS

Main ENDP END Main

An ALP to compare two 16 bit numbers stored in the AX and BX registers. If both r equal the increment SI register.

CMP AX, BX JE LAB NOP LAB INC SI

An ALP to add two 16 bit numbers stored in the AX and BX registers. If no carry exists after the addition increment SI register.

ADD AX, BX JNC LAB NOP LAB INC SI

Write a ALP for swapping the values stored in two memory locations(M1) and (M2)

MOV AL,(M1) XCHG (M2),AL MOV (M1),AL INT 20H

Write an ALP to evaluate an expression (a+b)*(c+d), where a,b,c and d are the hexadecimal bytes.

MOV AL,a MOV BL,b ADD Al,BL PUSH AL MOV AL,c MOV BL,d ADD BL,AL POP AL MUL AL,BL

Wap to add two 32bit no.sThe first no. is stored in the locations 8100H,8101H,8102H,8103H.The 2nd 32bit no. is stored in the locations 8104H,8105H,8106H,8107H.Store the result in locations 8108H,8109H,810AH and 810BH.

MOV AX,[8100] MOV BX,[8102] MOV CX,[8104] MOV DX,[8106] ADD AX,CX ADC BX,DX MOV [8108],AX MOV [810A],BX INT 20

Write an ALP to find the greatest number in a given series of 8-bit numbers. The length of the series is stored in a location whose 16 bit offst adrs is 8100H.the series begins from the location whose offset adrs is 8102H.Store the result in the location whose 16-bit offset adrs is 8150H.

TITLE Find the max. in a given series of data DOS SEG .MODEL Small .STACK 100H .DATA

List db 20,45,13,15,04,72

.CODE Main PROC

MOV AX,@data MOV DS, AX MOV SI,8102H MOV AL,0 MOV CX,[8100H]
JNC LAB MOV AL,[SI] LOOP BACK MOV [8150H],AL MOV AX,4C00H INT 21 H

;initialise DS Register
;initialize SI register ;Length of series in CX

BACK CMP AL,[SI]


;is next element>max

LAB

INC SI

;repeat until CX=0 ;return to DOS

Main ENDP

END main

Stack and Subroutine

Stack
Is a section of memory set aside for storing return addresses. It is also used to save the contents of registers for the calling program while a procedure executes. Another use of stack is to hold data or addresses that will b acted upon by a procedure.

The 8086 lets u set aside up to an entire 64 kb of memory as stack. It uses 2 registers-SS and SP. SS-hold the upper 16 bits of the starting address given to the stack segment. SP-hold the offset of the last word written on the stack. 8086 produces the physical address for a stack location by adding the offset contained in the SP register to the SS base adrs represented by the 16 bit number in the SS register.

PUSH & POP


Are 2 instructions that can operate on stack. Push to StackPUSH SRC Pop from stackPOP DST

Subroutines (Procedures)

A subroutine is a set of code that can b branched to and returned from such a way that the code is as if it were inserted at the point from which it is branched to. The branch to a subroutine is referred to as CALL and the corresponding branch back is known as RETURN. The return is always made to the instruction immediately following the call.

CALL Subroutine A . . CALL Subroutine A . .

Subroutine . . .

RET

Subroutines provide the primary means of breaking the code in a program to modules. Requirements of subroutine: 1)A procedure CALL must save the adrs of the next instruction , so that the return will b able to branch back to the proper place in the calling program. 2)The registers r used by the procedure need to b stored before their contents r changed and then restored just before the procedure is exited.

CALL & RET


These are two instructions used with procedure. CALL-not only branches to the indicated adrs but also pushes the return adrs on to the stack. CALL ProcName. RET- it simply pops the return address from the stack.

Sub Routine Eg.


.DATA num1 dw 22 num2 dw 32 result dw 0 .CODE addnum proc mov ax, [num1] mov bx, [num2] add ax, bx mov [result], ax ret addnum endp

start:

mov ax, @data mov ds, ax call addnum ; add num1 and num2 mov ax, 4c00h int 21h END start

Use of ALP for I/O operations


Each microprocessor provides instructions for I/O with the devices that are attached to it, e.g. the keyboard and screen. In assembly language we must have a mechanism to call the operating system to carry out I/O. In addition we must be able to tell the operating system what kind of I/O operation we wish to carry out. e.g. to read a character from the keyboard, to display a character or string on the screen.

In 8086 assembly language, we do not call operating system subprograms by name, instead, we use a software interrupt mechanism;eg : int 21h An interrupt signals the processor to suspend its current activity (i.e. running your program) and to pass control to an interrupt service program. This is done by placing a specific number in a register. The ah register is used to pass this information. For example, the subprogram to display a character is subprogram number 2h.This number must be stored in the ah register.

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 characters 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-DOSs 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

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 output, we specify which of MSDOSs 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 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

Example 3: A program to display the letter a on the screen: TITLE displays the character a on the screen DOS SEG .model small .stack 100h .code start: mov dl, a ; store ascii code of a in dl mov ah, 2h ; ms-dos character output function int 21h ; displays character in dl register mov ax, 4c00h ; return to ms-dos int 21h end start

Example 4: Write a program to read a character from the keyboard and display it on the screen: TITLE read a character and display it .model small .stack 100h .code start: mov ah, 1h ; keyboard input subprogram int 21h ; read character into al mov dl, al mov ah, 2h ; display subprogram int 21h ; display character in dl mov ax, 4c00h ; return to ms-dos int 21h end start

String Output
A string is a list of characters treated as a unit. In programming languages we denote a string constant by using quotation marks, e.g. "Enter first number". In 8086 assembly language, single or double quotes may be used. Defining String Variables The following 3 definitions are equivalent ways of defining a string "abc": version1 db "abc" ; string constant version2 db a, b, c ; character constants version3 db 97, 98, 99 ; ASCII codes

EG:-message db "Hello world", 13, 10, $ The string message contains Hello world followed by Return (ASCII 13), Line-feed (ASCII 10) and the $ character. This method is very useful if we wish to include control characters (such as Return) in a string. We terminate the string with the $ character because there is an MS-DOS subprogram (number 9h) for displaying strings which expects the string to be terminated by the $ character.

In order to display a string we must know where the string begins and ends. The beginning of string is given by obtaining its address using the offset operator. The end of a string may be found by either knowing in advance the length of the string or by storing a special character at the end of the string which acts as a sentinel.

MS-DOS provides subprogram number 9h to display strings which are terminated by the $ character. In order to use it we must: 1. Ensure the string is terminated with the $ character. 2. Specify the string to be displayed by storing its address in the dx register. 3. Specify the string output subprogram by storing 9h in ah. 4. Use int 21h to call MS-DOS to execute subprogram 9h.

Example 5: Write a program to display the message Hello world followed by Return and Line-feed : TITLE Display the message Hello World .model small .stack 100h .data message db Hello World, 13, 10, $ .code start: mov ax, @data mov ds, ax ; copy address of message to dx mov dx, offset message mov ah, 9h ; string output int 21h ; display string mov ax, 4c00h int 21h end start

Use of subprograms & Prompt user to enter a character. TITLE enter a character & display the character entered .model small .stack 100h .data prompt db Enter a character: $ msgout db You entered: $ .code start:

mov ax, @data mov ds, ax ; copy address of prompt to dx mov dx, offset prompt call puts ; display prompt call getc ; read character into al mov bl, al ; save character in bl ;display next message mov dx, offset msgout call puts ; display msgout ; display character read from keyboard mov dl, bl ; copy character to dl call putc

mov ax, 4c00h ; return to ms-dos int 21h ; User defined Subprograms putc: ; display character in dl mov ah, 2h int 21h ret getc: ; read character into al mov ah, 1h int 21h ret puts: ; display string terminated by $ ; dx contains address of string

mov ah, 9h int 21h ret end start

OUTPUT Enter a character: x You entered: x

TABLE PROCESSING

Direct table addressing : Example


Suppose the user enters a numeric month such as 12; write a program to convert it to alphabetic format, ie.December. TITLE Direct table addressing

.MODEL SMALL .ORG 100H .DATA NINE DB MONIN DB


ALFMON DB

9 12
9 DUP (20H), $

MONTAB DB JANUARY ,FEBRUARY ,MARCH DB APRIL ,MAY ,JUNE DB JULY ,AUGUST ,SEPTEMBER DB OCTOBER ,NOVEMBER ,DECEMBER .CODE BEGIN PROC NEAR ;main procedure CALL CONVB ;convert to binary CALL LOCMON ;locate month CALL DISPMON ;display month MOV AX, 4C00H ;exit to dos INT 21H ENDP CONVB PROC MOV AH, MONIN ;set up month MOV AL, MONIN+1 XOR AX, 3030H ;clear ascii CMP AH, 00 ;month 01-09? JZ C20 ;yes, bypass SUB AH, AH ;no, clear AH ADD AL, 10 ;correct for binary C20: RET CONVB ENDP

LOCMON PROC LEA SI,MONTAB DEC AL MUL NINE ADD SI,AX MOVZX CX,NINE CLD LEA DI,ALFMON REP MOVSB RET LOCMON ENDP DISPMON PROC MOV AH,09H LEA DX,ALFMON INT 21H RET DISPMON ENDP END BEGIN

;correct for table ;multiply AL by 9

;initialise 9-char move

;move 9 chars

;request display

Você também pode gostar