CONDITION CODE AND ARITHMETIC OPERATIONS 353156 Microprocessor Asst

  • Slides: 23
Download presentation
CONDITION CODE AND ARITHMETIC OPERATIONS 353156 – Microprocessor Asst. Prof. Dr. Choopan Rattanapoka and

CONDITION CODE AND ARITHMETIC OPERATIONS 353156 – Microprocessor Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat

Objectives To understand � Overflow and Underflow � CPSR condition flags � Arithmetic Operation

Objectives To understand � Overflow and Underflow � CPSR condition flags � Arithmetic Operation Instructions

Review : Binary Addition/Subtraction Binary number � 1001 + 1100 ? � 1100 -

Review : Binary Addition/Subtraction Binary number � 1001 + 1100 ? � 1100 - 0010 ? In 4 -bit microprocessor (2’s complement) � 1001 + 1100 ? � 1100 - 0010 ?

Overflow and Underflow Overflow occurs when an arithmetic operation yields a result that is

Overflow and Underflow Overflow occurs when an arithmetic operation yields a result that is greater than the range’s positive limit of 2 N-1 – 1 Underflow occurs when an arithmetic operation yields a result that is less than the range’s negative limit of -2 N-1

Example : overflow 510 + 610 (4 -bits 2’s complement) Note that 4 bits

Example : overflow 510 + 610 (4 -bits 2’s complement) Note that 4 bits can store +7 to -8 5 0101 + 6 + 0110 1011 -510 11 ≠ -5 OVERFLOW

Example : underflow -510 - 710 (4 -bits 2’s complement) Note that 4 bits

Example : underflow -510 - 710 (4 -bits 2’s complement) Note that 4 bits can store +7 to -8 -5 1011 + -7 + 1001 -1210 1 0100 410 -12 ≠ 4 UNDERFLOW

Status Flags in Program Status Register CPSR Flags N = Negative result from ALU

Status Flags in Program Status Register CPSR Flags N = Negative result from ALU flag Z = Zero result from ALU flag C = ALU opertion Carried out V = ALU operation o. Verflowed Arithmetic Instruction Negative (N = ‘ 1’) Bit 31 of the result has been set indicates a negative number in signed operation Zero (Z = ‘ 1’) Result of operation was zero Carry (C = ‘ 1’) Result was greater than 32 bits Overflow (V = ‘ 1’) Result was greater than 32 bits indicates a possible corruption of the sign bit in signed number

Experimentation with Condition Flags (1) Assume 4 bit-number 0011 + 0010 0 1 1

Experimentation with Condition Flags (1) Assume 4 bit-number 0011 + 0010 0 1 1 + 0 0 1 0 0 0 Negative (N) =0? Zero (Z) =0? Carry (C) =0? Overflow (V) =0? 1 0 1 Overflow (V) = MSB carry in XOR MSB carry out

Experimentation with Condition Flags (2) Assume 4 bit-number 1111 + 0010 0 1 1

Experimentation with Condition Flags (2) Assume 4 bit-number 1111 + 0010 0 1 1 1 1 + 0 0 1 0 Negative (N) =0? Zero (Z) =0? Carry (C) =1? Overflow (V) =0? 0 0 1 Why V is 0 ?

Experimentation with Condition Flags (3) Assume 4 bit-number 0111 + 0010 0 0 1

Experimentation with Condition Flags (3) Assume 4 bit-number 0111 + 0010 0 0 1 1 1 + 0 0 1 Negative (N) = ? Zero (Z) = ? Carry (C) = ? Overflow (V) = ? 0 0 1

Experimentation with Condition Flags (4) Assume 4 bit-number 1110 + 1001 0 0 1

Experimentation with Condition Flags (4) Assume 4 bit-number 1110 + 1001 0 0 1 1 1 0 + 1 0 0 1 1 0 Negative (N) = ? Zero (Z) = ? Carry (C) = ? Overflow (V) = ? 1 1 1

Exercise : Condition Flags Assume 4 bit-number, What are the values store in condition

Exercise : Condition Flags Assume 4 bit-number, What are the values store in condition flags (N, Z, C, V) of the following operations. � 1001 + 1010 � 1011 + 0101 � 1110 – 0111 � 0110 – 0010

More on ARM Assembly Instructions Largest category of ARM instructions, all sharing the same

More on ARM Assembly Instructions Largest category of ARM instructions, all sharing the same instruction format Load/Store architecture � These instructions only work on registers, NOT memory They each perform a specific operation on operands � 4 1. 2. 3. 4. fields format : 1, 2, 3, 4 Operation by name Operand getting result (“destination”) 1 st operand for operation (“source 1”) 2 nd operand for operation : register or shifted register or immediate (numerical constant)

Review : Instructions we’ve learnt 4 fields format : 1, 2, 3, 4 1.

Review : Instructions we’ve learnt 4 fields format : 1, 2, 3, 4 1. 2. 3. 4. Operation by name Operand getting result (“destination”) 1 st operand for operation (“source 1”) 2 nd operand for operation : register or shifted register or immediate (numerical constant) 1 2 3 4 ADD R 0, R 1, R 2 ADD R 0, R 1, #100 ADD R 0, R 1, #0 x 20

Review: Registers ARM processor has register R 0 – R 15 By convention, each

Review: Registers ARM processor has register R 0 – R 15 By convention, each register also has a name to make it easier to code. Register Number Name R 0 a 1 R 12 IP R 1 a 2 R 13 SP R 2 a 3 R 14 LR R 3 a 4 R 15 PC R 4 v 1 R 5 v 2 R 6 v 3 R 7 v 4 R 8 v 5 R 9 v 6 R 10 v 7 IP : Intra Procedure-call scratch register SP : Stack Pointer LR : Link Register PC : Program Counter ADD R 0, R 1, R 2 = ADD a 1, a 2, a 3 MOV R 0, R 15 = MOV a 1, PC

Arithmetic Operations ADD ADC SUB SBC RSB RSC reg 1, reg 1, reg 2,

Arithmetic Operations ADD ADC SUB SBC RSB RSC reg 1, reg 1, reg 2, reg 3 ; reg 1 reg 2 + reg 3 + C reg 2, reg 3 ; reg 1 reg 2 - reg 3 + C – 1 reg 2, reg 3 ; reg 1 reg 3 - reg 2 + C – 1 ** C = Carry bit in the CPSR **

Example 1 AREA ex 1, CODE, READONLY ENTRY start MOV a 1, #0 x

Example 1 AREA ex 1, CODE, READONLY ENTRY start MOV a 1, #0 x 5 MOV a 2, #0 x 1 A RSB a 3, a 1, a 2 END After the execution of the program is terminated, What are the value store in r 0, r 1, and r 2 What are the value in N, Z, C, V bit of CSPR

Example 2 AREA ex 2, CODE, READONLY ENTRY start MOV a 1, #0 x

Example 2 AREA ex 2, CODE, READONLY ENTRY start MOV a 1, #0 x 0 MOV a 2, #0 x 0 ADC a 3, a 1, a 2 END After the execution of the program is terminated, What are the value store in r 0, r 1, and r 2 What are the value in N, Z, C, V bit of CSPR

Updating Condition Flags in CPSR Normally, data processing instructions such as ADD, ADC, SUB,

Updating Condition Flags in CPSR Normally, data processing instructions such as ADD, ADC, SUB, etc. do not change the value of condition flags in CPSR We can add S at the end of instructions to indicate that the condition code (flag) should be updated Instruction (not update Instruction (update CC) ADDS ADCS SUBS SBCS RSBS RSCS

Example 3 AREA ex 3, CODE, READONLY ENTRY start MOV a 1, #0 x

Example 3 AREA ex 3, CODE, READONLY ENTRY start MOV a 1, #0 x 0 MOV a 2, #0 x 0 ADCS a 3, a 1, a 2 END After the execution of the program is terminated, What are the value store in r 0, r 1, and r 2 What are the value in N, Z, C, V bit of CSPR

Example 4 AREA ex 4, CODE, READONLY ENTRY start MOV a 1, #0 x.

Example 4 AREA ex 4, CODE, READONLY ENTRY start MOV a 1, #0 x. FFFFFFFB MOV a 2, #0 x 5 ADDS a 3, a 1, a 2 END After the execution of the program is terminated, What are the value store in r 0, r 1, and r 2 What are the value in N, Z, C, V bit of CSPR

Example 5 AREA ex 4, CODE, READONLY ENTRY start MOV a 1, #0 x.

Example 5 AREA ex 4, CODE, READONLY ENTRY start MOV a 1, #0 x. FFFFFFFB MOV a 2, #0 x 5 ADDS a 3, a 1, a 2 ADD a 3, a 1, a 2 END After the execution of the program is terminated, What are the value store in r 0, r 1, and r 2 What are the value in N, Z, C, V bit of CSPR

Assignment 4 1. Assume 8 bit-number, What are the values store in condition flags

Assignment 4 1. Assume 8 bit-number, What are the values store in condition flags (N, Z, C, V) of the following operations 0 x. CA + 0 x 17 0 x. F 0 + 0 x. F 8 0 x 6 F - 0 x 75 0 x. C 5 - 0 x. D 6 2. After the execution of the program is terminated in ARM processor, What are the value store in r 0, r 1, and r 2 What are the value in N, Z, C, V bit of CSPR AREA assignment 4, CODE, READONLY ENTRY start MOV a 1, #0 x. FFFFFF 00 MOV a 2, #0 x. FFFFFF 58 ADDS a 3, a 1, a 2 MOV a 2, #0 x 1 B RSCS a 3, a 2, a 3 END