Arithmetic and Logic Chapter 5 Sepehr Naimi www

  • Slides: 18
Download presentation
Arithmetic and Logic Chapter 5 Sepehr Naimi www. Nicer. Land. com

Arithmetic and Logic Chapter 5 Sepehr Naimi www. Nicer. Land. com

8 -bit Signed numbers Decimal -128 -127 -126. . . -2 -1 0 +1

8 -bit Signed numbers Decimal -128 -127 -126. . . -2 -1 0 +1 +2 … +127 Binary 1000 0000 1000 0001 1000 0010. . . . 1111 1110 1111 0000 0001 0000 0010. . . . 0111 1111 Hex 80 81 82. . FE FF 00 01 02. . 7 F 2

16 -bit Signed numbers Decimal -32, 768 -32, 767 -32, 766. . . -2

16 -bit Signed numbers Decimal -32, 768 -32, 767 -32, 766. . . -2 -1 0 +1 +2 … +32, 766 +32, 767 Binary 1000 0000 1000 0000 0001 1000 0000 0010. . . . 1111 1110 1111 0000 0000 0001 0000 0010. . . . 0111 1111 1110 0111 1111 Hex 8000 8001 8002. . FFFE FFFF 0000 0001 0002 … 7 FFE 7 FFF 3

32 -bit Signed numbers Decimal -2, 147, 483, 648 -2, 147, 483, 647 -2,

32 -bit Signed numbers Decimal -2, 147, 483, 648 -2, 147, 483, 647 -2, 147, 483, 646. . . -2 Binary 10000000000000000 100000000000000010. . . 11111111111111110 Hex 80000000 80000001 80000002. . . FFFFFFFE -1 1111111111111111 FFFF 0 +1 +2. . . +2, 147, 483, 646 00000000000000001 00000000000000010. . . 01111111111111110 00000001 00000002. . . 7 FFFFFFE +2, 147, 483, 647 01111111111111111 7 FFFFFFF 4

Adding signed numbers mov adds r 1, 0 xfc r 2, #0 x 07

Adding signed numbers mov adds r 1, 0 xfc r 2, #0 x 07 r 3, r 1, r 2 @ r 1 = 1111 0101 @ r 2 = 0000 0111 @ r 3 = r 1 + r 2 Considering 8 -bit signed numbers -11 + +7 -4 Considering 8 -bit unsigned numbers 1111 0101 + 0000 0111 1100 245 + 7 252 5

Overflow 0110 0000 + 0100 0110 1010 0110 +96 + +70 -90 • There

Overflow 0110 0000 + 0100 0110 1010 0110 +96 + +70 -90 • There is a carry from D 6 to D 7 but no carry out of D 7 (C = 0). • There is a carry from D 7 out (C = 1) but no carry from D 6 to D 7 6

Overflow (Cont. ) • Solving the Overflow problem: Choose proper types for variables 0000

Overflow (Cont. ) • Solving the Overflow problem: Choose proper types for variables 0000 0110 0000 +0000 0100 0110 0000 1010 0110 +96 + +70 +166 7

Overflow (V) and Negative (N) flags in ARM In a 32 -bit operation, V

Overflow (V) and Negative (N) flags in ARM In a 32 -bit operation, V is set to 1 in either of two cases: 1. There is a carry from D 30 to D 31 but no carry out of D 31 (C = 0). 2. There is a carry from D 31 out (C = 1) but no carry from D 30 to D 31. +6 E 2 F 356 F 0110 1110 0010 1111 0011 0101 0110 1111 + +13 D 49530 +0001 0011 1101 0100 1001 0101 0011 0000 +8203 CA 9 F 01000 0010 0000 0011 1100 1010 1001 1111 = – 0 x 7 DFC 3561 C =0 N =1 V =1 8

Multiplication of Signed numbers • Unsigned Multiplication 1111 1101 × 0000 0010 11111 1010

Multiplication of Signed numbers • Unsigned Multiplication 1111 1101 × 0000 0010 11111 1010 253 × 2 506 mul Rd, Rn, Op 2 umull Rd. Lo, Rd. Hi, Rn, Rm • Signed Multiplication 1111 1101 × 0000 0010 1111 1010 -3 × 2 -6 smull Rdlo, Rdhi, Rm, Rn 9

Division of Signed numbers • Unsigned Division udiv Rd, Rm, Rn Rd = Rm

Division of Signed numbers • Unsigned Division udiv Rd, Rm, Rn Rd = Rm / Rn • Signed Division sdiv Rd, Rm, Rn Rd = Rm / Rn 10

ASR (Arithmetic Shift Right) Instruction ASR(S) MOVS Rd, Rm, #count @Arithmetic Shift Right Rd,

