Lesson 1: The Language of Microprocessors pps

137 183 0
Lesson 1: The Language of Microprocessors pps

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Lesson 1: The Language of Microprocessors Most people think that computers are some kind of complicated device that is impossible to learn and infinitely intelligent, able to think better than a person. The truth is much less glamorous. A computer can only do what the programmer has told it to do, in the form of a program. A program is just a sequence of very simple commands that lead the computer to solve some problem. Once the program is written and debugged (you hardly ever get it right the first time), the computer can execute the instructions very fast, and always do it the same, every time, without a mistake. And herein lies the power of a computer. Even though the program consists of very simple instructions, the overall result can be very impressive, due mostly to the speed at which the computer can process the instructions. Even though each step in the program is very simple, the sequence of instructions, executing at millions of steps per second, can appear to be very complicated, when taken as a whole. The trick is not to think of it as a whole, but as a series of very simple steps, or commands. Most microprocessors, or very small computers, (here after referred to simply as micro's) have much the same commands or instructions that they can perform. They vary mostly in the names used to describe each command. In a typical micro, there are commands to move data around, do simple math (add, subtract, multiply, and divide), bring data into the micro from the outside world, and send data out of the micro to the outside world. Sounds too simple it is. A typical micro has three basic parts inside. They are the Program Counter, Memory, and Input / Output. The Program Counter keeps track of which command is to be executed. The Memory contains the commands to be executed. The Input / Output handles the transfer of data to and from the outside world (outside the micro's physical package). The micro we'll be using is housed inside a 40 pin package, or chip. There are many other actual parts inside our micro, but too much, too soon, would be too confusing right now. We will, however, learn about each and every single one. A Simple Program As stated before, a program is a sequence or series of very simple commands or instructions. A real world example program might be the problem of crossing a busy street. Step 1: Walk up to the corner of the street and stop. Step 2: Look at the traffic light. Step 3: Is the light green for your direction of travel? Step 4: If the light is red, goto step 2. (otherwise continue to step 5) Step 5: Look to the left. Step 6: Are there cars still passing by? Step 7: If yes, goto step 5. (otherwise continue to step 8). Step 8: Look to the right. Step 9: Are there cars still passing by? (there shouldn't be any by now, but, you never know!) Step 10: If yes, goto step 8. (otherwise continue to step 11) Step 11: Proceed across the street, carefully!! Now this may seem childish at first glance, but this is exactly what you do every time you cross a busy street, that has a traffic light (at least, I hope you do). This is also exactly how you would tell a micro to cross the street, if one could. This is what I mean by a sequence or series of very simple steps. Taken as a whole, the steps lead you across a busy intersection, which, if a computer did it, would seem very intelligent. It is intelligence, people are intelligent. A programmer that programmed these steps into a micro, would impart that intelligence to the micro. The micro would not, however, in this case, know what to do when it got to the other side, since we didn't tell it. A person, on the other hand, could decide what to do next, at a moments notice, without any apparent programming. In the case of a person, though, there has been some programming, it's called past experiences. Another program might be to fill a glass with water from a faucet. Step 1: Turn on the water. Step 2: Put the glass under the faucet. Step 3: Look at the glass. Step 4: Is it full? Step 5: If no, goto step 3.(otherwise, continue to step 6) Step 6: Remove the glass from under the faucet. Step 7: Turn off the water. This is a simpler program, with fewer steps, but it solves a problem, to fill a glass with water. In a micro, the problems are different (they could be the same if you've made some kind of servant robot), but the logical steps to solve the problem are similar, that is, a series of very simple steps, leading to the solution of a larger problem. Also notice that since the steps are numbered, 1 through 7, that is the order in which they're executed. The Program Counter, in this case, is you, reading each line, starting with 1 and ending with 7, doing what each one says. In a micro, the Program Counter automatically advances to the next step, after doing what the current step says, unless a branch, or jump, is encountered. A branch is an instruction that directs the Program Counter to go to a specific step, other than the next in the sequence. The branch in this example is step 5. Not only is this a branch, but it is a conditional branch. In other words, based on whether the glass is full or not, the branch is taken, or not. A micro has both branch and conditional branch instructions. Without this ability to reuse instructions, in a sort of looping action, a solution would take many more steps, if it would be possible at all. The point of this lesson is to show how a simple set of instructions can solve a bigger problem. Taken as a whole, the solution could appear to be more complicated than any of the separate steps it took to solve it. Well that wasn't so tough, was it? The most difficult problem to be solved in programming a micro is to define the problem you are trying to solve. Sounds like some kind of play on words, but I assure you, it's not. This is the Logical Thought Process I mentioned earlier. The mark of a good programmer is one who has this ability. It is, in my humble opinion, a developed skill, coupled with some talent, and a good understanding of the problem you're trying to solve. In most cases, God has endowed you with the talent and the ability to reason, all you need do is develop the skill of Problem Solving. My email address is here and I would welcome your questions or comments as you proceed through this course. Depending on the volume of mail I get, it may take a couple of days or so for me to get back to you, but be assured, I will get back to you. I really do want you to understand the information I'm presenting, and not being a professional teacher, I might not always explain things to the satisfaction of everyone, but with your patience, I'll do my best. My home page is http://www.hkrmicro.com/personal/index.html Decimal Vs. Binary (and Hex) Most people have learned to use the Decimal numbering system for counting and calculations. But micros use a different system. It's called Binary. All numbering systems follow the same rules. Decimal is Base 10 and Binary is Base 2. The base of a system refers to how many possible numbers can be in each digit position. In decimal, a single digit number is 0 through 9. In binary a single digit number is 0 or 1. In decimal, as you count up from 0, when you reach 9 and add 1 more, you have to add another digit position to the left and carry a 1 into it to get 10 (ten). Ten is a two digit decimal number. In binary, as you count up from 0, when you reach 1 and add 1 more, you have to add another digit position to the left and carry a 1 into it to get 10 (two decimal). While this is exactly what you do in decimal, the result looks like ten. What you have to do is get past seeing all numbers as decimal. While decimal 10 (ten) looks like binary 10 (two decimal) they represent different decimal values. It is still useful to think in decimal, since that's what we're used to, but we have to get used to seeing numbers represented in binary. Another small difference between decimal terminology and binary is that in binary a digit is called a bit. It gets even more confusing by the fact that 4 bits make a nibble. Two nibbles make a byte. Two bytes make a word. Most numbers used in a micro don't go beyond this, although there are others. Using what I've just said, if two nibbles make a byte, you could also say that a byte is eight bits. To represent a binary number larger than 4 bits, or a nibble, a different numbering system is normally used. It is called hexadecimal, or Base 16. A shorter name for hexadecimal is simply hex, and that's what we'll use here after. In this system there are 16 possible numbers for each digit. For the first 10, 0 through 9, it looks like decimal. Unlike decimal, when you add 1 more to 9, you get A. I know that having a letter to represent a number is really confusing, but they had to call it something, and A is what they chose. So a hex A is a decimal 10 (ten). The numbers count up from A through F. Just to clarify this here is the sequence of counting in hex from 0 to where you have to add another digit position to the left 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F (no G). This represents a decimal count of 0 through 15. At a count of F (15 decimal), if you add 1 more you get 10 (oh no! not another 10 that means something else!!). Sad but true. Let's regroup here. A binary 10 (one zero) is decimal 2, a decimal 10 is ten, and a hex 10 is decimal 16. If you can get this concept, you will have conquered the most difficult part of learning micros. I need to get past one more obstacle, the idea of significance. In a decimal number like 123, 3 is the least significant digit position (the right most digit) and 1 is the most significant digit position (the left most digit). Significance means the relative value of one digit to the next. In the number 123 (one hundred twenty three) , each number in the right hand most digit position (3) is worth 1. The value of each number in the next most significant digit position (2) is worth ten and in the most significant digit position (1) each is worth a hundred. I'm not trying to insult your intelligence here, but rather to point out the rule behind this. The rule is that no matter what base you're working in, as you start adding digits to the left, each one is worth the base times (multiplied) the digit to the right. In the decimal number 123 (base 10), the 2 digit position is worth 10 times the 3 digit position and the 1 digit position is worth 10 times the 2 digit position. Hence the familiar units, tens, hundreds, and so on. For some reason, for most people, this makes sense for decimal (base 10) but not for any other base numbering system. The very same is true for binary. The only difference is the base. Binary is base 2. So in binary the least significant bit (remember bits?) is worth 1 ( this happens to be the same for all bases). The next most significant bit is worth 2, the next worth 4, the next worth 8, and so on. Each is 2 times (base 2) the previous one. So in an 8 bit binary number (or byte, remember bytes?), starting from the right and moving left, the values for each of the 8 bit positions are 1, 2, 4, 8, 16, 32, 64 , and 128. If you've got this, you have passed a major milestone, you should go celebrate your passage. If you haven't, I would re-read the above until you do, and then go celebrate!! ( By the way, if you didn't get this the first time through, join the crowd. I didn't either!!) In hex (base 16) the same rule applies. In a 4 digit hex number, starting at the right and working left, the first digit is worth 1 ( hey that's just like decimal and binary!!), the next is worth 16 (base times the previous digit), the next is worth 256 (16 X 16), and the most significant is worth 4096 (256 X 16). One last note, hex is just binary described another way. A hex digit is a binary nibble ( remember nibbles?). Both are 4 bit binary values. Trying to wrap all this confusion up in review, 4 bits is a nibble or a hex digit. Two hex digits is a byte or 8 bit binary. A 4 digit hex number is a word, or 16 bit binary or 4 nibbles, or 2 bytes. You may have to review the previous paragraphs a few times (I did!!) to get all the relationships down pat, but it is crucial that you are comfortable with all I've said, so that what follows will make more sense. Let's take a few example numbers and find the decimal equivalent of each. Let's start with the binary number 1011, a nibble. Starting at the right and moving left, the first digit is worth one, because there is a 1 there. The next is worth two, because there is a 1 in it. The next would be worth 4, but since there is a 0, it's worth zero. The last or most significant is worth eight, since there is a 1 in it. Add all these up and you get eleven. So a binary 1011 is decimal 11. Also, since this could be a hex digit, the hex value would be B. Let's take a longer binary number 10100101. Starting at the right moving left, the first is worth 1, the next is worth 0, the next is worth 4, the next is 0, the next is 0, the next is worth 32, the next is 0, and the last is 128. Adding all these up you get decimal 165. Dividing this number up into two hex digits, you get A5. So binary 10100101, decimal 165, and hex A5 are all the same value. Using hex, the rightmost digit is worth 5, and the most significant digit is worth 160 (10 X 16), resulting in decimal 165. If you understand this, your ready to move on, leaving the different systems behind. If you don't, keep reviewing and studying until you do. If you don't understand and still continue, you will be confused by what follows. I promise that once you get this, the rest is easier. This ends the second lesson. I hope this wasn't too daunting or difficult, but there was a lot to get through. The rest of the course should be a little easier. Learning a new numbering system is like learning a new language. It's a little cumbersome at first, but it gets easier. My email address is here and I would welcome your questions or comments as you proceed through this course. Depending on the volume of mail I get, it may take a couple of days or so for me to get back to you, but be assured, I will get back to you. I really do want you to understand the information I'm presenting, and not being a professional teacher, I might not always explain things to the satisfaction of everyone, but with your patience, I'll do my best. My home page is http://www.hkrmicro.com/personal/index.html A Micro's Commands (or Instructions). I would like to take a moment and address a few things that will make it easier for me to describe things to you later. In a micro we refer to a binary number like 1111 as 1111b, a decimal number like 123 as 123, and a hex number like A5 as A5h. So don't be confused by the letters following numbers, it is easier than having to say the whole words binary, decimal, or hexadecimal, but still indicate it. By doing this, there is no doubt what base a multidigit, or multibit, number is in. Also there is another kind of memory, called flags. Flags are single bit numbers used to indicate different conditions. They are called flags because they flag the program of events or conditions. If a flag is raised, or has a 1 in it, it is said to be SET. If it is a 0, it is said to be CLEARED. One other thing, in an 8 bit byte the 8 bits are referred to as bits 0 through 7, with bit 0 being the right most, or least significant, and bit 7 as the left most or most significant. In micros, a 0 is just as real a number as 1 or 8, and it should not be thought of as "nothing". Lastly, there are various Registers inside a micro. These vary from micro to micro, but all contain a register called the Accumulator. It is also referred to in some as the a register. I will be using the accumulator in the following discussion. It is a type of memory for storing temporary results and is 8 bits wide, or a byte, as are most places that data can be put inside the micro. In the micro we will be using, there are 5 different types of instructions and several variations of each, resulting in 110 different instructions. These 5 types are ARITHMETIC, LOGICAL, BOOLEAN, BRANCHING, and DATA TRANSFER. ARITHMETIC The arithmetic instructions include addition, subtraction, division, multiplication, incrementing, and decrementing. There are two flags used with arithmetic that tell the program what the outcome of the instruction was. One is the Carry (C) flag. The other is the Zero (Z) flag. The C flag will be explained in the following example of addition. The Z flag, if set, says that the result of the instruction left a value of 0 in the accumulator. We will see the Z flag used in a later lesson. Addition This is straight forward and is simply to add two numbers together and get the result. However there is one more thing. If, in the addition, the result was too big to fit into the accumulator, part of it might be lost. There is a safeguard against this. Take the case of 11111111b (255) and 11111111b (255). These are the largest numbers that can fit into an 8 bit register or memory location.You can add these as decimal numbers, since I gave you their values in decimal also, and you would get 510. The binary value for 510 is 111111110b (9 bits). The accumulator is only 8 bits wide, it is a byte. How do you fit a 9 bit number into 8 bits of space? The answer is, you can't, and its called an OVERFLOW condition. So how do we get around this dilemma? We do it with the CARRY (C) flag. If the result of the addition is greater than 8 bits, the CARRY (C) flag will hold the 9 th bit. In this case the accumulator would have a 11111110b (254) and the C flag would be a 1, or set. This 1 has the value of 256 because this is the 9th bit. We haven't covered a 9 bit number, but they come up all the time as overflows in addition. Since we are using base 2, and we found out in lesson 2 that the 8th bit (bit 7) in a byte is worth 128, then the 9th bit is worth 2 times that, or 256. Adding 254 and 256, we get 510, the answer, and we didn't loose anything, because of the C flag. Had the result of the addition not caused an overflow, the C flag would be 0, or cleared. Subtraction In the case of subtraction, the process is more difficult to explain, and as such, I'm not going to cover it here. It involves 1's compliment and 2's compliment representation. But I will tell you this, you can subtract two numbers and if there is an under flow, the C flag will be a 1, otherwise it will be a 0. An under flow is where you subtract a larger number from a smaller number. Multiplication and Division In the micro we will be using, are multiply and divide instructions, but I will wait till later to talk about them. They do, however, just what the names suggest. Two other instructions are included in the arithmetic group. They are increment and decrement. These instructions are used to count events or loops in a program. Each time an increment is executed, the value is incremented by 1. A decrement, decrements the value by 1. These can be used with conditional jumps to loop a section of program, a certain number of times. We will see these used later. LOGICAL In micros there are other mathematical instructions called logical instructions. These are OR , AND, XOR, ROTATE, COMPLEMENT and CLEAR. These commands are usually not concerned with the value of the data they work with, but, instead, the value, or state, of each bit in the data. OR The OR function can be demonstrate by taking two binary numbers, 1010b and 0110b. When OR'ing two numbers, it doesn't matter at which end you start, right or left. Let's start from the left. In the first bit position there is a 1 in the first number and a 0 in the second number. This would result in a 1. The next bit has a 0 in the first number and a 1 in the second number. The result would be 1. The next bit has a 1 in the first number and a 1 in the second number. The result would be a 1. The last bit has a 0 in the first number and a 0 in the second number, resulting in a 0. So the answer would be 1110b. The rule that gives this answer says that with an OR, a 1 in either number results in a 1, or said another way, any 1 in, gives a 1 out. AND AND'ing uses a different rule. The rule here is a 0 in either number will result in a 0 , for each corresponding bit position. Using the same two numbers 1010b and 0110b the result would be 0010b. You can see that every bit position except the third has a zero in one or the other number. Another way of defining an AND is to say that a 1 AND a 1 results in a 1. XOR (eXclusive OR) XOR'ing is similar to OR'ing with one exception. An OR can also be called an inclusive OR. This means that a 1 in either number or both will result in a 1. An eXclusive OR says that if either number has a 1 in it, but not both, a 1 will result. A seemingly small difference, but crucial. Using the same two numbers, the result would be 1100b. The first two bits have a 1 in either the first or the second number but not both. The third bit has a 1 in both numbers which results in a 0. The fourth has no 1's at all, so the result is 0. The difference may seem small, even though the OR and XOR result in different answers. The main use of an XOR is to test two numbers against each other. If they are the same, the result will be all 0's, otherwise the answer will have 1's where there are differences. Compliment Complimenting a number results in the opposite state of all the 1's and 0's. Take the number 1111b. Complimenting results in 0000b. This is the simplest operator of all and the easiest to understand. It's uses are varied, but necessary, as you'll see later. Rotate These instructions rotate bits in a byte. The rotation can be left or right, and is done one bit each instruction. An example might be where the accumulator has a 11000011b in it. If we rotate left, the result will be 10000111b. You can see that bit 7 has now been moved into bit 0 and all the other bits have move 1 bit position in, the left direction. Clear This instruction clears, or zero's out the accumulator. This is the same as moving a 0 into the accumulator. This also clears the C flag and sets the Z flag. BOOLEAN There are other commands that deal with single bit values. In a program, an 8 bit byte location may be used as 8 separate flags or indicators, defined by the programmer, and used to signal events or conditions between separate parts of a program. This might be a bit that indicates whether a door is open or closed, or whether a key has been pressed or a switch is closed or open. These bits or flags are usually the mechanism that keeps law and order in the program, and insures that the program does not get "lost" or misdirected. We'll see how this works later. There are instructions to do the same things with bits that are done with bytes by other instructions. You can AND, OR, SET, CLEAR, or MOVE bits. You will see in a later lesson how these are used. BRANCHING There are also program flow commands. These are branches or jumps. They have several different names reflecting the way they do the jump or on what condition causes the jump, like an overflow or under flow, or the results being zero or not zero. But all stop the normal sequential execution of the program, and jump to another location, other than the next instruction in sequence. Jump on Bit These instructions let you make a jump based on whether a certain bit is set (a 1) or cleared (a 0). This bit can be the CY (carry) flag, the Z (zero) flag, or any other bit. Compare and jump These instructions compare two values and jump based on the result. In lesson 1 we looked at a glass filling with water and compared it to a full one in our mind, and if it wasn't full we looked at it some more. This caused that program to loop, or wait, until the glass was full before continuing on. Call There is also a variation on a jump that is referred to as a CALL. A CALL does a jump, but then eventually comes back to the place where the CALL instruction was executed and continues with the next instruction after the CALL. This allows the programmer to create little sub-programs, or subroutines, that do repetitive tasks needed by the main program. This saves programming time because once the subroutine is written, it can be used by the main program when ever it needs it, a kind of way to create your own instructions. DATA TRANSFER Moving These instructions do exactly what you would think. They move data around between the various registers and memory. Exchanging Exchanging is a variation on the move instruction. Here data is exchanged between two places. Exchange Digit This is a variation on the exchange instruction. Here the lower nibble, or digit, is exchanged with the lower nibble of the other location. The upper nibble is the left 4 bits (bits 4 through 7) and the lower nibble is the right 4 bits (bits 0 through 3). Swapping This is another variation on the exchange instruction, but here the data exchange occurs between the upper nibble and the lower nibble in the accumulator. This is the end of lesson 3. I've tried to show all the possible instructions without actually showing each. I will, in a later lesson, go into each of the 110 different instructions and explain each one. In the next lesson we will learn more about memory and all the possible ways to get to a memory location. My email address is here and I would welcome your questions or comments as you proceed through this course. Depending on the volume of mail I get, it may take a couple of days or so for me to get back to you, but be assured, I will get back to you. I really do want you to understand the information I'm presenting, and not being a professional teacher, I might not always explain things to the satisfaction of everyone, but with your patience, I'll do my best. My home page is http://www.hkrmicro.com/personal/index.html The DS5000 Micro controller. Well, finally, this is what all the previous lessons have been getting you ready for. To start looking at the micro we will be using in this course. All the previous lessons have been laying the ground work and basic concepts common to most micros. The one we will be using is the Dallas Semiconductor DS5000 micro controller. This chip, in my mind, is one of the neatest ones ever made. Dallas Semiconductor knew what to put in a chip. There are a few external parts needed along with the DS5000, but they are few, and [...]... address is popped off of the stack and placed into the PC so that the next instruction fetched is the one after the call This is sort of like stacking (pushing) books in a pile, one on top of another with the most recent addition on top of the stack Then as a book is taken off of the stack (popped) the next one is on top As each byte of the two byte return address is pushed onto the stack, the stack pointer... incremented and the first (low) byte is pushed onto the stack Then the stack pointer is incremented again and the second (high) byte is pushed When the return is encountered, the first (high) byte is popped off the stack and the stack pointer is decremented Then the the second (low) byte is popped off of the stack and then the stack pointer is decremented The stack pointer provides the address in internal... common The transmit out of the DS5000's serial port goes through a transmitter of the MAX232 chip through a connecting cable and into the receive of the PC's serial port The transmit out of the PC's serial port goes through a connecting cable through a receiver in the MAX232 chip and into the receive of the DS5000's serial port The ground or common on the PC's serial port is connected to the ground of the. .. start of the buffer ;put a 2 into the first location of the inc r0 mov @r0,#2 ;increment r0 to point to the second byte ;put a 2 into the second location of the mov mov inc add ;set r0 to the start of the buffer ;get the first 2 ;step to the second 2 ;add the second 2 to the first 2 buffer buffer r0,#buffer a,@r0 r0 a,@r0 segment memory memory org h'00 buffer: rs 2 The first part is the same with the. .. a period In the DS5000 there are many bit locations 128 of them were discussed in the previous lesson There are others like all the bits in the accumulator, all the bits in the p0, p1, p2, and p3 ports Each one is assigned an address by the DS5000 and can't be changed The first 128 are the 16 bytes of internal ram following the first 32 bytes Bit 0 of p0 is address 128 (80h) and bit 0 of p1 is address... ;get the state of the switch and put in led,c start ;mov the carry flag to the led ;jump to start This has the same result as the previous program, but doesn't assume anything about the other bits in p0 and p1 Also the equate only has to be made once at the start of the program, and thereafter the name or label is used instead of the bit number This makes things much simpler for the programmer Also the. .. in the use of the instruction, you always have to use the accumulator as either the source or the destination, but it shrinks the size of the instruction, so that more instructions can fit inside the micro This is a choice made by the maker of the micro, and is not up for discussion It's a trade off of flexibility vs size That's why you'll see lots of instructions that use the accumulator This is the. .. add 2 r0,#buffer @r0,#2 ;reserve two locations for the data ;set r0 to the start of the buffer ;put a 2 into the first location of the r0 @r0,#2 ;increment r0 to point to the second byte ;put a 2 into the second location of the r0,#buffer a,@r0 r0 a,@r0 ;set r0 to the start of the buffer ;get the first 2 ;step to the second 2 ;add the second 2 to the first 2 Line 1 is again a reserve storage directive,... stores a 2 in the first location of buffer The @ symbol tells the assembler that the following register holds the address (indirect) of where to put the 2 into So after this instruction, the first byte of buffer has a 2 in it Line 4 increments or steps r0 to the second location in buffer If r0 had the address of the first byte, then incrementing it by 1 now results in the address of the second byte... up in the accumulator is then stored in r3 The remainder in b is moved into the accumulator and the b register is loaded with a 10 Then the accumulator is divided by 10 The tens digit is now in the accumulator and the remainder, which is the ones digit is in the b register The routine is finished by moving the tens digit from the accumulator into r4 and the ones digit in the b register into r5 The routine . outcome of the instruction was. One is the Carry (C) flag. The other is the Zero (Z) flag. The C flag will be explained in the following example of addition. The Z flag, if set, says that the result. moves the first byte of buffer (the first 2)into the accumulator. Line 8 steps r0 to the address of the second 2 in buffer. Line 9 adds the second 2 to the first 2 and stores the result in the. various aspects of the operation of the DS5000. More on these as they come up later. Lastly there is one more area of memory that is divided into two parts. The program memory and the data memory.

Ngày đăng: 08/08/2014, 13:20

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan