1 / 18

Arithmetic and Logic Chapter 5

Arithmetic and Logic Chapter 5. Sepehr Naimi. www.NicerLand.com. 8-bit Signed numbers. 16-bit Signed numbers. 32-bit Signed numbers. Adding signed numbers. mov r1, 0xfc @ r1 = 1111 0101 mov r2, #0x07 @ r2 = 0000 0111 adds r3, r1, r2 @ r3 = r1 + r2.

tpatricia
Download Presentation

Arithmetic and Logic Chapter 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arithmetic and LogicChapter 5 Sepehr Naimi www.NicerLand.com

  2. 8-bit Signed numbers

  3. 16-bit Signed numbers

  4. 32-bit Signed numbers

  5. Adding signed numbers mov r1, 0xfc @ r1 = 1111 0101 mov r2, #0x07 @ r2 = 0000 0111 adds r3, r1, r2 @ r3 = r1 + r2 Considering 8-bit signed numbers Considering 8-bit unsigned numbers -11 + + 7 -4 1111 0101 + 0000 0111 1111 1100 245 + 7 252

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

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

  8. Overflow (V) and Negative (N) flags in ARM In a 32-bit operation, V is set to 1 in either of two cases: • There is a carry from D30 to D31 but no carry out of D31 (C = 0). • There is a carry from D31 out (C = 1) but no carry from D30 to D31. C =0 N =1 V =1

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

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

  11. ASR (Arithmetic Shift Right) Instruction ASR(S) Rd, Rm, #count @Arithmetic Shift Right MOVS 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 D6, D6 is moved to D5, D5 is moved to D4 and so on. In the next code you can see what happens to 0010 0110 after running 5 ASR instructions. MOV R2, #0xD0 @R2 = 0000 0000 0000 0000 0000 0000 1101 0000(-48) C = 0 ASRS R2, R2, #1 @R2 = 0000 0000 0000 0000 0000 0000 1110 1000(-24) C = 0 ASRS R2, R2, #1 @R2 = 0000 0000 0000 0000 0000 0000 1111 0100(-12) C = 0 ASRS R2, R2, #1 @R2 = 0000 0000 0000 0000 0000 0000 1111 1010(-6) C = 0 ASRS R2, R2, #1 @R2 = 0000 0000 0000 0000 0000 0000 1111 1101(-3) C = 0 ASRS R2, R2, #1 @R2 = 0000 0000 0000 0000 0000 0000 1111 1110(-1) C = 1

  12. Signed number comparison Op2 > Rn V = N Op2 = Rn Z = 1 Op2 < Rn N ≠ V CMP Rn, Op2

  13. Sign Extension

  14. In this program, the result is incorrect! @ storing data in program memory. .text .global _start _start: ldr r0, =AA @ point to AA ldrb r1, [r0] ldr r0, =BB @ point to BB ldrb r2, [r0] sdiv r0, r1, r2 @ r0 = r1 / r2 @ terminate the program mov r7, #1 svc 0 aa: .byte -25 bb: .byte -2 R1 = 0x000000E7 = 231 R2 = 0x000000FE = 254 R0 = R1 / R2 = 231 / 254 = 0

  15. Sign extending • Sign extending a byte ldrsb Rn,[Rs] Example 1: @ assume memory location 0x80000 has +96 = 0110 0000 and R1=0x80000 ldrsb r0, [r1] @ now r0 = 00000000000000000000000001100000 Example 2: @ assume memory location 0x80000 contains -2 = 1111 1110 and R2=0x80000 ldrsb r4, [r2] @ now r4 = 11111111111111111111111111111110

  16. Sign extending • Sign extending a half word ldrsh Rn,[Rs] Example 1: @ assume 0x80000 contains +260 = 0000 0001 0000 0100 and r1=0x80000 ldrsh r0, [r1] @ r0=0000 0000 0000 0000 0000 0001 0000 0100 Example 2: @ assume location 0x20000 has -327660=0x8002 and r2=0x20000 ldrsh r1, [r2] @ r1=FFFF8002

  17. Now, the result is correct! @ storing data in program memory. .text .global _start _start: ldr r0, =AA @ point to AA ldrsb r1, [r0] ldr r0, =BB @ point to BB ldrsb r2, [r0] sdiv r0, r1, r2 @ r0 = r1 / r2 @ terminate the program mov r7, #1 svc 0 aa: .byte -25 bb: .byte -2 R1 = 0xFFFFFFE7 = -25 R2 = 0xFFFFFFFE = -2 R0 = R1 / R2 = -25 / -2 = 12

  18. Avoiding overflow by using proper variables .data data1: .byte 96 data2: .byte 70 result: .hword 0 .text .global _start _start: ldr r1, =data1 ldr r2, =data2 ldr r3, =result ldrsb r4, [r1] @ r4 = +96 ldrsb r5, [r2] @ r5 = +70 add r4, r4, r5 @ r4 = r4 + r5 = 96 + 70 = +166 str r4, [r3] @store +166 in location result mov r7, #1 svc 0

More Related