Download presentation
Presentation is loading. Please wait.
Published byGriselda Pope Modified over 9 years ago
1
Lecture 2: Advanced Instructions, Control, and Branching EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr. Rozier (UM)
2
COURSE PLAN FOR TODAY 1.Representing Numbers 2.Logical Operations 3.Control Instructions
3
REPRESENTING NUMBERS
4
Unsigned Binary Integers Given an n-bit number Range: 0 to +2 n – 1 Example 0000 0000 0000 0000 0000 0000 0000 1011 2 = 0 + … + 1×2 3 + 0×2 2 +1×2 1 +1×2 0 = 0 + … + 8 + 0 + 2 + 1 = 11 10 Using 32 bits 0 to +4,294,967,295
5
2s-Complement Signed Integers Given an n-bit number Range: –2 n – 1 to +2 n – 1 – 1 Example 1111 1111 1111 1111 1111 1111 1111 1100 2 = –1×2 31 + 1×2 30 + … + 1×2 2 +0×2 1 +0×2 0 = –2,147,483,648 + 2,147,483,644 = –4 10 Using 32 bits –2,147,483,648 to +2,147,483,647
6
2s-Complement Signed Integers Bit 31 is sign bit – 1 for negative numbers – 0 for non-negative numbers –(–2 n – 1 ) can’t be represented Non-negative numbers have the same unsigned and 2s-complement representation Some specific numbers – 0:0000 0000 … 0000 – –1:1111 1111 … 1111 – Most-negative:1000 0000 … 0000 – Most-positive:0111 1111 … 1111
7
2s-Complement Signed Integers Complement and add 1 – Complement means 1 → 0, 0 → 1 Example: negate +2 +2 = 0000 0000 … 0010 2 –2 = 1111 1111 … 1101 2 + 1 = 1111 1111 … 1110 2
8
Hexadecimal Base 16 – Compact representation of bit strings – 4 bits per hex digit 000004010081000c1100 100015010191001d1101 2001060110a1010e1110 3001170111b1011f1111 Example: eca8 6420 1110 1100 1010 1000 0110 0100 0010 0000
9
LOGICAL OPERATIONS
10
Bitwise Operations Sometimes we want to operate on registers in a bitwise fashion. Logical Operations Shifts Rotates
11
Logical Operations Instructions for bitwise manipulation Example bic r0, r1, r2 @ bitwise NAND r1 and r2, store the result in r0 OperationCJavaARM Bitwise AND&&and Bitwise OR||orr Bitwise XOR^^eor Bitwise NAND~(A & B) bic
12
The ARM Barrel Shifter ARM architectures have a unique piece of hardware known as a barrel shifter. – Device moves bits in a word left or right. Most processors have stand alone instructions for shifting bits. ARM allows shifts as part of regular instructions. Allows for quick multiplication and division.
13
The ARM Barrel Shifter
14
Reality of the hardware – There are no shift instructions – Barrel shifter can be controlled WITH an instruction – Can only be applied to operand 2 on instructions which use the ALU
15
Types of Shifting Logical Shifts – lsl – left – lsr – right Arithmetic Shifts – asr – right Rotates – ror – right – rrx – right with extend
16
Example mov r0, r1, lsl #1 This would perform a logical shift left of 1 bit on r1, and then copy the result into r0. mov r0, r1, lsl r2 This would do the same as before, but use the value of r2 for the shift amount.
17
Logical Shifts Logical shifting a number left or right has the effect of doubling or halving it. lsl – Highest order bit shifts into the carry flag – Lowest order bit is filled with 0. lsr – Lowest order bit shifts into the carry flag – Highest order bit is filled with 0. LSLCb7b6b5b4b3b2b1b0 Before010001111 After100011110
18
Arithmetic Shift Preserves the sign bit. asr – Extends the sign bit to the second most significant – Shifts the least significant into the carry flag. LSLCb7b6b5b4b3b2b1b0 Before010001111 After111000111
19
Rotations Rotates bits from low order to high order ror – Moves bits from the lowest order to the highest, setting the carry bit in the process with the last bit rotated out. rrx – Always and only rotates by one position. – Carry flag is dropped into the highest order bit. Lowest order bit is moved to the carry flag LSLCb7b6b5b4b3b2b1b0 Before000001111 After110000111
20
Rotations ror rrx
21
Adding a Shift or Rotate Shifts and rotates can be used with: – adc, add, and – bic – cmn, cmp – eor – mov, mvn – orr – rsb – sbc, sub – teq, tst
22
BRANCHING
23
Branching What makes a computer different from a calculator?
24
Branching Branches allow us to transfer control of the program to a new address. – b ( ) – bl ( ) b start bl start
25
Branch (b) Branch, possibly conditionally, to a new address. beq subroutine @ If Z=1, branch Good practice to use bal instead of b.
26
Branch with link (bl) Branch, possibly conditionally, to a new address. – Before the branch is complete, store the PC in the LR. – Allows easy return from the branch. bleq subroutine @ If Z=1, branch, saving the PC
27
Branch with link (bl) How do we get back once we’ve saved the PC? mov pc, lr Moves the contents of the link register to the program counter.
28
Implementing If Statements C code: if (i == j) f = g+h; else f = g - h; ARM code cmp r0, r1 @ Set flags via r0-r1 and discard beq Else add r2, r3, r4 @ r2 = r3 + r4 bal Exit Else: sub r2, r3, r4 @ r2 = r3 + r4 Exit:
29
Implementing Loop Statements C code: while (i < j) i += 1; ARM code Loop: cmp r0, r1 bge Exit add r0, r0, #1 bal Loop Exit: i < j? i=i+1 i<j Exit i>=j
30
Basic Blocks A basic block is a sequence of instructions with – No embedded branches (except at end) – No branch targets (except at beginning) A compiler identifies basic blocks for optimization An advanced processor can accelerate execution of basic blocks
31
EXERCISE THE HUMAN PROCESSOR
32
WRAP UP
33
For next time Read Chapter 2, Sections 2.6 – 2.8 Learn about control, branch, loops, etc.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.