ASR (Arithmetic Shift Right) Instruction ASR(S) MOVS Rd, Rm, #count @Arithmetic Shift Right Rd, Rm, ASR #count ASR means arithmetic shift right. ASR instruction can divide signed number by 2. In ASR, as bits are shifted from left to right, MSB is held constant and the LSB exits to the carry flag. In other words MSB is not changed but is copied to D 6, D 6 is moved to D 5, D 5 is moved to D 4 and so on. In the next code you can see what happens to 0010 0110 after running 5 ASR instructions. MOV R 2, #0 x. D 0 @R 2 = 0000 0000 1101 0000(-48) C = 0 ASRS R 2, #1 @R 2 = 0000 0000 1110 1000(-24) C = 0 ASRS R 2, #1 @R 2 = 0000 0000 1111 0100(-12) C = 0 ASRS R 2, #1 @R 2 = 0000 0000 1111 1010(-6) C = 0 ASRS R 2, #1 @R 2 = 0000 0000 1111 1101(-3) C = 0 ASRS R 2, #1 @R 2 = 0000 0000 1111 1110(-1) C = 1 11

Signed number comparison CMP Rn, Op 2 BEQ BNE BMI BPL BVS BVC BGE

Signed number comparison CMP Rn, Op 2 BEQ BNE BMI BPL BVS BVC BGE BLT BGT BLE Op 2 > Rn Op 2 = Rn Op 2 < Rn Instruction Branch equal Branch not equal Branch minus (branch negative) Branch plus (branch positive) Branch if V set (branch overflow) Branch if V clear (branch if no overflow) Branch greater than or equal Branch less than Branch greater than Branch less than or equal V=N Z=1 N≠V Action Branch if Z = 1 Branch if Z = 0 Branch if N = 1 Branch if N = 0 Branch if V = 1 Branch if V = 0 Branch if N = V Branch if N ≠ V Branch if Z = 0 and N = V Branch if Z = 1 or N ≠ V 12

Sign Extension

Sign Extension

In this program, the result is incorrect! @ storing data in program memory. .

In this program, the result is incorrect! @ storing data in program memory. . text. global _start: ldr r 0, =AA @ point to AA ldrb r 1, [r 0] ldr r 0, =BB @ point to BB ldrb r 2, [r 0] sdiv r 0, r 1, r 2 @ r 0 = r 1 / r 2 @ terminate the program mov r 7, #1 svc 0 aa: bb: . byte R 1 = 0 x 000000 E 7 = 231 R 2 = 0 x 000000 FE = 254 R 0 = R 1 / R 2 = 231 / 254 = 0 -25 -2 14

Sign extending • Sign extending a byte ldrsb Rn, [Rs] Example 1: @ assume

Sign extending • Sign extending a byte ldrsb Rn, [Rs] Example 1: @ assume memory location 0 x 80000 has +96 = 0110 0000 and R 1=0 x 80000 ldrsb r 0, [r 1] @ now r 0 = 00000000000001100000 Example 2: @ assume memory location 0 x 80000 contains -2 = 1111 1110 and R 2=0 x 80000 ldrsb r 4, [r 2] @ now r 4 = 11111111111111110 15

Sign extending • Sign extending a half word ldrsh Rn, [Rs] Example 1: @

Sign extending • Sign extending a half word ldrsh Rn, [Rs] Example 1: @ assume 0 x 80000 contains +260 = 0000 0001 0000 0100 and r 1=0 x 80000 ldrsh r 0, [r 1] @ r 0=0000 0000 0001 0000 0100 Example 2: @ assume location 0 x 20000 has -327660=0 x 8002 and r 2=0 x 20000 ldrsh r 1, [r 2] @ r 1=FFFF 8002 16

Now, the result is correct! @ storing data in program memory. . text. global

Now, the result is correct! @ storing data in program memory. . text. global _start: ldr r 0, =AA @ point to AA ldrsb r 1, [r 0] ldr r 0, =BB @ point to BB ldrsb r 2, [r 0] sdiv r 0, r 1, r 2 @ r 0 = r 1 / r 2 @ terminate the program mov r 7, #1 svc 0 aa: bb: . byte R 1 = 0 x. FFFFFFE 7 = -25 R 2 = 0 x. FFFFFFFE = -2 R 0 = R 1 / R 2 = -25 / -2 = 12 -25 -2 17

Avoiding overflow by using proper variables data 1: data 2: result: . data. byte.

Avoiding overflow by using proper variables data 1: data 2: result: . data. byte. hword 96 70 0 . text. global _start: ldr ldr r 1, =data 1 r 2, =data 2 r 3, =result ldrsb add str mov svc 0 r 4, [r 1] r 5, [r 2] r 4, r 5 r 4, [r 3] r 7, #1 @ r 4 = +96 @ r 5 = +70 @ r 4 = r 4 + r 5 = 96 + 70 = +166 @store +166 in location result 18