131 Week 13 - Arithmetic Operations Flashcards
Move instruction
Used to copy data between registers, or set an immediate to a register.
Syntax:
mov destinationRegister, sourceRegister
E.g.,
mov r1, r2 (9Copy value of r2 into r1)
or
mov r1, #77 (set r1 to the value 77)
Adding instruction
Used to add the contents of 2 registers together. It assumes that its operands are encoded using two’s complement.
Syntax:
add destinationRegister, sourceRegister1, sourceRegister2
E.g.,
add r1, r1, r2 (add r1 and r2 and store the result in r1)
Subtracting instruction
Used to subtract the contents of one register from another.
Syntax:
sub destinationRegister, sourceRegister1, sourceRegister2
E.g.,
sub r1, r1, r2 (subtract r2 from r1 and store the result in r1)
which can also be written as:
sub r1, r2
Writing complex instructions
Unlike in high level languages, you cannot complete multiple operations on the same line.
Complex instructions must be broken down into smaller steps.
Current Program Status Register (CPSR)
An ARM register that records the state or effects of the program after an arithmetic instruction in case of need for a carry bit, overflow or comparisons.
Arithmetic instructions will affect its value every time
CPSR Flags
N bit: The “negative flag”. Flags if the instruction result was negative.
Z bit: The “zero flag”. Flags if the instruction result was zero.
C bit: The “carry flag”. Flags if the Instruction causes a carry-out or borrow.
V bit: The “overflow flag”. Flags if the Instruction produces an overflow in 2’s complement numbers.
Bit shifting
A key operation to manage individual bits in a register.
It can be used for fast multiplication/division with powers of 2 e.g., multiply by 16 or divide by 8.
You can only use shifts on r0-r7
Syntax
shiftType resultRegister, registerEvaluated, numShifts
Logical shift left
All bits are moved left by a specified amount.
All bits from the left side are deleted and the space at the right side is replaced with zeros.
Operator: lsl (Logical Shift Left)
Logical shift right
All bits are moved right by a specified amount.
All bits from the right side are deleted and the space at the left side is replaced with zeros.
Operator: lsr (Logical Shift Right)
Arithmetic shift right
All bits are shifted right by a specified amount similar to logical shift right. However,
If the number is positive (msb is 0), the empty space on the left will be filled with zeros.
If the number is negative (msb is 1), the empty space on the left will be filled with ones
This keeps the numbers in the correct representation in twos complement.
Operator: asr (Arithmetic Shift Right)
Rotate right shift
All bits are shifted right however instead of removing bits from the right side and adding new bits to the left side of the number, any bit that is removed from the right-hand side is added back to the left-hand side of the number.
Operator: ror (ROtate Right)
Logical operators
Allows you to apply boolean operations to registers.
Syntax:
logicalOperator resultRegister, registersEvaluated, registersEvaluated
AND
For all bits in the register, compare bits of the same position. If both bits at the position are 1, return 1, else return 0.
Operator: and
E.g., and r0,r1,r2
OR
For all bits in the register, compare bits of the same position. If both bits at a point in a register are 0, return 0, else return 1.
Operator: orr
E.g., orr r0,r1,r2
Exclusive OR
For all bits in the register, compare bits of the same position. If both bits at a point in a register are either 0 or 1, return 0, else return 1.
Operator: eor
E.g., eor r0,r1,r2
Move Not
Similar to move but you apply the NOT operation to all the bits in the register before moving.
Operator: mvn
E.g., mvn r0,r1
Immediate operand
An immediate operator is an operator that represents a fixed integer value – a constant.
An immediate operator can be used in place of a register input to input an integer instead of the contents of a register.
Immediates start with a # and are interpreted as a signed integer encoded using two’s complement.
Loading large numbers
Mov can only load between 0x00000000 to 0x0000FFFF to registers meaning you cannot load larger numbers using only mov.
Larger numbers can be loaded however, by splitting it into the upper and lower halves and loading them separately using movt which loads values into the upper 16 bits of a register, leaving the lower 16 bits alone.
E.g., storing 0x7fffffff in register r0:
load lower bits: mov r0, 0xffff
load upper bits: movt r0, 0x7fff