Dale - Computer Science Illuminated 240

Dale - Computer Science Illuminated 240 - have the...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
7.5 Assembly Language 213 A New Program Let’s make a step up in complexity and write a program to read in three numbers and write out their sum. How would we do this task by hand? If we had a calculator, we would first clear the total; that is, set the sum to zero. Then we would get the first number and add it to the total, get the second number and add it to the total, and finally get the third number and add it to the total. The result would be what is in the accumulator of the calculator. We can model the program on this algorithm. The first step is concrete: It involves setting a place in memory to zero. In fact, all of these statements look concrete. The most complex problem is that there are four identifiers that we must associate with places in memory, and this requires knowing how many places the program itself takes—that is, if we put the data at the end of the program. Let’s make this process easier by putting our data before the program. We can start associ- ating identifiers with memory locations beginning with location 0001 and
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: have the fetch-execute cycle skip over these places to continue with the program. In fact, we can assign identifiers to the memory locations and use these names later in the program. We set up space for the sum using the *W "D pseudocode so that we can set the contents to 0. We set up space for the three numbers using the *L RK pseudocode. sum: *W "D d#1 ;s7t up w.rd with z7r. as th7 c.nt7nts num0: *L RK d# ;s7t up a tw. byt7 b8.ck f.r num0 num : *L RK d# ;s7t up a tw. byt7 b8.ck f.r num num : *L RK d# ;s7t up a tw. byt7 b8.ck f.r num We can refer to these identifiers, and the assembler substitutes the addresses. Now all of the steps are concrete. The first step in the algorithm is to set the sum to zero. This is already done by the *W "D pseudo-op. The next two steps are repeated three times: Read a number and Add number to Set sum to 0 Read num1 Add num1 to sum Read num2 Add num2 to sum Read num3 Add num3 to sum Write sum...
View Full Document

Ask a homework question - tutors are online