Presentation is loading. Please wait.

Presentation is loading. Please wait.

Examples with 3-Digit Numbers

Similar presentations


Presentation on theme: "Examples with 3-Digit Numbers"— Presentation transcript:

1 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

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)

3 Goals layout of LMC functionality of LMC LMC instruction set
LMC instruction codes a simple program fetch-execute instruction cycle von Neumann architecture

4 The Little Man Computer

5 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”

6 Instruction Code Format
Instruction mailbox address Instruction 5 == LOAD Code 525 == LOAD value from mailbox 25 onto calculator

7 Instruction Cycle Fetch: Little Man finds out what instruction he is to execute Execute: Little Man performs the work

8 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

9 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)

10 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)

11 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

12 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.

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)

14 A Simple Program Mailbox Code Meaning 00 901 Reads from In basket to C
Store C to 99 (op1) Reads from In basket to C Add 99 (op1) to C Output C to output basket Halt The logic is: sequential The program Adds two numbers and Outputs the result

15 Add 3 numbers and Output Result
Mailbox Code Meaning Reads from In basket to C Store C to 99 (op1) Reads from In basket to C Store C to 98 (op2) Reads from In basket to C Add 99 (op1) to C Add 98 (op2) to C Output C to output basket Halt

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)

17 Primary Programming Constructs
Algorithms Control structures Sequence Branching Flowcharts Pseudo-code

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

19 Program / Algorithm Development
develop concept (in English) sketch control flow of concept fill in details convert into programming language

20 Three Control Structures
Sequence Branching Looping

21 Sequence standard operation of a computer
instructions are executed sequentially (from consecutively numbered memory locations) program counter increments consecutively through memory slots

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

23 Flowchart for Sequence
start end

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.

25 Flowchart for Branching
196 Line long? Get 106 yes no

26 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

27 Grading Example 85-100 Honors 60-84 Pass Fail start input grade yes no
end

28 Summary Programming == algorithm development + coding
algorithm development requires clarity of thought coding requires attention to detail

29 Looping Examples of looping Structured programming Writing pseudocode

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 >

31 Flowchart for Looping more cards? deal a card yes no

32 Example of Looping Mailbox Code Meaning
<assume that calculator value represents number of remaining cards > Subtract 99 from C (consider 99 holding the value 1) if positive, branch to 22 branch to 30 (skip code to deal card) 22… < code to deal cards > branch to 19 (repeat until no more cards) 30 < remainder of program >

33 Structured Programming
Any program can be written with only the three primary constructs Branch unconditionally (goto) is not externally available

34 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

35 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

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

37 Program 2 00 IN “01” 901 Input from “01” to C [op1]
01 STO Store from C to M10 02 IN “01” 901 Input from “01” to C [op2] 03 STO Store from C to M11 04 SUB Subtract op2-op1 = r1 05 BRP If r1 > 0 goto M08 06 LDA else load op1 into C 07 SUB op1-op2 = r2 08 OUT “02” 902 Output to “02” 09 HLT 10 Data 11 Data

38 Program 2 00 IN “01” 901 Input from “01” to C [op1]
01 STO Store from C to M10 02 IN “01” 901 Input from “01” to C [op2] 03 STO Store from C to M11 04 SUB Subtract op2-op1 = r1 05 BRP If r1 > 0 goto M08 06 LDA else load op1 into C 07 SUB op1-op2 = r2 08 OUT “02” 902 Output to “02” 09 HLT 10 Data 11 Data

39 How to do C-(A+B) ? 00 Input 901 01 Store 20 320 02 Input 901
03 ADD 04 Store 05 Input 901 06 SUB 07 Output 902 08 Halt 0 Read A Read B Sum D=A+B Read C Sub C-D

40 Compare A and B. Output Letter C if A greater and D otherwise
00 Input 901 01 Store 02 Input 901 03 Sub (B-A) 04 BRP 05 STVAL (43 hex) 06 Output 902 07 Halt 000 08 STVAL (44 hex) 09 Output 902 10 Halt 0 Read A Read B IF A>B Then Write “C” Else Write “D”

41 Other Alternative 00 Input 901 01 Store 20 320 02 Input 901
03 Sub (B-A) 04 BRP 05 STVAL 06 Branch 07 STVAL 08 Output 902 09 Halt 0

42 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”

43 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 01 Store 02 Stval 03 Sub 04 Store 05 Input 901 06 Add 07 Store 08 StVal Sub (10 – Result) 10 BRP 11 Staval 12 Branch 13 Stval 14 Output 902 15 Halt 0

44 Multiply A*B Read A Read B C=0 While A>0 Do C=C+B A=A-1 Enddo
Write B

45 00 Input 901 01 Store 02 Input 902 03 Store 04 Stval 05 Store 06 Staval 1 411 07 Store 08 Load 09 Add 10 Store 11 Load 12 Sub BZ Store 15 Branch 16 Load Output 902 18 Halt 0

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.

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

48 Summary LMC is almost real computer! Computers are simple
Programming is easy


Download ppt "Examples with 3-Digit Numbers"

Similar presentations


Ads by Google