Difference between revisions of "CSC103: DT's Notes 1"
Line 1,233: | Line 1,233: | ||
We won't learn all of the them. The ones we touched on in the lab is sufficient to understand how a processor works. | We won't learn all of the them. The ones we touched on in the lab is sufficient to understand how a processor works. | ||
+ | |||
+ | ====Studying an Assembly Language Program==== | ||
+ | |||
+ | Let's take the solution program for the last problem of the [[CSC103 Assembly Language Lab (version 2) 2013| laboratory]] | ||
+ | you just did and go through it, one instruction at a time. | ||
+ | |||
+ | The program is the following: | ||
+ | |||
+ | <br /> | ||
+ | <code><pre> | ||
+ | ; Solution for Problem 5 | ||
+ | ; D. Thiebaut | ||
+ | ; First initialize both variables with 55 | ||
+ | start: lod-c 55 | ||
+ | sto var1 | ||
+ | sto var2 | ||
+ | |||
+ | ; now loop and increment Location 15 and decrement Location 16 | ||
+ | ; the loop is endless. | ||
+ | loop: lod 15 | ||
+ | add-c 1 | ||
+ | sto 15 | ||
+ | lod 16 | ||
+ | dec | ||
+ | sto 16 | ||
+ | jmp loop | ||
+ | hlt | ||
+ | @15 | ||
+ | 0 | ||
+ | 0 | ||
+ | </pre><code> | ||
+ | First, the lines starting with semi colons are comments, and not part of the actual program. They allow the programmers | ||
+ | to add extra information for other programmers who may read the code. | ||
+ | |||
+ | {| | ||
+ | ! instruction | ||
+ | ! comment | ||
+ | |- | ||
+ | | | ||
+ | start: lod-c 55 | ||
+ | | start: is a label. It is a word we choose that cannot be confused with an instruction. It is a way | ||
+ | of documenting the program. The instruction loads the constant 55 into the AC register. | ||
+ | |- | ||
+ | | | ||
+ | sto 15 | ||
+ | sto 16 | ||
+ | | | ||
+ | store the contents of the AC register (which now contains 55) at Memory Locations 15 and 16. | ||
+ | |- | ||
+ | | | ||
+ | ; now loop and increment Location 15 and decrement Location 16 | ||
+ | ; the loop is endless. | ||
+ | | | ||
+ | Two comments to indicate what is going on next. | ||
+ | |- | ||
+ | | | ||
+ | loop: lod 15 | ||
+ | | | ||
+ | We give a name to the current memory location and call it "loop". The instruction tells the | ||
+ | processor to go to Address 15 and load the number there into AC. | ||
+ | |- | ||
+ | | | ||
+ | add-c 1 | ||
+ | | | ||
+ | Add the constant 1 to AC. | ||
+ | |- | ||
+ | | | ||
+ | sto 15 | ||
+ | | | ||
+ | Store AC back at memory location 15 | ||
+ | |- | ||
+ | | | ||
+ | lod 16 | ||
+ | | | ||
+ | Now load the contents of Memory Location 16 into AC. | ||
+ | |- | ||
+ | | | ||
+ | dec | ||
+ | | | ||
+ | Decrement AC by 1 (always by 1 for INC and DEC) | ||
+ | |- | ||
+ | | | ||
+ | sto 16 | ||
+ | | | ||
+ | Store AC back at Memory Location 16. | ||
+ | |- | ||
+ | | | ||
+ | jmp loop | ||
+ | | | ||
+ | Force the processor to set its PC register to the address corresponding to the '''loop''' label. | ||
+ | The processor will go back to this address and execute the '''lod 15''' instruction. | ||
+ | |- | ||
+ | | | ||
+ | hlt | ||
+ | | | ||
+ | Halt. The processor will stop when it reaches this instruction. But... because we have | ||
+ | a program with an endless or ''infinite'' loop, the processor will never reach this instruction. | ||
+ | Nonetheless, it is good practice to always have a HLT instruction to mark the end of a program. | ||
+ | |- | ||
+ | | | ||
+ | @15 | ||
+ | | | ||
+ | We now indicate that we are going to store some variables starting at Address 15. | ||
+ | |- | ||
+ | | | ||
+ | 0 | ||
+ | | | ||
+ | The word at Memory Location 15 is initialized with 0. | ||
+ | |- | ||
+ | | | ||
+ | 0 | ||
+ | | | ||
+ | The word at Memory Location 16 (next logical address after 15) is initialized with 0 as well. | ||
+ | |} | ||
+ | |||
Here we summarize the important points you should remember from this section. | Here we summarize the important points you should remember from this section. |
Revision as of 11:37, 22 September 2013
--© D. Thiebaut 08:10, 30 January 2012 (EST)