Assembly Language Assembly Language A Level Only Photocopiable/digital resources may only be copied by the purchasing institution on a single site and for their own use © ZigZag Education, 2016
Machine Code Each type of CPU is designed to carry out a set of specific instructions. Each instruction is represented by a binary number. This is called machine code. Machine Code Assembly Language 1110 0001 1010 0000 0011 0000 0000 1001 MOV R3, R9 1110 0010 0100 0011 0001 0000 0000 1010 SUB R1, R3, #10 1110 0000 1000 0000 0000 0000 0000 0001 ADD R0, R0, R1 It is hard for humans to read and write machine code so assembly language was developed to make it easier. Assembly language features a set of mnemonics, each one representing one machine code instruction. © ZigZag Education, 2016
These are the mnemonics that are commonly used in assembly language: Description LDR Load from memory location into a register ADD Add data in a register SUB Subtract data in a register STR Store values from register into a memory location B Branch to a marked position in the program CMP Compare values in a register AND Bitwise logical AND operation ORR Bitwise logical OR operation XOR Bitwise logical XOR operation MVN Bitwise logical NOT operation LSL Logically shift the value left, used for multiplication LSR Logically shift the value right, used for division HALT Stops the program © ZigZag Education, 2016
Instruction Format ADD MOV SUB R0, R1, #5 R0, R2 R2, R2, R4 This is the standard format for writing instructions in assembly language. opcode: the operation code is a single instruction operands: the values the instruction will use ADD R0, R1, #5 MOV R0, R2 SUB R2, R2, R4 operation destination input input © ZigZag Education, 2016
Storage Two different types of storage are used when the CPU is executing instructions. Registers Storage locations contained within the CPU which are much faster to access than memory Memory The RAM is located outside the CPU and is slower to access than registers. © ZigZag Education, 2016
Addressing There are different methods to access data and instructions in memory; these are called memory addressing modes. Immediate The data is hard-coded into the instruction. It is the fastest method as it doesn’t use memory. Direct The instruction refers directly to a location in memory (either a register or a memory location). ADD R0, R1, #5 Direct Direct Immediate © ZigZag Education, 2016
Example LDR LDR SUB STR R0, 102 R1, 101 R2, R0, R1 R2, 100 Registers Loads the value from memory location 102 into register 0 LDR R0, 102 Registers R0 20 R1 5 R2 15 Loads the value from memory location 101 into register 1 LDR R1, 101 Subtracts the value in R1 from the value in R0, stores the result in R2 SUB R2, R0, R1 Memory 100 15 101 5 102 20 Stores the value from R2 in memory location 100 STR R2, 100 © ZigZag Education, 2016
Logical Shift 1 1 1 = 45 = 90 (×2) = 180 (×4) Multiplication and division are very CPU-intensive operations; logical shift is an alternative method that can be used for multiplication and division by powers of 2. 1 1 = 45 1 = 90 (×2) = 180 (×4) As you can see, shifting the number one place to the left is the equivalent to multiplying by 2 and shifting by two places is the equivalent to multiplying by 4. © ZigZag Education, 2016
Branching is used to jump to different points in the program. Opcode Operands Description MOV R0, #50 The value 50 is moved into register 0 R1, #0 The value 0 is moved into register 1 CMP R0, R1 The values stored in registers 0 and 1 are compared BGT numIsGT If the value stored in register 0 is greater than the value stored in register 1, the program branches to the numIsGT label STR R1, 100 HALT numIsGT: A label used to define a point in the program R0, 100 Stores the value stored in register 0 in memory location 100 Stops the program © ZigZag Education, 2016
END