Hexidecimal, CPU, and the Toy Computer (Week 4) Flashcards
Definition : Byte
A byte is a sequence of 8 bits
It encodes 256 distinct values (0 to 255)
Each distinct value represents a unique sequence of 8 bits
Definition : Hexidecimal Numbers
Hexadecimal numbers use 16 symbols per digit: numerals 0-9 and letters A-F (base-16).
Why use hexadecimal?
Because binary numbers rapidly grow too large to be read effectively by humans, e.g. 2021(decimal) equals 111 1110 01012(binary)
To represent binary numbers in a compact format, hexadecimal numbers are particularly suitable
How many bits is one Hexidecimal digit?
1 hexadecimal digit represents exactly 4 binary digits (no more, no less)
Convert Hexidecimal to Decimal :
A
10
Convert Hexidecimal to Decimal :
B
11
Convert Hexidecimal to Decimal :
C
12
Convert Hexidecimal to Decimal :
D
13
Convert Hexidecimal to Decimal :
E
14
Convert Hexidecimal to Decimal :
F
15
Convert Hexidecimal Digits to Bits :
1
0001
Convert Hexidecimal Digits to Bits :
7
0111
Convert Hexidecimal Digits to Bits :
3B
00111011
Convert Hexidecimal to Decimal :
4B
75
Convert Hexidecimal to Decimal :
A7
167
Convert Hexidecimal to Decimal :
FF
255
Convert Hexidecimal to Decimal :
EC
236
How is color encoded?
In Hexidecimal eg.
ffffff
000000
00ff00
How many bits are 7 bytes?
56
What does a CPU do?
The CPU controls all other parts of a computer (by sending out signals on the bus)
Examples of what CPU controls: RAM, storage devices and peripherals (e.g. mouse, printer, etc.)
What can a CPU do?
CPUs can execute only up to a few dozen (sometimes up to a few hundred) of quite basic instructions
- Arithmetic: add, subtract, multiply, divide, etc. different types of numbers
- Move data around (fetch instructions and data from RAM, store results back in RAM, move date from and to Input/Output devices)
- Instructions to control the flow of execution, i.e. instructions that decide what to do next based on previous computation results
What is the CPU instruction cycle?
CPUs perform billions of operations per second (e.g. 3 billion per second if your CPU runs at 3 GHz clock speed)
An instruction cycle features the following (simplified and) ever repeating steps:
1. Clock cycle 1: Fetch next instruction from RAM
2. Clock cycle 2: Decode what to do
3. Clock cycle 3: Execute it
What is a Toy Computer?
The toy computer is a tool to simulate a highly simplified Computer/CPU in order to demonstrate how it works in principle.
It can run simple computer programs made of very basic instructions (similar to instructions of real CPUs).
The “Toy” uses one accumulator => storage area inside the CPU with capacity of one data item
Process of the Toy Computer?
Programs are stored in RAM
* Each line represents one memory location
* Each location stores an instruction or data item
The CPU executes one instruction after another starting at the top.
What do the following commands do?
GET PRINT STOP
Answer :
GET get a number from keyboard into accumulator PRINT print contents of accumulator STOP stop running
What do the following commands do?
LOAD Val STORE M ADD Val SUB Val M Num
Answer :
LOAD Val load accumulator with Val (Val unchanged) STORE M store contents of accumulator into memory location called M (accumulator unchanged) ADD Val add Val to contents of accumulator (Val unchanged) SUB Val subtract Val from contents of accumulator (Val unchanged) M Num before program runs, set this memory location (called M) to numeric value Num
What do the following commands do?
GOTO L IFPOS L IFZERO L
Answer :
GOTO L go to instruction labeled L IFPOS L go to instruction labeled L if accumulator is >= zero IFZERO L go to instruction labeled L if accumulator is zero
Determine the Output of the following Toy Computer code :
1 GET (3) 2 PRINT 3 END
Output :
error: invalid instruction 'end' at line 3
Determine the Output of the following Toy Computer code :
1 GET (3) 2 PRINT 3 STOP
Output :
3
Determine the Output of the following Toy Computer code :
1 Top GET (7, 9, 2, 0) 2 PRINT 3 GOTO Top 4 IFZERO End 5 End STOP
Output :
error: infinite loop
Determine the Output of the following Toy Computer code :
1 Top GET (3, 7, 0) 2 IFZERO Bot 3 ADD sum 4 STORE sum 5 GOTO Top 6 Bot LOAD sum 7 PRINT 8 STOP 9 sum
Output :
10
Determine the Output of the following Toy Computer code :
1 Top GET (3, 7, 0) 2 IFZERO Bot 3 ADD sum 4 STORE sum 5 GOTO Top 6 Bot LOAD sum 7 PRINT 8 STOP
Output :
line 3 error: there is no instruction labeled sum line 4 error: there is no instruction labeled sum line 3 error: there is no instruction labeled sum line 4 error: there is no instruction labeled sum line 6 error: there is no instruction labeled sum
Determine the Output of the following Toy Computer code :
1 Top GET (0, 3, 7) 2 IFZERO Bot 3 ADD sum 4 STORE sum 5 GOTO Top 6 Bot LOAD sum 7 PRINT 8 STOP 9 sum
Output :
0
What value(s) of GET would cause an error?
1 GET 2 Loop Sub 1 3 PRINT 4 IFZERO Exit 5 GOTO Loop 6 Exit STOP
If 0 or less (0,-1,-2, -3, …) is the input, the program will loop infinitely.
error: infinite loop
What value(s) of GET would cause an error?
1 GET 2 Loop PRINT 3 Sub 1 4 IFPOS Loop 5 STOP
None. All numbers will eventually be subtracted below zero, and even letters will be considered not positive.
Determine the Output of the following Toy Computer code :
1 GET (8) 2 STORE length 3 GET (4) 4 GOTO Top 5 End LOAD perimeter 6 ADD length 7 STORE perimeter 8 LOAD width 9 Top SUB 1 10 STORE width 11 IFPOS End 12 LOAD perimeter 13 PRINT 14 STOP 15 length 16 width 17 perimeter 0
Output :
32
How to check if this (or any other) program works correctly?
Testing: run the program and see if it works
Enter unexpected input data
* a negative or a floating-point number, e.g. 1.25
* a character, e.g. T
Above all else, programmers should test “edge cases”
* Does the program work with no input or only one number as input?
* Does it work with very small or very large numbers?
* If it works with 3 and with 1000 numbers as input, it is likely to work with 5 or 777 numbers (or anything in between) as well.