Arithmetic and Logical Operators

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/23

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

24 Terms

1
New cards

CPU Registers

  • Registers are represented with a 4-bit number.

  • R0–R7: lower registers (bit-4 = 0)

  • R8-R12: higher registers (bit-4=1)

    • specific instructions store data to them automatically

  • R13 is the Stack Pointer (SP) 

  • R14 is the Link Register (LR) 

  • R15 is the program counter (PC) 

  • Current Program Status Register (CPSR)

<ul><li><p><span style="font-family: &quot;Open Sans&quot;, sans-serif">Registers are represented with a 4-bit number.</span></p></li><li><p><span style="font-family: &quot;Open Sans&quot;, sans-serif">R0–R7: lower registers (bit-4 = 0)</span></p></li><li><p><span style="font-family: &quot;Open Sans&quot;, sans-serif">R8-R12: higher registers (bit-4=1)</span></p><ul><li><p><span style="font-family: &quot;Open Sans&quot;, sans-serif">specific instructions store data to them automatically</span></p></li></ul></li><li><p><span style="font-family: &quot;Open Sans&quot;, sans-serif">R13 is the Stack Pointer (SP)&nbsp;</span></p></li><li><p><span style="font-family: &quot;Open Sans&quot;, sans-serif">R14 is the Link Register (LR)&nbsp;</span></p></li><li><p><span style="font-family: &quot;Open Sans&quot;, sans-serif">R15 is the program counter (PC)&nbsp;</span></p></li><li><p><span style="font-family: &quot;Open Sans&quot;, sans-serif">Current Program Status Register (CPSR)</span></p></li></ul><p></p>
2
New cards

Move

Useful to copy data between registers, or set an immediate to a register

<p><span style="font-family: &quot;Open Sans&quot;, sans-serif">Useful to copy data between registers, or set an immediate to a register</span></p>
3
New cards

Adding

<p></p>
4
New cards

Subtracting

<p></p>
5
New cards

Complex Calculations

must be broken down in smaller steps —> compiler

<p><span style="font-family: &quot;Open Sans&quot;, sans-serif">must be broken down in smaller steps —&gt; compiler</span></p>
6
New cards

Number Encoding

  • The "add" instruction assumes that its operands are encoded using two’s complement

  • Since each register holds a 32-bit number:

  • Different instructions can have different expectations for how numbers are mapped or coded in registers

<ul><li><p><span style="font-family: &quot;Open Sans&quot;, sans-serif">The "add" instruction assumes that its operands are encoded using two’s complement</span></p></li><li><p><span style="font-family: &quot;Open Sans&quot;, sans-serif">Since each register holds a 32-bit number:</span></p></li></ul><ul><li><p><span style="font-family: &quot;Open Sans&quot;, sans-serif">Different instructions can have different expectations for how numbers are mapped or coded in registers</span></p></li></ul><p></p>
7
New cards

Two’s Compliment

  • Positive number is directly converted 

  • To obtain negative number flip bits and add 1 

  • To obtain the corresponding positive value flip bits again and add 1

<ul><li><p><span style="font-family: &quot;Open Sans&quot;, sans-serif">Positive number is directly converted&nbsp;</span></p></li><li><p><span style="font-family: &quot;Open Sans&quot;, sans-serif">To obtain negative number flip bits and add 1&nbsp;</span></p></li><li><p><span style="font-family: &quot;Open Sans&quot;, sans-serif">To obtain the corresponding positive value flip bits again and add 1</span></p></li></ul><p></p>
8
New cards

Recording Operation Effects

  • Sometimes, more than 32-bits is needed for the outcome of an operation

    • Carry bit

    • Overflow

    • Comparison

  • ARM uses a special register (CPSR) to record unusual arithmetic results

  • We will discuss this a lot when we get to loop and if statements

9
New cards

Current Program Status Register (CPSR)

  • An ARM register that records the state of the program

    • Arithmetic instructions will affect its value every time

  • N bit - "negative flag”: instruction result was negative

  • Z bit -"zero flag”: instruction result was zero

  • C bit - "carry flag”: Instruction causes a carry-out or borrow

  • V bit - "overflow flag”: Instruction produces an overflow in 2's complement numbers

