5 1 Examples with 3 Digit Numbers Example
5 -1 Examples with 3 -Digit Numbers • Example 1: – 10’s complement representation of 247 • 247 (positive number) – 10’s complement of 247 • 1000 – 247 = 753 (negative number) • Example 2: – 10’s complement of 17 • 1000 – 017 = 983 • Example 3: – 10’s complement of 777 • Negative number because first digit is 7 • 1000 – 777 = 223 • Sign-magnitude value = -223 ITEC 1000 Introduction to Information Technologies
2 Introduction • Basic operations are defined by the hardware – CPU instructions (operations) – Data flow – I/O • Store program: in the memory (von Neumann) • Power: not from complexity, but speed • LMC: MIT 1965; 1979 (Madnick) ITEC 1000 Introduction to Information Technologies
3 Goals • • layout of LMC functionality of LMC instruction set LMC instruction codes a simple program fetch-execute instruction cycle von Neumann architecture ITEC 1000 Introduction to Information Technologies
The Little Man Computer ITEC 1000 Introduction to Information Technologies 4
5 LMC Architecture • Memory – 100 mailboxes: (address, content) – Addresses: 00 -99 – Content: 999 • Calculator: – S 999; S = + or – – No overflow – Can remember the sign • Location counter • LM • I/O: 2 baskets “ 01”, “ 02” ITEC 1000 Introduction to Information Technologies
6 Instruction Code Format Instruction mailbox address 5 25 Instruction 5 == LOAD Code 525 == LOAD value from mailbox 25 onto calculator ITEC 1000 Introduction to Information Technologies
Instruction Cycle • Fetch: Little Man finds out what instruction he is to execute • Execute: Little Man performs the work ITEC 1000 Introduction to Information Technologies 6 -7
Fetch Portion of Fetch and Execute Cycle 1. Little Man reads the address from the location counter 2. He walks over to the mailbox that corresponds to the location counter ITEC 1000 Introduction to Information Technologies 6 -8
Fetch, cont. 3. And reads the number on the slip of paper (he puts the slip back in case he needs to read it again later) ITEC 1000 Introduction to Information Technologies 6 -9
Execute Portion 1. The Little Man goes to the mailbox address specified in the instruction he just fetched 2. He reads the number in that mailbox (he remembers to replace it in case he needs it later) ITEC 1000 Introduction to Information Technologies 6 -10
Execute, cont. 3. He walks over to the calculator and punches the number in 4. He walks over to the location counter and clicks it, which gets him ready to fetch the next instruction ITEC 1000 Introduction to Information Technologies 6 -11
von Neumann Architecture (1945) • Stored program concept • Memory is addressed linearly • Memory is addressed by location number without regard to content Copyright ITEC 1000 2013 John to Information Technologies Introduction Wiley & Sons, Inc. 6 -12
13 LMC (Basic) Instructions • • • Notations: Memory = M, calculator=C LOAD (5)99 from M to C STORE (3)99 from C to M STORE (4) XX Store XX to C ADD (1)99 M to C SUBTRACT (2)99 M from C INPUT (9)01 from “ 01” to C OUTPUT (9)02 from C to “ 02” HALT (0) ITEC 1000 Introduction to Information Technologies
14 A Simple Program Mailbox Code Meaning 00 901 Reads from In basket to C 01 399 Store C to 99 (op 1) 02 901 Reads from In basket to C 03 199 Add 99 (op 1) to C 04 902 Output C to output basket 05 000 Halt The logic is: sequential The program Adds two numbers and Outputs the result ITEC 1000 Introduction to Information Technologies
15 Add 3 numbers and Output Result Mailbox Code 00 01 02 03 04 05 06 07 08 ITEC 1000 901 399 901 398 901 199 198 902 000 Meaning Reads from In basket to C Store C to 99 (op 1) Reads from In basket to C Store C to 98 (op 2) Reads from In basket to C Add 99 (op 1) to C Add 98 (op 2) to C Output C to output basket Halt Introduction to Information Technologies
16 Extended instructions • • BRANCH (JUMP) (6)99 BRANCH ON ZERO (7)99 BRANCH ON POSITIVE (8)99 Used to define new types of logic – Branches – Repetitions ([while; for] loops) ITEC 1000 Introduction to Information Technologies
17 Primary Programming Constructs • • • Algorithms Control structures Sequence Branching Flowcharts Pseudo-code ITEC 1000 Introduction to Information Technologies
18 English vs. Code add two numbers (comments from before) input first number, input second number, add them together, output the result 901, 399, 901, 199, 902, 000 ITEC 1000 Introduction to Information Technologies
19 Program / Algorithm Development • • develop concept (in English) sketch control flow of concept fill in details convert into programming language ITEC 1000 Introduction to Information Technologies
20 Three Control Structures • Sequence • Branching • Looping ITEC 1000 Introduction to Information Technologies
21 Sequence • standard operation of a computer • instructions are executed sequentially (from consecutively numbered memory locations) • program counter increments consecutively through memory slots ITEC 1000 Introduction to Information Technologies
22 Example of Sequence How do we get from here to Union Station (using the TTC)? Directions to get to Union Station are followed in sequence Get The bus Get The subway Leave at Union Station Directions are an everyday program/algorithm ITEC 1000 Introduction to Information Technologies
23 Flowchart for Sequence start end ITEC 1000 Introduction to Information Technologies
24 Branching allows program execution to depend on input example: “I Want to wait as little as possible for the Bus” program: Is 196 Line too long ? Go get the 106. ITEC 1000 Introduction to Information Technologies
25 Flowchart for Branching 196 Line long? yes Get 106 no ITEC 1000 Introduction to Information Technologies
26 Pseudocode is a brief English description of an algorithm (the comments from before) • it is not natural English • it does not have complete detail • it is not syntactically correct code ITEC 1000 Introduction to Information Technologies
27 Grading Example start input grade yes 85 -100 Honors no yes 60 -84 Pass no Fail ITEC 1000 end Introduction to Information Technologies
28 Summary • Programming == algorithm development + coding • algorithm development requires clarity of thought • coding requires attention to detail ITEC 1000 Introduction to Information Technologies
29 Looping • Examples of looping • Structured programming • Writing pseudocode ITEC 1000 Introduction to Information Technologies
30 Looping allows program to repeatedly execute a given section example: deal all the cards from the deck program: check to see if there are more cards deal a card < repeat > ITEC 1000 Introduction to Information Technologies
31 Flowchart for Looping more cards? yes deal a card no ITEC 1000 Introduction to Information Technologies
32 Example of Looping Mailbox Code Meaning <assume that calculator value represents number of remaining cards > 19 20 21 299 822 630 Subtract 99 from C (consider 99 holding the value 1) if positive, branch to 22 branch to 30 (skip code to deal card) 22… 29 < code to deal cards > 619 branch to 19 (repeat until no more cards) 30 99 ITEC 1000 < remainder of program > 001 Introduction to Information Technologies
33 Structured Programming • Any program can be written with only the three primary constructs • Branch unconditionally (goto) is not externally available ITEC 1000 Introduction to Information Technologies
34 Natural Language to Pseudocode 1. Use a large sauce pan Bring 4 litres of water to a rolling boil Add salt to taste, if desired 2. Add contents of package Stir gently 3. Boil 11 minutes 4. Remove from heat 5. Drain well 6. Serve ITEC 1000 Introduction to Information Technologies
35 For A payroll System : 1. Input the employees payroll data 2. If hours worked is greater than 40 then calculate overtime pay 3. Calculate gross pay for the employee - Gross Pay = hourly pay times 40 plus overtime 4. Calculate tax for the employee ITEC 1000 Introduction to Information Technologies
36 Summary • Natural language is NOT pseudocode • Programming requires great explicitness of thought • Details are easier to fill in after you have the algorithm figured out ITEC 1000 Introduction to Information Technologies
Program 2 00 01 02 03 04 05 06 07 08 09 10 11 ITEC 1000 IN STO SUB BRP LDA SUB OUT HLT “ 01” 10 “ 01” 11 10 08 10 11 “ 02” 901 310 901 311 210 808 510 211 902 Input from “ 01” to C [op 1] Store from C to M 10 Input from “ 01” to C [op 2] Store from C to M 11 Subtract op 2 -op 1 = r 1 If r 1 > 0 goto M 08 else load op 1 into C op 1 -op 2 = r 2 Output to “ 02” Data Introduction to Information Technologies 37
Program 2 00 01 02 03 04 05 06 07 08 09 10 11 ITEC 1000 IN STO SUB BRP LDA SUB OUT HLT “ 01” 10 “ 01” 11 10 08 10 11 “ 02” 901 310 901 311 210 808 510 211 902 Input from “ 01” to C [op 1] Store from C to M 10 Input from “ 01” to C [op 2] Store from C to M 11 Subtract op 2 -op 1 = r 1 If r 1 > 0 goto M 08 else load op 1 into C op 1 -op 2 = r 2 Output to “ 02” Data Introduction to Information Technologies 38
How to do C-(A+B) ? Read A Read B Sum D=A+B Read C Sub C-D ITEC 1000 00 01 02 03 04 05 06 07 08 Input 901 Store 20 320 Input 901 ADD 20 120 Store 21 321 Input 901 SUB 21 221 Output 902 Halt 0 Introduction to Information Technologies 39
Compare A and B. Output Letter C if A greater and D otherwise Read A Read B IF A>B Then Write “C” Else Write “D” ITEC 1000 00 01 02 03 04 05 06 07 08 09 10 Input Store 20 Input Sub 20 BRP 08 STVAL 67 Output Halt STVAL 68 Output Halt Introduction to Information Technologies 901 320 901 220 (B-A) 808 467 (43 hex) 902 000 468 (44 hex) 902 0 40
41 Other Alternative 00 01 02 03 04 05 06 07 08 09 ITEC 1000 Input Store 20 Input Sub 20 BRP 07 STVAL 67 Branch 08 STVAL 68 Output Halt 901 320 901 220 (B-A) 807 467 608 468 902 0 Introduction to Information Technologies
Subtract 10 -8 and Add to A (input). If result >10 Output 1 else Output 2 A=10 B=8 C=A-B Read D E=D-C IF E>10 write “ 1” Else write “ 2” ITEC 1000 Introduction to Information Technologies 42
43 Subtract 10 -8 and Add to A (input). If result >10 Output 1 else Output 2 00 01 02 03 04 05 06 07 08 ITEC 1000 Stval 08 Store 21 Stval 10 Sub 21 Store 21 Input Add 21 Store 21 St. Val 10 408 321 410 221 321 901 121 221 410 9 Sub 21 221 (10 – Result) 10 BRP 13 813 11 Staval 50 450 12 Branch 14 614 13 Stval 49 449 14 Output 902 15 Halt 0 Introduction to Information Technologies
Multiply A*B Read A Read B C=0 While A>0 Do C=C+B A=A-1 Enddo Write B ITEC 1000 Introduction to Information Technologies 44
00 01 02 03 04 05 06 07 ITEC 1000 Input Store 21 Input Store 22 Stval 0 Store 23 Staval 1 Store 24 901 321 902 322 400 323 411 324 08 09 10 11 12 13 14 15 16 17 18 Load 23 Add 22 Store 23 Load 21 Sub 24 BZ 16 Store 21 Branch 08 Load 23 Output Halt Introduction to Information Technologies 523 122 323 521 224 716 321 608 523 902 0 45
46 Is the LMC a “real” computer? • CPU instruction set is created using binary numbers rather than decimal • Instructions are performed in a simple electronic way using logic based upon Boolean algebra instead of having a Little Man running around a mailroom. ITEC 1000 Introduction to Information Technologies
47 LMC to “real” computer • LMC also uses fetch-execute cycles • LMC processes instructions in sequence • Mailboxes are memory • LM and calculator are CPU • Baskets provide I/O ITEC 1000 Introduction to Information Technologies
48 Summary • LMC is almost real computer! • Computers are simple • Programming is easy ITEC 1000 Introduction to Information Technologies
- Slides: 48