All About Circuits Forum I am stuck on a homework problem regarding assembly code
 Register Blogs FAQ Members List Today's Posts Search Today's Posts Mark Forums Read

 Embedded Systems and Microcontrollers Discussion forum for projects and working with embedded systems and microcontrollers (FPGAs, PICs, AVRs). Get help with hardware issues and embedded programming.

#1
09-13-2009, 02:54 PM
 upopads Junior Member Join Date: Dec 2007 Posts: 42
I am stuck on a homework problem regarding assembly code

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
#2
09-13-2009, 03:43 PM
 upopads Junior Member Join Date: Dec 2007 Posts: 42

I'm starting to think the answer to the first question is 32 because 8*4 cycles.
#3
09-16-2009, 06:56 PM
 RiJoRI Senior Member Join Date: Aug 2007 Location: Long Island,NY (GMT-5) Posts: 536

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
__________________
"If you think the problem is bad, wait until we've fixed it!"
--Unknown

Real programmers use 8048s!
#4
09-19-2009, 07:47 AM
 Tahmid Senior Member Join Date: Jul 2008 Location: Cornell University, Ithaca, New York, USA (from Dhaka, Bangladesh) Posts: 344 Blog Entries: 7

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:
`MOVLW    0x32`
Select All
means, putting decimal 50 literal in w register.
Code:
`MOVWF   REG2,0`
Select All
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:
`LOOP1: DECF REG2, 1-4 Cyl`
Select All
This instruction should have written in the following manner.
Code:
`LOOP1: DECF REG2,1   ; 4 Cyl`
Select All
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:
`BNZ LOOP1 - 8/4 Cyl`
Select All
This Instr should have written in the following manner according to the Assembly grammar to make the new users understand properly:
Code:
`BNZ LOOP1      ; 8/4 Cyl`
Select All
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.
__________________
পৃথিবীর সর্বোচ্চ দালানও প্রথম ইট বসানো থেকেই তৈরি হয়েছে।
The tallest building in the world starts from the first brick.

Last edited by Tahmid; 09-19-2009 at 08:07 AM.
#5
09-27-2009, 09:10 PM
 Tahmid Senior Member Join Date: Jul 2008 Location: Cornell University, Ithaca, New York, USA (from Dhaka, Bangladesh) Posts: 344 Blog Entries: 7

Are you clear regarding your queries?
__________________
পৃথিবীর সর্বোচ্চ দালানও প্রথম ইট বসানো থেকেই তৈরি হয়েছে।
The tallest building in the world starts from the first brick.
#6
09-29-2009, 01:33 PM
 upopads Junior Member Join Date: Dec 2007 Posts: 42
Thank you for helping.

I am going to have to review this thoroughly for the midterm.

 Tags assembly, code, homework, problem, stuck

 Related Site Pages Section Title Textbook Microprocessor programming : Principles Of Digital Computing Textbook Microprocessors : Principles Of Digital Computing

 Similar Threads Thread Thread Starter Forum Replies Last Post jut Homework Help 29 10-09-2009 06:24 AM maple23 Programmer's Corner 1 04-23-2008 11:56 PM erickoh1985 Embedded Systems and Microcontrollers 6 03-04-2008 06:23 AM mentaaal Homework Help 6 12-11-2007 10:50 PM

 Thread Tools Display Modes Linear Mode

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home Electronics Forums     General Electronics Chat     The Projects Forum     Homework Help     Electronics Resources Software, Microcomputing, and Communications Forums     Programmer's Corner     Embedded Systems and Microcontrollers     Computing and Networks     Radio and Communications Circuits and Projects     The Completed Projects Collection Abstract Forums     Math     Physics     General Science All About Circuits Commmunity Forums     Off-Topic     The Flea Market     Feedback and Suggestions

All times are GMT. The time now is 07:28 AM.