Examples with 3-Digit Numbers 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
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)
Goals layout of LMC functionality of LMC LMC instruction set LMC instruction codes a simple program fetch-execute instruction cycle von Neumann architecture
The Little Man Computer
LMC Architecture Memory Calculator: Location counter LM 100 mailboxes: (address, content) Addresses: 00-99 Content: 999 Calculator: S999; S = + or – No overflow Can remember the sign Location counter LM I/O: 2 baskets “01”, “02”
Instruction Code Format Instruction mailbox address 5 25 Instruction 5 == LOAD Code 525 == LOAD value from mailbox 25 onto calculator
Instruction Cycle Fetch: Little Man finds out what instruction he is to execute Execute: Little Man performs the work
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
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)
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)
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
von Neumann Architecture (1945) Stored program concept Memory is addressed linearly Memory is addressed by location number without regard to content Copyright 2013 John Wiley & Sons, Inc.
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)
A Simple Program Mailbox Code Meaning 00 901 Reads from In basket to C 01 399 Store C to 99 (op1) 02 901 Reads from In basket to C 03 199 Add 99 (op1) 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
Add 3 numbers and Output Result Mailbox Code Meaning 00 901 Reads from In basket to C 01 399 Store C to 99 (op1) 02 901 Reads from In basket to C 03 398 Store C to 98 (op2) 04 901 Reads from In basket to C 05 199 Add 99 (op1) to C 06 198 Add 98 (op2) to C 07 902 Output C to output basket 08 000 Halt
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)
Primary Programming Constructs Algorithms Control structures Sequence Branching Flowcharts Pseudo-code
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
Program / Algorithm Development develop concept (in English) sketch control flow of concept fill in details convert into programming language
Three Control Structures Sequence Branching Looping
Sequence standard operation of a computer instructions are executed sequentially (from consecutively numbered memory locations) program counter increments consecutively through memory slots
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
Flowchart for Sequence start end
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.
Flowchart for Branching 196 Line long? Get 106 yes no
Pseudocode 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
Grading Example 85-100 Honors 60-84 Pass Fail start input grade yes no end
Summary Programming == algorithm development + coding algorithm development requires clarity of thought coding requires attention to detail
Looping Examples of looping Structured programming Writing pseudocode
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 >
Flowchart for Looping more cards? deal a card yes no
Example of Looping Mailbox Code Meaning <assume that calculator value represents number of remaining cards > 19 299 Subtract 99 from C (consider 99 holding the value 1) 20 822 if positive, branch to 22 21 630 branch to 30 (skip code to deal card) 22… < code to deal cards > 29 619 branch to 19 (repeat until no more cards) 30 < remainder of program > 99 001
Structured Programming Any program can be written with only the three primary constructs Branch unconditionally (goto) is not externally available
Natural Language to Pseudocode Use a large sauce pan Bring 4 litres of water to a rolling boil Add salt to taste, if desired Add contents of package Stir gently Boil 11 minutes Remove from heat Drain well Serve
For A payroll System : Input the employees payroll data If hours worked is greater than 40 then calculate overtime pay Calculate gross pay for the employee - Gross Pay = hourly pay times 40 plus overtime Calculate tax for the employee
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
Program 2 00 IN “01” 901 Input from “01” to C [op1] 01 STO 10 310 Store from C to M10 02 IN “01” 901 Input from “01” to C [op2] 03 STO 11 311 Store from C to M11 04 SUB 10 210 Subtract op2-op1 = r1 05 BRP 08 808 If r1 > 0 goto M08 06 LDA 10 510 else load op1 into C 07 SUB 11 211 op1-op2 = r2 08 OUT “02” 902 Output to “02” 09 HLT 10 Data 11 Data
Program 2 00 IN “01” 901 Input from “01” to C [op1] 01 STO 10 310 Store from C to M10 02 IN “01” 901 Input from “01” to C [op2] 03 STO 11 311 Store from C to M11 04 SUB 10 210 Subtract op2-op1 = r1 05 BRP 08 808 If r1 > 0 goto M08 06 LDA 10 510 else load op1 into C 07 SUB 11 211 op1-op2 = r2 08 OUT “02” 902 Output to “02” 09 HLT 10 Data 11 Data
How to do C-(A+B) ? 00 Input 901 01 Store 20 320 02 Input 901 03 ADD 20 120 04 Store 21 321 05 Input 901 06 SUB 21 221 07 Output 902 08 Halt 0 Read A Read B Sum D=A+B Read C Sub C-D
Compare A and B. Output Letter C if A greater and D otherwise 00 Input 901 01 Store 20 320 02 Input 901 03 Sub 20 220 (B-A) 04 BRP 08 808 05 STVAL 67 467 (43 hex) 06 Output 902 07 Halt 000 08 STVAL 68 468 (44 hex) 09 Output 902 10 Halt 0 Read A Read B IF A>B Then Write “C” Else Write “D”
Other Alternative 00 Input 901 01 Store 20 320 02 Input 901 03 Sub 20 220 (B-A) 04 BRP 07 807 05 STVAL 67 467 06 Branch 08 608 07 STVAL 68 468 08 Output 902 09 Halt 0
Subtract 10-8 and Add to A (input) 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”
Subtract 10-8 and Add to A (input) Subtract 10-8 and Add to A (input). If result >10 Output 1 else Output 2 00 Stval 08 408 01 Store 21 321 02 Stval 10 410 03 Sub 21 221 04 Store 21 321 05 Input 901 06 Add 21 121 07 Store 21 221 08 StVal 10 410 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
Multiply A*B Read A Read B C=0 While A>0 Do C=C+B A=A-1 Enddo Write B
00 Input 901 01 Store 21 321 02 Input 902 03 Store 22 322 04 Stval 0 400 05 Store 23 323 06 Staval 1 411 07 Store 24 324 08 Load 23 523 09 Add 22 122 10 Store 23 323 11 Load 21 521 12 Sub 24 224 BZ 16 716 Store 21 321 15 Branch 08 608 16 Load 23 523 17 Output 902 18 Halt 0
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.
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
Summary LMC is almost real computer! Computers are simple Programming is easy