<ul><li><p><span style="font-family: &quot;Open Sans&quot;, sans-serif">An ARM register that records the state of the program</span></p><ul><li><p><span style="font-family: &quot;Open Sans&quot;, sans-serif">Arithmetic instructions will affect its value every time</span></p></li></ul></li><li><p><span style="font-family: &quot;Open Sans&quot;, sans-serif">N bit - "negative flag”: instruction result was negative</span></p></li><li><p><span style="font-family: &quot;Open Sans&quot;, sans-serif">Z bit -"zero flag”: instruction result was zero</span></p></li><li><p><span style="font-family: &quot;Open Sans&quot;, sans-serif">C bit - "carry flag”: Instruction causes a carry-out or borrow</span></p></li><li><p><span style="font-family: &quot;Open Sans&quot;, sans-serif">V bit - "overflow flag”: Instruction produces an overflow in 2's complement numbers</span></p></li></ul><p></p>
10
New cards

Integer Arithmetic Operations in ARM

<p></p>
11
New cards

Bit Shifting

  • Key operation to manage individual bits in a register

  • Can be used for fast multiplication/division with powers of 2

    • ASR arithmetic-shift-right

    • LSL logical-shift-left

    • LSR logical-shift-right

    • ROR right-rotation

  • You can only use R0-R7

  • Non-immediate operations must use the same source and destination register (i.e, lsl r0, r0, r1)

12
New cards

Logical Left Bit Shifting

<p></p>
13
New cards

Logical Right Bit Shifting

<p></p>
14
New cards

Arithmetic Right Bit Shifting

Bits filled in are the same as the MSB e.g. if MSB is  0 then filled with 0s and if MSB is 1 then filled with 1s

<p><span style="font-family: &quot;Open Sans&quot;, sans-serif">Bits filled in are the same as the MSB e.g. if MSB is&nbsp; 0 then filled with 0s and if MSB is 1 then filled with 1s</span></p>
15
New cards

Rotate Right Bit Shifting

Any bit that leaves from the right becomes the MSB

<p><span style="font-family: &quot;Open Sans&quot;, sans-serif">Any bit that leaves from the right becomes the MSB</span></p>
16
New cards

Logical Operators

  • Using these instructions you can apply all the Boolean operations you learned in the first part of this module

    • AND: Rn = Rd & Rm

    • EOR: Rn = Rd ^ Rm (xor) 

    • ORR: Rn = Rd | Rm 

    • BIC: Rn = Rd & ~Rm 

  • update the N and Z flags according to the result for the PCSR

17
New cards

bit-by-bit AND

<p></p>
18
New cards

bit-by-bit ORR

<p></p>
19
New cards

bit-by-bit EOR (Exclusive OR)

<p></p>
20
New cards

bit-by-bit MVN (Move and Not)

<p></p>
21
New cards

Immediate Operands

  • Programmers require operations with constants

    • incrementing a number (e.g. counting, array indexes)

    • initialising the value held by a program variable

  • Most instructions can replace one of the input register with an immediate, i.e. an integer value

  • Because ARM MIPS aims to optimize the space of a program, immediate size varies

  • You may need to load big values in a register in multiple steps

  • or use memory to store constants

22
New cards

Adding a Constant

  • Constants are frequently prepended with the symbol #

  • They are interpreted as a signed integer

  • Encoded using two’s complement

<ul><li><p><span style="font-family: &quot;Open Sans&quot;, sans-serif">Constants are frequently prepended with the symbol #</span></p></li><li><p><span style="font-family: &quot;Open Sans&quot;, sans-serif">They are interpreted as a signed integer</span></p></li><li><p><span style="font-family: &quot;Open Sans&quot;, sans-serif">Encoded using two’s complement</span></p></li></ul><p></p>
23
New cards

Integer Arithmetic Operations in ARM

<p></p>
24
New cards

Loading Large Immediates

  • mov can load values between 0x00000000 to 0x0000FFFF to registers

  • What about larger integers?

  • movt can load 16-bit values into the upper 16 bits of a register (lower 16-bits remain the same)

    • mov r0, 0x1 @ r0 = 0x10000 

mov r0, 0xffff @ Loading 0x7fffffff 

movt r0, 0x7fff