Examples with 3-Digit Numbers
-
Upload
cameron-ramsey -
Category
Documents
-
view
248 -
download
0
description
Transcript of Examples with 3-Digit Numbers
ITEC 1000 Introduction to Information Technologies
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
5-1
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• LMC instruction set• LMC instruction codes• a simple program• fetch-execute instruction cycle• von Neumann architecture
ITEC 1000 Introduction to Information Technologies
4The Little Man Computer
ITEC 1000 Introduction to Information Technologies
5
LMC Architecture• Memory
– 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”
ITEC 1000 Introduction to Information Technologies
6
Instruction Code Format
Instruction mailbox address
5 25
Code 525 == LOAD value from mailbox 25 onto calculator
Instruction 5 == LOAD
ITEC 1000 Introduction to Information Technologies 6-7
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-8
Fetch Portion ofFetch 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-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)
ITEC 1000 Introduction to Information Technologies 6-10
Execute Portion1. 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-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
ITEC 1000 Introduction to Information TechnologiesCopyright 2013 John Wiley & Sons, Inc. 6-12
von Neumann Architecture (1945)
• Stored program concept• Memory is addressed linearly• Memory is addressed by location number
without regard to content
ITEC 1000 Introduction to Information Technologies
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 ProgramMailbox Code Meaning
00 901 Reads from In basket to C01 399 Store C to 99 (op1)02 901 Reads from In basket to C03 199 Add 99 (op1) to C04 902 Output C to output basket05 000 Halt
The logic is: sequentialThe program Adds two numbers and Outputs the result
ITEC 1000 Introduction to Information Technologies
15
Add 3 numbers and Output ResultMailbox Code Meaning
00 901 Reads from In basket to C01 399 Store C to 99 (op1)02 901 Reads from In basket to C03 398 Store C to 98 (op2)04 901 Reads from In basket to C05 199 Add 99 (op1) to C06 198 Add 98 (op2) to C07 902 Output C to output basket08 000 Halt
ITEC 1000 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 SequenceHow do we get from here to Union Station (using the TTC)?
Directions to get to Union Station are followed in sequenceGet The busGet The subwayLeave at Union Station
Directions are an everyday program/algorithm
ITEC 1000 Introduction to Information Technologies
23
Flowchart for Sequencestart
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
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 Examplestart
input grade
85-100
60-84
Honors
Pass
Fail
end
yes
yesno
no
ITEC 1000 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
morecards? yes
deal a card
no
ITEC 1000 Introduction to Information Technologies
32
Example of LoopingMailbox 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 2221 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
ITEC 1000 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 panBring 4 litres of water to a rolling boilAdd salt to taste, if desired
2. Add contents of packageStir gently
3. Boil 11 minutes4. Remove from heat5. Drain well6. Serve
ITEC 1000 Introduction to Information Technologies
35
1. Input the employees payroll data2. If hours worked is greater than 40
then calculate overtime pay3. Calculate gross pay for the
employee - Gross Pay = hourly pay times 40 plus overtime
4. Calculate tax for the employee
For A payroll System :
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
37
Program 200 IN “01” 901 Input from “01” to C [op1]01 STO 10 310 Store from C to M1002 IN “01” 901 Input from “01” to C [op2]03 STO 11 311 Store from C to M1104 SUB 10 210 Subtract op2-op1 = r105 BRP 08 808 If r1 > 0 goto M0806 LDA 10 510 else load op1 into C07 SUB 11 211 op1-op2 = r208 OUT “02” 902 Output to “02”09 HLT10 Data11 Data
ITEC 1000 Introduction to Information Technologies
38
Program 200 IN “01” 901 Input from “01” to C [op1]01 STO 10 310 Store from C to M1002 IN “01” 901 Input from “01” to C [op2]03 STO 11 311 Store from C to M1104 SUB 10 210 Subtract op2-op1 = r105 BRP 08 808 If r1 > 0 goto M0806 LDA 10 510 else load op1 into C07 SUB 11 211 op1-op2 = r208 OUT “02” 902 Output to “02”09 HLT10 Data11 Data
ITEC 1000 Introduction to Information Technologies
39
How to do C-(A+B) ?
Read ARead BSum D=A+BRead CSub C-D
00 Input 90101 Store 20 32002 Input 90103 ADD 20 12004 Store 21 32105 Input 90106 SUB 21 22107 Output 90208 Halt 0
ITEC 1000 Introduction to Information Technologies
40Compare A and B. Output Letter C if A greater and D otherwise
Read ARead BIF A>BThen Write “C”Else Write “D”
00 Input 90101 Store 20 32002 Input 90103 Sub 20 220 (B-A)04 BRP 08 80805 STVAL 67 467 (43 hex)06 Output 90207 Halt 00008 STVAL 68 468 (44 hex)09 Output 90210 Halt 0
ITEC 1000 Introduction to Information Technologies
41
Other Alternative00 Input 90101 Store 20 32002 Input 90103 Sub 20 220 (B-A)04 BRP 07 80705 STVAL 67 46706 Branch 08 60807 STVAL 68 46808 Output 90209 Halt 0
ITEC 1000 Introduction to Information Technologies
42Subtract 10-8 and Add to A (input). If result
>10 Output 1 else Output 2
A=10B=8C=A-BRead DE=D-CIF E>10 write “1”Else write “2”
ITEC 1000 Introduction to Information Technologies
43
Subtract 10-8 and Add to A (input). If result >10 Output 1 else Output 2
00 Stval 08 40801 Store 21 32102 Stval 10 41003 Sub 21 22104 Store 21 32105 Input 90106 Add 21 12107 Store 21 22108 StVal 10 410
9 Sub 21221 (10 – Result)10 BRP 13 81311 Staval 50 45012 Branch 14 61413 Stval 49 44914 Output 90215 Halt 0
ITEC 1000 Introduction to Information Technologies
44
Multiply A*BRead A Read B C=0 While A>0 Do C=C+B
A=A-1 EnddoWrite B
ITEC 1000 Introduction to Information Technologies
4500 Input 90101 Store 21 32102 Input 90203 Store 22 32204 Stval 0 40005 Store 23 32306 Staval 1 41107 Store 24 324
08 Load 23 52309 Add 22 12210 Store 23 32311 Load 21 52112 Sub 24 22413 BZ 16 71614 Store 21 32115 Branch 08 60816 Load 23 52317 Output 90218 Halt 0
ITEC 1000 Introduction to Information Technologies
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