# I am stuck on a homework problem regarding assembly code

Discussion in 'Embedded Systems and Microcontrollers' started by upopads, Sep 13, 2009.

Dec 18, 2007
42
0
This Question is for my class in Embedded microcontrollers in the book Fundamentals of Microcontrollers and applications in Embedded systems by Ramesh S. Gaonkar it is chapter 5 on branch operations. I am at a loss as to what the code is asking, i need help in solving the problem specifically with understanding what Cyl means in the code.

Question:
5.22 How many times is LOOP1 executed in the following instructions?

MOVLW 0x32
MOVWF REG2,0
LOOP1: DECF REG2, 1-4 Cyl
BNZ LOOP1 - 8/4 Cyl

Other questions that are confusing to me:

How many times is LOOP1 executed if BNZ is changed to BZ

How many times is LOOP1 executed if the instructions BNZ is changed to BNC

How many times is LOOP1 executed if the instruction DECF REG2,1 is changed to DECF REG2, 0

Dec 18, 2007
42
0
I'm starting to think the answer to the first question is 32 because 8*4 cycles.

3. ### RiJoRI Well-Known Member

Aug 15, 2007
536
26
Time to "play computer"!
Change the 0x32 to 0x03 (unless you want to do all 50 reps!). Get a sheet of paper and a pencil, and write down the instructions. The author seems to be using a "-" instead of the more common ";". Go through all the instructions, writing down the value in REG2. It will look something like:

????
03
02
etc.

Every time you pass through the label, count it. When you fail the test, you will know how many times you've gone through the loop compared to your original number (0x03). Now apply this to 0x30.

Now repeat this for all the other test instructions. Look at the assembly instruction set to see the effect the test instructions have on the flags (Z,C, etc.).

--Rich

4. ### Tahmid Active Member

Jul 2, 2008
344
27
The example you have put and not clear to you is very interesting and set by the author to make sure that the reader understands all the tidbits of Branching and related Instructions in Assembly Language fully in the process of solving the question no.5.22 . Though it requires detail Assembly Language knowledge to understand that, I will try to make you understand as simply as possible.

Code ( (Unknown Language)):
1. MOVLW    0x32
means, putting decimal 50 literal in w register.
Code ( (Unknown Language)):
1. MOVWF   REG2,0
means, placing that dec 50 in REG2 and by placing a 0 after REG2 it is meant that,this REG2 is located in Access Bank. This is the default address and if even you write only REG2 the Assembler will consider it in Access Bank and you need not to bother about Bank changing etc. But if the instruction is REG2,1 then you have to use Bank Select Register yourself.

Code ( (Unknown Language)):
1. LOOP1: DECF REG2, 1-4 Cyl
This instruction should have written in the following manner.
Code ( (Unknown Language)):
1. LOOP1: DECF REG2,1   ; 4 Cyl
means, decrement the value of REG2 from dec 50 to dec 49 and place this decremented value in the REG2 itself and continue this process till the REG2 value decremented to 0. In place of 1,you can write F also, like REG2,F. Here,1 is destination which is file itself if place 1 or F and in W Register if put 0 or W.
Actual instruction is REG2,0,1. Since Access Bank is Default, so it does not require to mention and hence written as, REG2,1. If written, REG2,0 here, then the meaning would be placing the decremented value 49 and all other subsequent values including the last value 0 in W Register.

4 Cyc means 4 Clock cycles. Pic Microcontroller requires 4 Clock cycles to carry our 1 Instruction and hence 4 Clock cycles are known as 1 Instruction Cycle. Suppose, your Processor is having 4MHz Oscillator and hence Instruction cycles will be carried out in 1MHz cycles and each instruction will take 1Microsecond to execute.
In this case, it means, the DECF instruction is a single word instruction which will take 1 Instruction cycle or 4 Clock Cycles to execute.

Last Instr :
Code ( (Unknown Language)):
1. BNZ LOOP1 - 8/4 Cyl
This Instr should have written in the following manner according to the Assembly grammar to make the new users understand properly:
Code ( (Unknown Language)):
1. BNZ LOOP1      ; 8/4 Cyl
means, with decrementing the REG2 by the instr DECF if Zero Flag is not raised, then Branch rearward to LOOP1 Label ( BNZ means Branch not Zero.) and when the decremented value will be 0, the Zero Flag will be raised and then the program counter will not Branch to LOOP1 and it will move to the next Instruction.

8/4 Cyl means when the Program Counter Branches to LOOP1, the Processor will take 8 Clock Cycles(2 Instr Cycles) in each Branch action and when the decremented value reaches 0, then the Program Counter will not Branch but move to the next instruction and to execute that last move, the Processor will take 4 Clock Cycles(1 Instr Cycle). For that reason it is mentioned 8/4 Cyl.

Now other questions that are confusing to you:
1.How many times is LOOP1 executed if BNZ is changed to BZ

Ans. If BNZ is changed to BZ(Branch if Zero Flag is raised), the Loop will not carry out even for once because with first DECF Instr, the REG2 value will be decremented to dec 49 and so Zero Flag will not be raised and hence no Branching to LOOP1 and so no LOOP ing action.

2. How many times is LOOP1 executed if the instructions BNZ is changed to BNC

Ans. If the Instr BNZ is changed to BNC (Branch not Carry) Loop1 will not carry out for even a single time as with the first decrement of REG2 value from dec 50 to dec 49, the Carry Flag will be raised and hence the program counter will not Branch to Label LOOP1 as the Instr is BNC(Branch if Carry Flag is not raised).

3. How many times is LOOP1 executed if the instruction DECF REG2,1 is changed to DECF REG2, 0

Ans. With the change of Instr DECF REG2,1 to DECF REG2,0 - an endless LOOP will be created and Program will not go out of the LOOP. With this change Instr, the first decremented value of the REG2 will be dec 49 and this value will not be put in REG2 but in W Register. So, the REG2 value remains dec 50 and W Register value will remain dec 49 and all the time program counter will Branch to LOOP1 and endless Looping will continue till the Program is changed or Power Supply is off.

Hope,this will help you to clear your doubt. You see, Assembly Language Programming is somehow complex, time consuming but very exciting and interesting.
With Thanks.

Last edited: Sep 19, 2009

Jul 2, 2008
344
27