To start with CP command in Atmega32 using ATMEL STUDIO 7 Assembly Two registers, Rd and Rr, are compared by the CP instruction. Not a single register has been altered. Following this directive, all conditional branches can be utilized. Operation:
- Rd – Rr
Syntax: Operands: Program Counter:
(i) CP Rd,Rr 0 ≤ d ≤ 31, 0 ≤ r ≤ 31 PC ← PC + 1
16-bit Opcode:
0001 | 01rd | dddd | rrrr |
For example
cp r4,r19 ; Compare r4 with r19
brne noteq ; Branch if r4 <> r19
noteq: nop ; Branch destination (do nothing)
Let us understand this command through a program in assembly language.
Firstly, we will include a header file.
CODE:
.INCLUDE “M32DEF.INC”
If you do not know how to include it, you can check by clicking here.
LDI R16,HIGH(RAMEND)
OUT SPH, R16
LDI R16,LOW(RAMEND)
OUT SPL,R16 // INITIALIZING THE STACK
First, we will initialize the stack.
For initializing the stack, we OUT the High(RAMEND) on the SPH and we OUT the Low(RAMEND) on the SPL. Here SPH means Stack Pointer High and SPL means Stack Pointer Low. It means that we have specified the space for the stack in RAM.
LDI R16,0X00
OUT DDRB, R16 //configured as input.
As we have to check the values on port B, port B has been configured as input. For this purpose, we loaded the hex value 00 in the general-purpose register R16.
Then we OUT that value on the data direction register of port B.
LDI R17,0XFF
OUT DDRC,R17 //portc configured as output
Then, we loaded the hex value FF in the general-purpose register R17. After that, we OUT that value on the data direction register of port C. In this way, port C has been configured as OUTPUT.
IN R18,PINB //r18=pinb
The IN command gets the data from any special function register and gives it to the general-purpose register.
As here in our code, PINB is the special function register of port B so, the IN command got the data from it and gave it to general purpose register R18.
LDI R19,0X45 //r19=45h
Here we loaded the x45 value in the general-purpose register R19 so that we can compare this value with the value of the register R18 that came via PINB.
LOOP:
CP R18,R19 //r18=r18-r19
BRNE LOOP1
LDI R20,0X99
OUT PORTC,R20
JMP LOOP
EXPLANATION:
In the loop first, we used the command CP what the CP command does is, it compares the values, and if the answer is 0, that means this is the exact value with which we are comparing our value.
For example, if we compare the value of r19 which is 45 with r18 if the value of r18 is 45 then we will get the answer 0.
In the next step, we used the command branch not equal. What the BRNE command does is, it remains true until the z flag has the value 0.
And if the answer is 0 the z flag would be raised, and it would have a value of 1 and the BRNE condition becomes false.
Remember from the statement, that if port B has a value of 45H it would send 99H to PORTC.
So, if after the comparison we got the 0 value the BRNE would terminate, and the pointer would jump to the next instruction. That is, it will load the value of 99 in the general purpose register r20 and then OUT that value on port C and the loop continues.
But in case, if the value is nonzero then the BRNE would become true, and it will jump to LOOP1.
LOOP1:
LDI R20,0X00
OUT PORTC,R20
JMP LOOP
Here, it will load the value of 00 in the general purpose register r20 and then OUT that value on port C. In this way, we will get the answer 0 and the port will be cleared, and the statement will be fulfilled.
Now we will build and debug our solution and check the output step by step.
DEBUG:
Now we will check our output step by step by pressing the F10 or you can use the option step over from the menu bar.
Firstly, we can see the stack has been initialized.
Below you can see port B has been configured as input and port C as output.
In the next step, it would compare the values of r18 and r19.
As below you can see the z flag has not raised so it would jump to LOOP1.
So, the port C will have a value of 0.
Conclusion
In conclusion, learning how to use Atmel Studio 7 Assembly to grasp the CP command in the Atmega32 may significantly improve your programming abilities and give you the ability to write effective, optimized code for your projects. You may fully utilize this command’s strength and advance your programming skills by practicing and being dedicated to it. Continually study, explore, and experiment to become an expert Atmel Studio 7 Assembly programmer!
For Complete Trial Watch the Video: CP command in Atmega32 using ATMEL STUDIO 7 Assembly
For more blogs explore the website: https://ninjatech.live/