Autumn Term Year 10 Slides
Contents This slide set contains slides for the following topics: Binary numbers: Week 7 Lesson 1 Binary arithmetic: Week 9 Lesson 1 Negative numbers: Week 10 Lesson 1 Hexadecimal numbers: Week 11 Lesson 1 Low and high level programming languages:Week 12 Lesson 1 Translating programming languages: Week 13 Lesson 1 Contents
Binary numbers: Week 7 Lesson 1
Humans count using decimal numbers (base 10). We use 10 units: 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9. 103 102 101 100 1000 10 1 5 4 9 (5 x 1000 = 5000) 5000 (0 x 100 = 0) + 0 (4 x 10 = 40) + 40 (9 x 1 = 9) + 9 =5049 Computers count using binary numbers (base 2). They use just 2 units: 0 and 1. 23 22 21 20 8 4 2 1 (1 x 8 = 8) 8 (1 x 4 = 4) + 4 (0 x 2 = 0) + 0 (1 x 1 = 1) + 1 =13 Binary numbers
Binary to denary Binary Denary 8 4 2 1 Binary numbers
Denary to binary Binary Denary 8 4 2 1 3 5 6 7 9 10 11 12 13 14 15 Binary numbers
And with 8 bits (a byte) you can represent 256 different numbers: 0 to 255 27 26 25 24 23 22 21 20 128 64 32 16 8 4 2 1 (1 x 128) (1 x 64) (1 x 32) (0 x 16) (1 x 8) (1 x 4) (0 x 2) (1 x 1) + 64 + 32 + 0 + 8 + 4 + 1 = 237 Binary numbers
How to… convert from binary to denary: Add up the column values for each ‘1’, e.g. 1110 = 8 + 4 + 2 + 0 = 14 convert from denary to binary: Take away the largest power of two you can and put a 1 for each number you take away and a 0 for each number you don’t use, e.g. 29 = 29 – 16 = 13 – 8 = 5 – 4 = 1 – 1 = 0 0001 1101 Binary numbers
Binary arithmetic: Week 9 Lesson 1
What do you need to know? Binary arithmetic: addition shifts: arithmetic shifts: logical overflow. Binary arithmetic
Addition 1 4 + 5 1 + 2 1 + 1 = 2; so write a 0 down and carry 1 over into the next column on the left 3 1 7 + 10 In column 2, 1 + 1 + 1 = 1 lot of 4 plus ; so write a 1 down and carry 1 over into the next column on the left Binary arithmetic
Shifts: arithmetic Shifts are also known as ‘bitwise operations’ Left-shift 1 (decimal 23) = (decimal 46) This has the effect of multiplying by 2. A new 0 is shifted in. This has the effect of dividing by 2. The MSB value is always maintained; in this example a 0 is inserted. Right-shift 1 (decimal 23) = (decimal 11) Arithmetic right-shift best for signed two’s complement since this retains the MSB (i.e. sign) value Binary arithmetic
Shifts: logical Shifts are also known as ‘bitwise operations’ Left-shift 1 (decimal 23) = (decimal 46) This has the effect of multiplying by 2. A new 0 is shifted in. This has the effect of dividing by 2. In a logical shift a 0 is always inserted. Right-shift 1 (decimal 23) = (decimal 11) Logical shifts, which always copy in a 0, are ideal for unsigned binary numbers. So arithmetic and logical shifts appear to have the same effect, however there is a different result with negative numbers (see later). Binary arithmetic
Overflow 5 1 15 + 20 1 5 1 4 + 9 Solution: use the most significant bit as an overflow flag which is set to 1 when an overflow occurs. Drawback: there are fewer bits available to represent numbers. Binary arithmetic
Negative numbers: Week 10 Lesson 1
How to represent negative numbers There is a fixed number of bits in a processor to store data: 32-bit 64-bit At GCSE you only look at 8-bit numbers, but the principle is the same. How many positive whole number values can be stored: in 4 bits? in 8 bits? So, how are negative numbers represented in 8 bits? Sign and magnitude Two’s complement Negative numbers
Sign and magnitude The most significant bit is the ‘sign bit’: 1 = minus and 0 = plus –27 26 25 24 23 22 21 20 1 – 64 32 16 8 4 2 = –13 Conversion to denary is the same: just add up the values. Remember that the number is now negative. If the sign is 0, it is a positive number. Negative numbers
Sign and magnitude So now that we only have 7 bits available: –27 26 25 24 23 22 21 20 1 – 64 32 16 8 4 2 = –13 So now that we only have 7 bits available: the largest positive number that can be represented is 0111 1111 (i.e. +127) the largest negative number that can be represented is 1111 1111 (i.e. –127) Negative numbers
The problems with sign and magnitude Both 1000 0000 and 0000 0000 represent 0. It wastes one binary code. Addition doesn’t always work. 1 = +7 –5 + -12 Negative numbers
The alternative: two’s complement The most significant bit is a minus number as well as a sign bit. –27 26 25 24 23 22 21 20 1 –128 64 32 16 8 4 2 -128 = –115 The largest positive number that can be represented is 0111 1111 (i.e. +127) The largest negative number that can be represented is 1000 0000 (i.e. –128) Negative numbers
The same sum in two’s complement 1 = +7 –5 + +2 And there’s only one way of representing 0: 0000 0000 Negative numbers
Shifts: arithmetic – with negative numbers Left-shift 1 (decimal 23) = (decimal 46) This has the effect of multiplying by 2. A new 0 is shifted in. This has the effect of dividing by 2. The MSB value is always maintained; in this example a 1 is inserted. Right-shift 1 (decimal -105) = (decimal -53) Arithmetic right-shift best for signed two’s complement since this retains the MSB (i.e. sign) value. Negative numbers
Shifts: logical This has the effect of multiplying by 2. Left-shift 1 (decimal 23) = (decimal 46) This has the effect of multiplying by 2. A new 0 is shifted in. This has the effect of dividing by 2. In a logical shift a 0 is always inserted Right-shift 1 (decimal 23) = (decimal 11) Logical shifts, which always copy in a 0, are ideal for unsigned binary numbers. Negative numbers
Hexadecimal numbers: Week 11 Lesson 1
Recall: Humans count using decimal numbers (base 10). We use 10 symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9. Computers count using binary numbers (base 2). They use just 2 symbols: 0 and 1. Another way to represent the 0s and 1s is to use hexadecimal notation (base 16). This uses 16 symbols (the CAPITALS are important): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. Hexadecimal numbers
Hexadecimal values You don’t need to memorise this table! Remember: Denary Pure Binary Hex 0000 1 0001 2 0010 3 0011 4 0100 5 0101 6 0110 7 0111 8 1000 9 1001 10 1010 A 11 1011 B 12 1100 C 13 1101 D 14 1110 E 15 1111 F 16 10000 You don’t need to memorise this table! Remember: 1010 is 10102 and A16 1510 is 11112 and F16 Then you can work out the rest. Hexadecimal numbers
For example: 163 162 161 160 4096 256 16 1 2 8 A (2 x 4096 = 8192) +8192 (8 x 256 = 2048) + 2048 (10 x 16 = 160) + 160 (1 x 1 = 1) + 1 = 10406 Hexadecimal numbers
Convert unsigned binary to hexadecimal First remember that a ‘nibble’ is 4 bits Break the binary number into 4-bit nibbles. Translate each nibble into the hex equivalent. Write the hex digits together. Hexadecimal numbers
How to… convert from unsigned binary to denary: Example translate 111110102 This can be split into 11112 10102 The hex code for 11112 is F16 The hex code for 10102 is A16 Therefore, the number is FA16 Now do Activity 11.1.1 Hexadecimal numbers
How to… convert Hexadecimal to unsigned binary The quickest way to translate back to denary is to repeat the process in reverse. Convert each hex character to a 4-bit nibble Combine the nibbles into a single 8 bit binary value Hexadecimal numbers
Why is hexadecimal used to represent binary? Think about it! Hexadecimal numbers
Because: It’s a way of writing binary numbers in a shorter number of digits. Easier for humans to read than pure binary. The computer still just understands binary. A common misconception is that hex takes up less memory. It doesn’t, remember all 0s and 1s in memory. Hexadecimal numbers
Low and high level programming languages: Week 12 Lesson 1
Machine code Recall that all a computer recognises is binary code. We now know that 0s and 1s can represent different things and so far have looked at representing numbers. 0s and 1s also represent machine instructions: That is, binary digits that represent the program in such a way that the processor can run it. The software instructions which make the hardware work. These machine instructions relate to a single processor, e.g. ARM Intel Low and high level programming languages
central heating system ARM inside… eReader Lego® Mindstorms® robot tablet smartphone smart glasses camera Raspberry Pi® fitness band car digital TV washing machine Key points: Huge range of portable and mobile devices have ARM processors. ARM processors also used for embedded applications in cars, homes, hospitals, etc. games console central heating system Low and high level programming languages
Programming in machine code The first programmers had to program in machine code. Binary code representing instructions Programming at the hardware level (the machine), the lowest level. Problems? Slow. Error-prone. Hard to remember. Each machine code is for a single machine. Low and high level programming languages
Assembly language Uses mnemonics i.e. abbreviations to represent the instructions, e.g. MOV – move data CMP – compare values Then following the instruction, the data is referenced: MOV R3, #5 which instructs the machine to move 5 to register 3. This is still low level in terms of accessing hardware. Tell pupils they don’t have to program in assembly language, just know what it is They will learn more about processors later in the course. Low and high level programming languages
Machine code versus assembly language 1110 0011 1010 0000 0000 0000 0000 0010 MOV R0, #2 1110 0010 0100 0011 0001 0000 0000 1010 SUB R1, R3, #10 1110 0000 1000 0000 0000 0000 0000 0001 ADD R0, R0, R1 1110 0000 0000 0011 0000 0101 1001 0100 MUL R3, R4, R5 For reference: This code was assembled with "armasm --debug --cpu=6” 1110 0011 1010 0000 0000 0000 0000 0010 MOV r0,#2 1110 0010 0100 0011 0001 0000 0000 1010 SUB r1,r3,#0xa 1110 0000 1000 0000 0000 0000 0000 0001 ADD r0,r0,r1 1110 0000 0000 0011 0000 0101 1001 0100 MUL r3,r4,r5 Low and high level programming languages
Programming in assembly language It is easier to program in assembly language than machine code. But: Still error-prone. Difficult to debug. Hard to learn. Low and high level programming languages
High level programming languages As computer science developed and hardware became more available, there was a need to write programs faster. High level programming languages were developed to do this. The first ones were more suited to specific applications, e.g. FORTRAN – scientific COBOL – commercial Python is an example of a high level programming language. Low and high level programming languages
Characteristics of high level programming languages More ‘English-like’ Less error-prone Easier to debug Quicker to write Can work on different machines (transportable) Low and high level programming languages
Assembling an algorithm Python Assembly language start Total = 0 MOV R0, #0 // Total for Index in range (6): MOV R1, #1 // Index Total =+ Index; loop print(Total) ADD R0, R0, R1 // Total = Total + Index ADD R1, R1, #1 // Index = Index + 1 CMP R1, #6 // Check loop cond. BNE loop // Branch if not finished BL print // Call print() For reference: This is one of several ways the assembler could have implemented the algorithm. Alternatives would be compare with 5 and use the Equal To or Less Than condition, or to count down towards zero. Pupils don’t need to know this, but counting down to zero would usually be the most efficient approach. By using the ‘SUBS’ instruction to decrement the loop counter, the processor would check for a zero result as part of the operation, removing the need for the CMP instruction and thus saving one instruction execution per iteration of the loop. A prime objective of assembly language programming is to improve the speed of execution by eliminating unnecessary instructions. The inputs to a subroutine get passed in registers R0 to R3, and the return value (if any) is stored in R0 at the end of the subroutine. Low and high level programming languages
Why bother coding in assembly language? Most software is NOT written in assembly language because: it’s time-consuming to develop it’s not portable across different types of processors. BUT, assembly language is useful because: it enables programmers to write very efficient, performance- critical code it gives you a better insight into how the processor works it can be used to debug code that isn’t working as expected it’s fun! Key points: Majority of code produced to run a smartphone won’t be written in assembly language, but the ‘mission critical’ bits will be. A compiler can never produce as efficient code as a programmer writing in assembly language who really understands how the processor works. Low and high level programming languages
Translating programming languages: Week 13 Lesson 1
Why don’t programmers have to program in machine code? Recall that these are machine instructions are binary code. That is, binary digits that represent the program in such a way that the processor can run it. Fortunately we have such things as assemblers, compilers and interpreters. These do the translation into binary that the computer can understand and run. Translating programming languages
Assemblers What do they do? Translate assembly code into machine code. Features Each different type of machine/processor requires an assembler of its own (e.g. ARM). Translating programming languages
Compilers What do they do? Translate a program written in a high level language (e.g. Python, C) into assembly language or machine code (the executable file). Take an assembly language program and converts each mnemonic into its binary equivalent. Features Checks for errors before the exe is produced. Produce final software for distribution. Can be slow to compile. End user is unable to hack into the code. Translating programming languages
Interpreters What do they do? Translates one line at a time at run time (e.g. Java). Features Slower than compilers. But can be executed straight away. No object code generated. Often used when developing new programs. End users can easily see the code. Translating programming languages