Help on how to write these programs

Thread Starter

norbey

Joined Apr 18, 2017
4
1. Write a PIC16F877A assembler subroutine called “Mul_8x8” to multiply register Numb1 in memory location 20h, with register Numb2 in memory location 21h. The least significant byte result must be placed in register “AnswL” in memory location 22h. The most significant byte result must be placed in register “AnswH” in memory location 23h. Data in registers Numb1 and Numb2 must remain unchanged after the multiplication. First sketch a flowchart to determine the program structure and then write the firmware source code.
2. Write a PIC16F877A assembler subroutine called “DataStatistics” to count the number of registers in a memory block in RAM that contains number values that are greater than fifty, less than fifty and equal to fifty. The numbers counted must be placed in registers called “GreaterThan”, “LessThan” and “EqualTo”, located in memory 70h, 71h, and 72h respectively. Make use of indirect addressing to access the memory block in RAM that starts at 30h and ends at 60h.
3. Write a PIC16F877A assembler subroutine called “AsciiToBCD” to convert ASCII data in register “AsciiH” and “AsciiL” in memory locations 20h and 21h, to a single packed BCD byte. The result must be stored in register “PackedBCD” in memory location 22h.
4. Write a PIC16F877A assembler subroutine called “HexToAscii” to convert a hexadecimal byte in register “HexByte” in memory location 20h, into two ASCII characters. The higher nibble ASCII character must be returned in register “CharTens” in memory location 21h and the lower nibble ASCII character must be returned in register “CharUnits” in memory location 22h. A look-up table called “AsciiChar” must be used to convert a hexadecimal nibble value to an ASCII character.
5. Write a PIC16F877A assembler fixed delay subroutine called “D500US” using a single register called “DR1” located in memory 20h. The micro-controller is clocked by a 4MHz crystal oscillator. Show all calculations.
 

MrAl

Joined Jun 17, 2014
11,474
Hi,

Lots of questions. I'll start with number 1.

To multiply in greater precision you just follow the rule for normal multiplication like you would do on paper. For example multiply 12 times 34 as on paper.

First we multiply 4*2, right that down as 8, then multiply 4 times 1, write that down with the 8 like so:
048

and next we do 3*2=6 and then 3*1=3 and write that as:
360

and next we add the two:
360+048=408

If we were storing these values in 4 bit registers we would store one number in each register, and note we had to add a placeholder of 0 sometimes depending on what digit we were doing.

To extend this to multiple digits, we do the same thing, we just can use bigger registers. For example multiply 1234 times 5678 using registers that hold two digits this time.
We would first multiply 78 times 34, then 78 times 12, and then 56 times 34, then 56 times 12. The place holders this time woudl be two zeros 00 because we are doing two digits at a time. We then add them all the same way, added two digit groups at a time.

This is best done in binary, and the number of bits done at any one time is equal to the largest register available. For your problem it appears that you can use an 8 bit register so you can handle 8 bits at a time.

The catch is that your register has to be able to show the carry when you multiply, or else you have to reduce the number of bits done as one group. For example, doing 4 bits at a time means we might end up with 9 x 9 which would lead to two digits: 81. This would mean we need a register that could hold two digits at a time. In binary, this would be 0x0F times 0x0F which is 0xE1, so we'd need a register that could hold 8 bits even though we are only multiplying two 4 bit binary (or hex) numbers.

For speed you could use a table that has all the entries for digits 0x00 to 0x0F. You then just look up the result and store that in an appropriate place.

If you are working in pure binary, you would just either add the first number (shifted) or not add it, then proceed to the next second number binary digit.
For example to multiply 110 times 101
we add 110, then dont add anything, then add 11000. So it's just shifting and adding.
 

djsfantasi

Joined Apr 11, 2010
9,163
This is Homework Help after all. Sometime members will refer to it as "Not Homework Done For You",

You need to show us how far you have progressed on your own. That is, show us the code you have written so far. Or a flowchart or pseudo-code. This will help us see how much you know and/or where you are getting stuck.

I recommend that you split the three questions into three posts. That way, there is no confusion as to which problem is being addressed.
 

Thread Starter

norbey

Joined Apr 18, 2017
4
Hi, again, the thing is just that I have just being introduced to this type of programming.I don't even know how to write the program using MPLAB. Interpreting what's being written I can but I get stuck when it comes to the writing of the program
I would love if maybe you djsfantasi could maybe assist me on how to use the software
 

djsfantasi

Joined Apr 11, 2010
9,163
It's programming. Do you have a flowchart or pseudo code. Personally, I don't use MPLAB, but there are many here who do.
 
Last edited:

MaxHeadRoom

Joined Jul 18, 2013
28,686
Last edited:

JohnInTX

Joined Jun 26, 2012
4,787
The way to start is to turn the computer OFF, don't try to find a canned solution on the internet or a forum and don't just fire up MPLAB and start throwing code at it. It doesn't work that way, here on AAC because we don't do your work for you and specifically in programming since you can't code a problem that you haven't solved first.

Get a pencil and pad of paper and start breaking down one of your problems into smaller and smaller chunks. Think about each chunk until you get it solved. Only then can you think about implementing your solution on a PIC (or anything else for that matter). While it helps to know the instruction set of the target processor while thinking it is not necessary to get started. So get started. Since you are a beginner, there will be lots of fits and starts while you are thinking, that's why you use a pad and pencil, just sketch away until the problem and its solution becomes clear. There really is no other way..

Good luck!
 

djsfantasi

Joined Apr 11, 2010
9,163
The way to start is to turn the computer OFF, don't try to find a canned solution on the internet or a forum and don't just fire up MPLAB and start throwing code at it. It doesn't work that way, here on AAC because we don't do your work for you and specifically in programming since you can't code a problem that you haven't solved first.

Get a pencil and pad of paper and start breaking down one of your problems into smaller and smaller chunks. Think about each chunk until you get it solved. Only then can you think about implementing your solution on a PIC (or anything else for that matter). While it helps to know the instruction set of the target processor while thinking it is not necessary to get started. So get started. Since you are a beginner, there will be lots of fits and starts while you are thinking, that's why you use a pad and pencil, just sketch away until the problem and its solution becomes clear. There really is no other way..

Good luck!

Great explanation, John. After a while one forgets how to explain this stuff to a beginner. And I used to do this all the time.

@norbey, as John stated, writing a program begins by stating what you want to do and then breaking it down into ever finer steps, ON PAPER AT FIRST. I used to use the following example.

You want a peanut putter sandwich, so you get the peanut butter and spread it on some bread. Cover the peanut butter with more bread and eat it.

Fine, but a programmer would approach it differently.

Shout the word "initialization". Then, get a jar of peanut butter, a loaf of bread (from the bread box), a plate and a butter knife and place them on the counter.

Shout the word "setup". Open the jar of peanut butter and place the lid on the counter. Open the loaf of bread by removing the plastic tab and put two slices on the plate which is on the counter. Twist the bread bag, put the plastic tab on the bread bag and put the bread back in the bread box.

Shout the word "main". Get the butter knife, scoop a blob of peanut butter from the jar and spread it on one slice of bread. Repeat until covered to the desired thickness. Put the knife in the sink. Cover the peanut buttered slice with a second slice of bread. Place it on the plate. Cover the peanut butter jar. Eat the sandwich.

Of course I've omitted some details, like how thick is the desired thickness. But you get the idea.
 
Top