SRM UNIVERSITY RAMAPURAM CAMPUS DEPARTMENT OF COMPUTER SCIENCE AND ENGINNEERING SUBJECT : MICROPROCESSORS AND MICROCONTROLLERS
SUBJECT CODE : 15CS205J
SEMESTER
CLASS
HOURS/WEEK : 2 HOURS
: III CSE
EX. NO
NAME OF THE EXPERIMENTS
Sl. No.
Description of experiments
: VI
Assembly Language Programs Using TASM/TASM Program involving Arithmetic Instructions on 16 bit data 1
2
3
4 5
i. Addition & Subtraction ii. Multiplication & Division iii. Factorial of a given number Program involving Data Transfer Instructions on 16 bit data i. Byte and Word data transfer in different addressing modes ii. Block Data Transfer Program involving Bit Manipulation Instructions on 16 bit data -Given data is positive or negative
Implementation of Bubble Sort Algorithm
Program involving String Instructions on 16 bit data i. ii.
Reverse a given string and check whether it is a palindrome String Display using Display Interrupt (Read your name from the keyboard and displays it at a specified location on the screen after the
15CS205J/Microprocessors and Microcontrollers Lab
CSE/SRM
1 | 29
message “What is your name?” You must clear the entire screen before display)
6.
Time display using Interrupt (Read the current time from the system and display it in the standard format on the screen)
.
Basic 8051 programming using C
7
8
9
10
Port Programming
Timer-Counter Programming
Serial Programming
Interrupt Programming
PREPARED BY:
APPROVED BY,
V.PRAVEENA
(HOD / CSE)
INTRODUCTION TO TASM Introduction:
15CS205J/Microprocessors and Microcontrollers Lab
CSE/SRM
2 | 29
The aim of this experiment is to introduce the student to assembly language programming and the use of the tools that he will need throughout the lab experiments. This first experiment let the student use the Dos Debugger and the Microsoft Turbo Assembler (TASM). Editing, Assembling, Linking, Execute up can be done using TASM software Overview: In general, programming of microprocessor usually takes several iterations before the right sequence of machine code instruction is written. The process, however is facilitated using a special program called an “Assembler”. The Assembler allows the user to write alphanumeric instructions. The Assembler, in turn, generates the desired machine instructions from the assembly language instructions. Assembly language programming consists of following steps: Assembling the program: The assembler is used to convert the assembly language instructions to machine code. It is used immediately after writing the Assembly language program. The assembler starts by checking the syntax or validity of the structure of each instruction in the source file .if any errors are found, the assemblers displays a report on these errors along with brief explanation of their nature. However If the program does contain any errors ,the assembler produces an object file that has the same name as the original file but with the “obj” extension Linking the program: The Linker is used convert the object file to an executable file. The executable file is the final set of machine code instructions that can directly be executed by the microprocessor. It is the different than the object file in the sense that it is selfcontained and re-locatable. An object file may represent one segment of a long program. This segment can not operate by itself, and must be integrated with other object files representing the rest of the program ,in order to produce the final selfcontained executable file
STEPS
PROCEDURE
1
Editing
Source file
2
Assembling
Object file
3
Linking
Executable file
4
Execution
Results
15CS205J/Microprocessors and Microcontrollers Lab
CSE/SRM
3 | 29
Executing the program The executable contains the machine language code .it can be loaded in the RAM and executed by the microprocessor simply by typing, from the DOS prompt ,the name of the file followed by the carriage Return Key (Enter Key). If the program produces an output on the screen or sequence of control signals to control a piece of hard ware, the effect should be noticed almost immediately. However, if the program manipulates data in memory, nothing would seem to have happened as a result of executing the program. Procedure to enter a program using TASM software Start ↓ Run ↓ Type CMD ↓ Ok Display shows ↓ C :\> D: (Change to D driven because TASM is in D drive) ↓ Press ENTER ↓ D :\> CD TASM ↓ Press ENTER ↓ D: \TASM> EDIT FILENAME.ASM Example edit sarada.asm ↓ Press ENTER ↓ Then the display shows editor ↓ Type the asm program ↓ Then the save the program (Use Alt+F keys to appear the option window) ↓
15CS205J/Microprocessors and Microcontrollers Lab
CSE/SRM
4 | 29
Exit from editor Using Alt+F keys ↓ Then Display shows D: \TASM> ↓ Enter the name TASM FILENAME.ASM Example ↓ D: \TASM> TASM sarada.asm Then Display shows Errors,(0)Warnings(0) If there is errors correct them ↓ Enter the name Tlink FILENAME.OBJ Example ↓ D: \TASM> TLINK sarada.obj ↓ Then the display shows Turbo Link Version 3.0 ↓ Enter the name TD FILENAME.EXE Example ↓ D: \TASM> TD sarada.exe ↓ Then the display shows Program has no symbol table Choose OK ↓ RUN the Program using F9 Key or Select the RUN Option ↓ See the data in Registers ↓ See the data in Data segment Using Alt+F -View-Dump
Steps to use TASM TASM: 1. 2. 3. 4. 5.
TYPE IN NOTEPAD AND SAVE IT AS FILENAME.ASM(CLICK ALL FILES). EDIT : EDIT FN.ASM TASM FN.ASM LINK FN.OBJ DEBUG FN.EXE
15CS205J/Microprocessors and Microcontrollers Lab
CSE/SRM
5 | 29
8086 PROGRAM: ExNo:1
i) Program involving Arithmetic Instructions on 16 bit dataAddition
AIM: To implement assembly language program for addition of two 16-bit numbers. APPARTUS: TASM Software, P.C. PROGRAM: DATA SEGMENT N1 DW 1234H N2 DW 2134H RES DW ? DATA ENDS CODE SEGMENT ASSUME CS: CODE, DS: DATA START: MOV AX, DATA MOV DS, AX MOV AX, N1 MOV BX, N2 ADD AX, BX MOV RES, AX INT 21H CODE ENDS END START RESULT: AX = 3368h
15CS205J/Microprocessors and Microcontrollers Lab
CSE/SRM
6 | 29
ExNo:1 i)
Program involving Arithmetic Instructions on 16 bit data Subtraction
AIM: To implement assembly language program for subtraction of two 16-bit numbers. APPARTUS: TASM Software, P.C. PROGRAM: DATA SEGMENT N1 DW 4444H N2 DW 2121H RES DW ? DATA ENDS CODE SEGMENT ASSUME CS:CODE, DS:DATA START: MOV AX,DATA MOV DS,AX MOV AX,N1 MOV BX,N2 SUB AX,BX MOV RES,AX INT 21H CODE ENDS END START RESULT: AX = 2323h
15CS205J/Microprocessors and Microcontrollers Lab
CSE/SRM
7 | 29
ExNo:1 ii)
Program involving Arithmetic Instructions on 16 bit data Multiplication
AIM: To implement assembly language program for Multiplication of two 16-bit numbers. APPARTUS: TASM Software, P.C. PROGRAM: ASSUME CS : CODE, DS : DATA CODE SEGMENT MOV AX, DATA MOV DS, AX MOV AX, OPR1 MUL OPR2 MOV RESLW, AX MOV RESHW, DX HLT CODE ENDS DATA SEGMENT OPR1 DW 2000H OPR2 DW 4000H RESLW DW ? RESHW DW ? DATA ENDS END (DX)
Input: OPR1 = 2000H OPR2 = 4000H
Output: RESLW = 0000H (AX) RESHW = 0800H (DX)
15CS205J/Microprocessors and Microcontrollers Lab
CSE/SRM
8 | 29
ExNo:1 ii)
Program involving Arithmetic Instructions on 16 bit data
Division AIM: To implement assembly language program for Division of two 16-bit numbers. APPARTUS: TASM Software, P.C. Program:
ASSUME CS : CODE, DS : DATA CODE SEGMENT MOV AX, DATA MOV DS, AX MOV AX, OPR1 DIV OPR2 MOV RESQ, AL MOV RESR, AH HLT CODE ENDS DATA SEGMENT OPR1 DW 2C58H OPR2 DB 56H RESQ DB ? RESR DB ? DATA ENDS END
Input:
OPR1 = 2C58H (DIVIDEND) OPR2 = 56H (DIVISOR)
Output: RESQ = 84H (AL)
15CS205J/Microprocessors and Microcontrollers Lab
CSE/SRM
RESR = 00H (AH)
9 | 29
ExNo:1 iii) Factorial of a given number AIM: To implement assembly language program to find factorial of a given number APPARTUS: TASM Software, P.C.
Program:
DATA SEGMENT A DB 5 DATA ENDS CODE SEGMENT ASSUME DS:DATA,CS:CODE START: MOV AX,DATA MOV DS,AX MOV AH,00 MOV AL,A L1: DEC A MUL A MOV CL,A CMP CL,01 JNZ L1 MOV AH,4CH INT 21H CODE ENDS END START Input: 5 Output: 0078H
15CS205J/Microprocessors and Microcontrollers Lab
CSE/SRM
10 | 29
ExNo:2 i)
Byte and Word data transfer in different addressing modes
AIM: To implement assembly language program for Byte and Word data transfer in different addressing modes APPARTUS: TASM Software, P.C.
Program:
DATA SEGMENT DATA1 DB 23H DATA2 DW 1234H DATA3 DB 0H DATA4 DW 0H DATA5 DW 2345H,6789H DATA ENDS CODE SEGMENT ASSUME CS:CODE,DS:DATA START: MOV AX,DATA ; //Initialize DS to point to start of the memory MOV DS,AX ; //set aside for storing of data MOV AL,25H ; //copy 25H into 8 bit AL register MOV AX,2345H ;// copy 2345H into 16 bit AX register MOV BX,AX ;// copy the content of AX into BX register(16 bit) MOV CL,AL ;// copy the content of AL into CL register MOV AL,DATA1 ;// copies the byte contents of data segment memory location DATA1 into 8 bit AL MOV AX,DATA2 ;// copies the word contents of data segment memory location DATA2 into 16 bit AX MOV DATA3,AL ;// copies the AL content into the byte contents of data ;// segment memory location DATA3 MOV DATA4,AX ;//copies the AX content into the word contents of ;// data segment memory location DATA4 MOV BX,OFFSET DATA5 ;// The 16 bit offset address of DS memeory location ;// DATA5 is copied into BX MOV AX,[BX] ; //copies the word content of data segment ;// memory location addressed by BX into ;// AX(register indirect addressing) MOV DI,02H ;address element 15CS205J/Microprocessors and Microcontrollers Lab
CSE/SRM
11 | 29
MOV AX,[BX+DI}
; copies the word content of data segment ;memory location addressed by BX+DI into ;AX(base plus indirect addressing) MOV AX,[BX+0002H] ;copies the word content of data segment ;(16 bit) MOV AL,[DI+2] ;register relative addressing MOV AX,[BX+DI+0002H] ;copies the word content of data segment ;memory location addressed by BX+DI+0002H ;into AX(16 bit) MOV AH,4CH ;Exit to DOS with function call 4CH INT 21H CODE ENDS ;Assembler stop reading END START
15CS205J/Microprocessors and Microcontrollers Lab
CSE/SRM
12 | 29
ExNo:2 ii)
Block Data Transfer
AIM: To implement assembly language program for Block Data Transfer APPARTUS: TASM Software, P.C.
Block move (with and with out overlapping) Without overlapping Program:
DATA SEGMENT X DB 01H,02H,03H,04H,05H ; Initialize Data Segments Memory Locations Y DB 05 DUP(0) DATA ENDS CODE SEGMENT ASSUME CS:CODE,DS:DATA START:MOV AX,DATA ; Initialize DS to point to start of the memory MOV DS,AX ; set aside for storing of data MOV CX,05H ; Load counter LEA SI,X+04 ; SI pointer pointed to top of the memory block LEA DI,X+04+03 ; 03 is displacement of over lapping, DI pointed to ;the top of the destination block Before execution 00 00 00 00 00 05 04 03 02 01 After execution 05 04 15CS205J/Microprocessors and Microcontrollers Lab
CSE/SRM
13 | 29
03 02 01 05 04 03 02 01 Y,DI X, SI With Overlapping Program:
DATA SEGMENT X DB 01H,02H,03H,04H,05H ; Initialize Data Segments Memory Locations DATA ENDS CODE SEGMENT ASSUME CS:CODE,DS:DATA START:MOV AX,DATA ; Initialize DS to point to start of the memory MOV DS,AX ; set aside for storing of data MOV CX,05H ; Load counter LEA SI,X+04 ; SI pointer pointed to top of the memory block LEA DI,X+04+03 ; 03 is displacement of over lapping, DI pointed to ;the top of the destination block UP: MOV BL,[SI] ; Move the SI content to BL register MOV [DI],BL ; Move the BL register to content of DI DEC SI ; Update SI and DI DEC DI DEC CX ; Decrement the counter till it becomes zero JNZ UP MOV AH,4CH INT 21H CODE ENDS END START DS Before execution xx xx 15CS205J/Microprocessors and Microcontrollers Lab
CSE/SRM
14 | 29
xx xx xx 05 04 03 02 01 DI SI X DS After execution xx xx 05 04 03 - 02 01 03 02 01 -
15CS205J/Microprocessors and Microcontrollers Lab
CSE/SRM
15 | 29
ExNo:3 Program involving bit manipulation instruction AIM: To implement assembly language program involving bit manipulation instruction If given data is positive or negative APPARTUS: TASM Software, P.C.
Program:
DATA SEGMENT NUM DB 12H MES1 DB 10,13,'DATA IS POSITIVE $' MES2 DB 10,13,'DATA IS NEGATIVE $' DATA ENDS CODE SEGMENT ASSUME CS:CODE,DS:DATA START: MOV AX,DATA MOV DS,AX MOV AL,NUM;Move the Number to AL. ROL AL,1;Perform the rotate left side for 1 bit position. JC NEGA;Check for the negative number. MOV DX,OFFSET MES1 ;Declare it positive. JMP EXIT ;Exit program. NEGA: MOV DX,OFFSET MES2;Declare it negative. EXIT: MOV AH,09H INT 21H MOV AH,4CH INT 21H CODE ENDS END START Output: Data is positive Positive Numbers: 00-7F Negative numbers: 80-FF
15CS205J/Microprocessors and Microcontrollers Lab
CSE/SRM
16 | 29
ExNo:4
Bubble sort using TASM
AIM: To implement assembly language program for Bubble Sort APPARTUS: TASM Software, P.C.
Program: Ascending Order:
DATA SEGMENT arr db 5h,7h,6h,4h,10h,09h len db $-arr DATA ENDS ASSUME CS:CODE , DS:DATA CODE SEGMENT start: mov ax,@data mov ds,ax mov cl,len lp1: mov bx,cx lea si,arr lp2: mov al,[si] inc si cmp [si],al jb lp3 xchg [si],al mov [si-1],al lp3: dec bx jnz lp2 loop lp1 mov ah,4ch int 21h code ends end start
Descending Order: 15CS205J/Microprocessors and Microcontrollers Lab
CSE/SRM
17 | 29
DATA SEGMENT STRING1 DB 99H,12H,56H,45H,36H DATA ENDS CODE SEGMENT ASSUME CS:CODE,DS:DATA START: MOV AX,DATA MOV DS,AX MOV CH,04H UP2: MOV CL,04H LEA SI,STRING1 UP1:MOV AL,[SI] MOV BL,[SI+1] CMP AL,BL JNC DOWN MOV DL,[SI+1] XCHG [SI],DL MOV [SI+1],DL DOWN: INC SI DEC CL JNZ UP1 DEC CH JNZ UP2 INT 3 CODE ENDS END START
ExNo:5 i) Reverse a given string and check whether it is a palindrome
15CS205J/Microprocessors and Microcontrollers Lab
CSE/SRM
18 | 29
AIM: To implement assembly language program to check whether the string is a palindrome. APPARTUS: TASM Software, P.C.
Program:
DATA SEGMENT STR1 DB 'LIRIL' LEN EQU $-STR1 STR2 DB 20 DUP(0) ;start of data segment MES1 DB 10,13,'WORD IS PALINDROME$' MES2 DB 10,13,'WORD IS NOT PALINDROME$' DATA ENDS CODE SEGMENT ASSUME CS:CODE,DS:DATA,ES:DATA START: MOV AX,DATA MOV DS,AX MOV ES,AX LEA SI,STR1 LEA DI,STR2+LEN-1 MOV CX,LEN UP: CLD LODSB STD STOSB LOOP UP LEA SI,STR1 LEA DI,STR2 CLD MOV CX,LEN REPE CMPSB CMP CX,0H JNZ NOTPALIN LEA DX,MES1 MOV AH,09H INT 21H JMP EXIT NOTPALIN: LEA DX,MES2 MOV AH,09H INT 21H 15CS205J/Microprocessors and Microcontrollers Lab
CSE/SRM
19 | 29
EXIT: MOV AH,4CH INT 21H CODE ENDS END START OUTPUT: WORD IS PALINDROME
ExNo:5 ii) Program to use DOS interrupt INT 21H function called for reading a
character from keyboard, buffered keyboard input, display of character and string on console.
15CS205J/Microprocessors and Microcontrollers Lab
CSE/SRM
20 | 29
AIM: To implement assembly language program to display a String APPARTUS: TASM Software, P.C. Program:
DATA SEGMENT INKEY DB ? BUF DB 20 DUP(0) MES DB 10,13, ' SRM UNIVERSITY $' DATA ENDS CODE SEGMENT ASSUME CS:CODE , DS:DATA START: MOV AX,DATA MOV DS,AX MOV AH,01H INT 21H ;DOS function to read a character from keyboard ;with echo. [AL = 8bit character] MOV INKEY,AL ;Returns ASCII value of the pressed key. MOV BUF,10 MOV AH,0AH LEA DX,BUF INT 21H MOV AH,06H MOV DL,'A' INT 21H MOV AH,09H LEA DX,MES INT 21H MOV AH,4CH INT 21H CODE ENDS END START
ExNo:6 Time display using Interrupt (Read the current time from the system and display it in the standard format on the screen)
15CS205J/Microprocessors and Microcontrollers Lab
CSE/SRM
21 | 29
AIM: To implement assembly language program to display a system time APPARTUS: TASM Software, P.C. Program:
DATA SEGMENT HOUR DB ? MIN DB ? DATA ENDS CODE SEGMENT ASSUME CS:CODE, DS:DATA DISCHAR MACRO CHAR PUSH AX PUSH DX MOV DL,CHAR MOV AH,02 INT 21H POP DX POP AX ENDM START: MOV AX,DATA MOV DS,AX CALL TIME MOV AH,4CH INT21H TIME PROC NEAR MOV AH,2CH ;function to read system time INT21H MOV HOUR,CH MOV MIN,CL CMP CH,12 JB DOWN SUB CH,12 DOWN: MOV AL,CH MOV AH,00H AAM MOV AX,3030H DISCHAR AH 15CS205J/Microprocessors and Microcontrollers Lab
CSE/SRM
22 | 29
DISCHAR AL DISCHAR’:’ MOV AL,CL MOV AH,00H AAM - 56 ADD AX,3030H DISCHAR AH DISCHAR AL DISCHAR’ ’ CMP HOUR,12 JB AM DISCHAR ‘P’ JMP DOWN1 AM: DISCHAR’A’ DOWN1: DISCHAR’M’ RET TIME ENDP CODE ENDS END START
ExNo:7 Port Programming
15CS205J/Microprocessors and Microcontrollers Lab
CSE/SRM
23 | 29
i)Write an 8051 C program to send values of –4 to +4 to port P1. //Signed numbers #include void main(void) { char mynum[]={+1,-1,+2,-2,+3,-3,+4,-4}; unsigned char z; for (z=0;z<=8;z++) P1=mynum[z]; } ii)Write an 8051 C program to toggle bit D0 of the port P1 (P1.0) 50,000 times. Solution: #include sbit MYBIT=P1^0; void main(void) { unsigned int z; for (z=0;z<=50000;z++) { MYBIT=0; MYBIT=1; } }
iii)Write an 8051 C program to toggle bits of P1 ports continuously with a 250 ms. Solution: 15CS205J/Microprocessors and Microcontrollers Lab
CSE/SRM
24 | 29
#include void MSDelay(unsigned int); void main(void) { while (1) //repeat forever { p1=0x55; MSDelay(250); p1=0xAA; MSDelay(250); } } void MSDelay(unsigned int itime) { unsigned int i,j; for (i=0;i
ExNo:8 Timer-Counter Programming
i) Write an 8051 C program to toggle only pin P1.5 continuously every 250 ms. Use Timer 0, mode 2 (8-bit auto-reload) to create the 15CS205J/Microprocessors and Microcontrollers Lab
CSE/SRM
25 | 29
delay. Solution: #include void T0M2Delay(void); sbit mybit=P1^5; void main(void){ unsigned char x,y; while (1) { mybit=~mybit; for (x=0;x<250;x++) for (y=0;y<36;y++) //we put 36, not 40 T0M2Delay(); } } void T0M2Delay(void){ TMOD=0x02; TH0=-23; TR0=1; while (TF0==0); TR0=0; TF0=0; } ii) Write an 8051 C program to create a frequency of 2500 Hz on pin P2.7. Use Timer 1, mode 2 to create delay. Solution: #include void T1M2Delay(void); sbit mybit=P2^7; void main(void){ unsigned char x; while (1) { mybit=~mybit; T1M2Delay(); } } void T1M2Delay(void){ 15CS205J/Microprocessors and Microcontrollers Lab
CSE/SRM
26 | 29
TMOD=0x20; TH1=-184; TR1=1; while (TF1==0); TR1=0; TF1=0; }
ExNo:9 Serial Programming
Write a program for the 8051 to transfer letter “A” serially at 4800 15CS205J/Microprocessors and Microcontrollers Lab
CSE/SRM
27 | 29
baud, continuously. Solution: MOV TMOD,#20H ;timer 1,mode 2(auto reload) MOV TH1,#-6 ;4800 baud rate MOV SCON,#50H ;8-bit, 1 stop, REN enabled SETB TR1 ;start timer 1 AGAIN: MOV SBUF,#”A” ;letter “A” to transfer HERE: JNB TI,HERE ;wait for the last bit CLR TI ;clear TI for next char SJMP AGAIN ;keep sending A
Write a program for the 8051 to transfer “YES” serially at 9600 baud, 8-bit data, 1 stop bit, do this continuously Solution: MOV TMOD,#20H ;timer 1,mode 2(auto reload) MOV TH1,#-3 ;9600 baud rate MOV SCON,#50H ;8-bit, 1 stop, REN enabled SETB TR1 ;start timer 1 AGAIN: MOV A,#”Y” ;transfer “Y” ACALL TRANS MOV A,#”E” ;transfer “E” ACALL TRANS MOV A,#”S” ;transfer “S” ACALL TRANS SJMP AGAIN ;keep doing it ;serial data transfer subroutine TRANS: MOV SBUF,A ;load SBUF HERE: JNB TI,HERE ;wait for the last bit CLR TI ;get ready for next byte RET ExNo:10 Interrupt Programming Write a program in which the 8051 reads data from P1 and writes it to P2 continuously while giving a copy of it to the serial COM port to be 15CS205J/Microprocessors and Microcontrollers Lab
CSE/SRM
28 | 29
transferred serially. Assume that XTAL=11.0592. Set the baud rate at 9600.
Solution: ORG 0000H LJMP MAIN ORG 23H LJMP SERIAL ;jump to serial int ISR ORG 30H MAIN: MOV P1,#0FFH ; MOV TMOD,#20H ; MOV TH1,#0FDH ; MOV SCON,#50H ; MOV IE,10010000B ; SETB TR1 ; BACK: MOV A,P1 ; MOV SBUF,A ; MOV P2,A ; SJMP BACK ; ;-----------------SERIAL PORT ISR ORG 100H SERIAL: JB TI,TRANS; MOV A,SBUF ; CLR RI ; RETI ; TRANS: CLR TI ; RETI ; END
15CS205J/Microprocessors and Microcontrollers Lab
make P1 an input port timer 1, auto reload 9600 baud rate 8-bit,1 stop, ren enabled enable serial int. start timer 1 read data from port 1 give a copy to SBUF send it to P2 stay in loop indefinitely
jump if TI is high otherwise due to receive clear RI since CPU doesn’t return from ISR clear TI since CPU doesn’t return from ISR
CSE/SRM
29 | 29