Expriment No.: 2: Government Engineering College, Gandhinagar
Expriment No.: 2: Government Engineering College, Gandhinagar
Expriment No.: 2: Government Engineering College, Gandhinagar
EXPRIMENT No.: 2
AIM: To Study and Perform Data Transfer , Branch and Loop Instructions for AVR
ATmega32.
THEORY:
Cycles
Mnemonic Operands Description Operation Flags
MOV Rd,Rr Copy register Rd = Rr None 1
Rd+1:Rd =
1
MOVW Rd,Rr Copy register pair Rr+1:Rr, r,d even None
LDI Rd,K8 Load Immediate Rd = K None 1
LDS Rd,k Load Direct Rd = (k) None 2*
LD Rd,X Load Indirect Rd = (X) None 2*
Load Indirect and Post-
2*
LD Rd,X+ Increment Rd = (X), X=X+1 None
Load Indirect and Pre-
2*
LD Rd,-X Decrement X=X-1, Rd = (X) None
LD Rd,Y Load Indirect Rd = (Y) None 2*
Load Indirect and Post-
2*
LD Rd,Y+ Increment Rd = (Y), Y=Y+1 None
Load Indirect and Pre-
2*
LD Rd,-Y Decrement Y=Y-1, Rd = (Y) None
Load Indirect with
2*
LDD Rd,Y+q displacement Rd = (Y+q) None
LD Rd,Z Load Indirect Rd = (Z) None 2*
Load Indirect and Post-
2*
LD Rd,Z+ Increment Rd = (Z), Z=Z+1 None
Load Indirect and Pre-
2*
LD Rd,-Z Decrement Z=Z-1, Rd = (Z) None
Load Indirect with
2*
LDD Rd,Z+q displacement Rd = (Z+q) None
STS k,Rr Store Direct (k) = Rr None 2*
ST X,Rr Store Indirect (X) = Rr None 2*
Store Indirect and Post-
2*
ST X+,Rr Increment (X) = Rr, X=X+1 None
ST -X,Rr Store Indirect and Pre- X=X-1, (X)=Rr None 2*
Decrement
ST Y,Rr Store Indirect (Y) = Rr None 2*
Store Indirect and Post-
2
ST Y+,Rr Increment (Y) = Rr, Y=Y+1 None
Store Indirect and Pre-
2
ST -Y,Rr Decrement Y=Y-1, (Y) = Rr None
Store Indirect with
2
ST Y+q,Rr displacement (Y+q) = Rr None
ST Z,Rr Store Indirect (Z) = Rr None 2
Store Indirect and Post-
2
ST Z+,Rr Increment (Z) = Rr, Z=Z+1 None
Store Indirect and Pre-
2
ST -Z,Rr Decrement Z=Z-1, (Z) = Rr None
Store Indirect with
2
ST Z+q,Rr displacement (Z+q) = Rr None
LPM None Load Program Memory R0 = (Z) None 3
LPM Rd,Z Load Program Memory Rd = (Z) None 3
Load Program Memory 3
LPM Rd,Z+ and Post-Increment Rd = (Z), Z=Z+1 None
SPM None Store Program Memory (Z) = R1:R0 None -
IN Rd,P In Port Rd = P None 1
OUT P,Rr Out Port P = Rr None 1
PUSH Rr Push register on Stack STACK = Rr None 2
POP Rd Pop register from Stack Rd = STACK None 2
Mnemoni
c Operands Description Operation
RJMP k Relative Jump PC = PC + k +1
IJMP None Indirect Jump to (X,Y,Z) PC = Z
PROGRAMS:
1. Show a simple code to load values $30 and $97 into location $105,$106 respectively.
OUTPUT :
Location 0x105 has value 30H and location 0x106 has value 97H.
2. Show a simple code to load the values $55 into location $300 to $305.
OUTPUT:
OUTPUT: Location 0067H has value 15H. At the end of the program R21 has value 75H.
4. Write and assemble a program to load values into each of registers R20 –
R24 and then push each of these registers onto the stack. Single-step the
program, and examine the stack and the SP register after the execution of each
instruction.
OUTPUT : Different Values are loaded to location 20H to 24H and the values are pushed to
stack using PUSH instruction.
5. Write a program to
OUTPUT : By using BRNE instruction 3 is added to R20 ten times , and the output at PORT
B = 0X1E
6. Write a program to (a) load the PORTB register with the value of 0x55, and (b)
complement Port B 700 times.
OUTPUT: PORTB register is loaded with value 0x55. By complementing R16 we get value
0x55 at PORTB as output.
7. Write and assemble a program to toggle all the bits of PORTA, PORTB, and PORTC
continuously by sending $55 and $AA to these ports. Put a time delay between the
"on" and "off" states. Then using the simulator, single-step through the program and
examine the ports. Do not single-step through the time delay call.
OUTPUT : At the end of the program PORTA , PORTB , PORTC has the value 55H as output.
In DELAY subroutine R20 is set to 0xFF therefore loop is repeated 255 times, when R20
becomes zero it fall to RET and resumes the executing instruction after CALL.
Solution :
.INCLUDE "M32DEF.INC"
LDI R16, 1
STS $98, R16
LDI R16, 2
STS $99, R16
LDI R16, 3
STS $9A, R16
LDI R16, 4
STS $9B, R16
LDI R16, 5
STS $9C, R16
LDI R16, 6
STS $9D, R16
POP R20
POP R21
POP R22
POP R23
POP R24
L1: RJMP L1
2. Write and run a program to get data from PORTA and after adding a fixed value of 5
send it to PORTB.
Solution :
INCLUDE "M32DEF.INC"
3. Write a program to toggle all bits of I/O register PORTB every 1sec. Assume that the
crystal frequency is 8 MHz and the system is using an ATmega32
Solution :
.ORG 0x0000
LDI R16,HIGH(RAMEND)
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16
LDI R16,0X55
BACK : COM R16
OUT PORTB,R16
CALL DELAY_1S
RJMP BACK
DELAY_1S :
LDI R20,32
L1 : LDI R21,200
L2 : LDI R22,250
L3 : NOP
NOP
DEC R22
BRNE L3
DEC R21
BRNE L2
DEC R20
BRNE L1
RET
4. Write a program to determine if RAM location 0x200 contains the value 0. If so, put 0x55
into it.
Solution :
.EQU MYLOC=0X200
TST R30
BRNE NEXT
STS MYLOC,R30
Conclusion:
Data Transfer instructions are used to load and store data to the General Purpose Registers,
also known as the Register File. Exceptions are the push and pop instructions which modify
the Stack Pointer. Arithmetic and Logic Instructions plus Bit and Bit-Test Instructions use
the ALU to operate on the data contained in the general purpose registers.