Difference between revisions of "CSC103: DT's Notes 1"
Line 1,190: | Line 1,190: | ||
that is much easier to remember than the number. Writing a series of mnemonics to instruct the processor | that is much easier to remember than the number. Writing a series of mnemonics to instruct the processor | ||
to execute a series of action is the process of writing an ''assembly-language program''. | to execute a series of action is the process of writing an ''assembly-language program''. | ||
− | + | <br /> | |
{| style="width:100%; background:#FFD373" | {| style="width:100%; background:#FFD373" | ||
|- | |- | ||
Line 1,196: | Line 1,196: | ||
===Instructions and Assembly Language=== | ===Instructions and Assembly Language=== | ||
|} | |} | ||
− | + | <br /> | |
The first mnemonic we will look at is LOD-C ''n''. It means LOAD a Constant into the AC register. LOD-C | The first mnemonic we will look at is LOD-C ''n''. It means LOAD a Constant into the AC register. LOD-C | ||
is always followed by a number. | is always followed by a number. | ||
Line 1,234: | Line 1,234: | ||
</center> | </center> | ||
<br /> | <br /> | ||
+ | |||
+ | {| style="width:100%; background:#FFD373" | ||
+ | |- | ||
+ | | | ||
+ | ===A First Program=== | ||
+ | |} | ||
+ | <br /> | ||
+ | Let's write a program that takes two numbers stored in memory, adds them up together, and stores their sum in a third number. When we deal with memory locations that contain numbers (our data), we refer to them as ''variables''. So another way of phrasing our problem is this: | ||
+ | |||
+ | ::Let's write a program that adds two variables together and stores their sum in a third variable. | ||
+ | |||
+ | Our program is below: | ||
+ | <br /> | ||
+ | <source lang="asm"> | ||
+ | |||
+ | @0 | ||
+ | LOD 10 | ||
+ | ADD 11 | ||
+ | STO 12 | ||
+ | HLT | ||
+ | |||
+ | @10 | ||
+ | 3 | ||
+ | 5 | ||
+ | 0 | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | That's it. That's our first program. If we enter it in the ''program'' window of the simulator, and then ''translate'' the program into instructions stored in memory, and then ''run'' the program, we should end up with the number 8 appearing in memory at Address 12. | ||
+ | |||
+ | Let's decipher the program first: | ||
+ | <br /> | ||
+ | {| | ||
+ | ! Instructions | ||
+ | ! Explanations | ||
+ | |- | ||
+ | | | ||
+ | @0 | ||
+ | | | ||
+ | This line instructs the translator or ''compiler'' that will take this program and generate a list of instructions in memory that it should store the next instructions at '''Address 0'''. Programs always start at 0. | ||
+ | |- | ||
+ | | | ||
+ | LOD 10 | ||
+ | | | ||
+ | Load the variable at Address 10 into the AC register. Whatever AC's original value, after this instruction it will contain whatever is stored at Address 10. We'll see in a few moments that the number is 2. So AC will contain 2. | ||
+ | |- | ||
+ | | | ||
+ | ADD 11 | ||
+ | | | ||
+ | Get the variable at Address 11 and add the number found to the number in the AC register. The AC register's value becomes | ||
+ | 2 + 3 or 5. | ||
+ | |- | ||
+ | | | ||
+ | STO 12 | ||
+ | | | ||
+ | Store the contents of the AC at Address 12. Since AC contains 5, the number is copied in the variable at Address 12. | ||
+ | |- | ||
+ | | | ||
+ | HLT | ||
+ | | | ||
+ | Halt! All programs must end. This is the instruction that makes a program end. When this instruction is executed, the simulator stops. Real processors will continue working, however. In real computers, when a program stops the Operating Systems resumes. | ||
+ | |- | ||
+ | | | ||
+ | | ||
+ | | | ||
+ | Blank line. A blank line in an assembly language program doesn't do anything. It is there just to create a division between two logically different section. Here we are separating the code (the instructions between LOD 10 and HLT) from the data (the three variables that follow). | ||
+ | |- | ||
+ | | | ||
+ | @10 | ||
+ | | | ||
+ | We now indicate to the translator that the next information should be stored starting at a new address, in this case Address 10. | ||
+ | |- | ||
+ | | | ||
+ | 3 | ||
+ | | | ||
+ | The number 3 should be stored at 10. This is our first variable. The translator accepts instructions or numbers. It doesn't matter. We could have entered our whole program as a series of numbers, if we knew ahead of time the code representing LOD, | ||
+ | ADD, STO and HLT. But it's much easier to use these ''mnemonics'' for the code. For the data, however, they are always numbers. So we simply enter their value. | ||
+ | |- | ||
+ | | | ||
+ | 5 | ||
+ | | | ||
+ | The number 5 should be stored at memory Address 11. Why 11? Because the previous variable was at Address 10. | ||
+ | |- | ||
+ | | | ||
+ | 0 | ||
+ | | | ||
+ | This 0 represents the third variable that will hold the sum of the first two variables. We store 0 because we want to have | ||
+ | some value there when the program starts. And because we know the program is supposed to store 8 in this variable, we | ||
+ | pick a number different from 8. 0 is often a good value to initialize variables with. | ||
+ | |||
+ | |} | ||
+ | |||
+ | |||
+ | |||
+ | |||
At this point you should continue with the [[CSC103 Assembly Language Lab (version 2) 2013| laboratory]] | At this point you should continue with the [[CSC103 Assembly Language Lab (version 2) 2013| laboratory]] | ||
prepared for this topic. It will take you step by step through the process of creating programs | prepared for this topic. It will take you step by step through the process of creating programs |
Revision as of 19:50, 28 September 2013
--© D. Thiebaut 08:10, 30 January 2012 (EST)