Computer Engineering

profileAlkhawaa
lctr18.pdf

Class notes for lctr 18 of CEC 3203/6/2019 p. 1

1. Shifting and rotation instructions

1.1 Barrel shifter

ADD r2, r1, r0, LSL #1

LDR r2, [r1, r0, LSL #2]

ADD r2, r0, r0, LSR #1 ; r2 = 3*r0/2 (unsigned)

SUB r2, r0, r0, ASR #2 ; r2 = 3*r0/4 (signed)

1.2 Shifting instructions (operations) that have C implementations

For signed only

For both signed and unsigned

For unsigned only

Class notes for lctr 18 of CEC 3203/6/2019 p. 2

Note:

• The ASR, SLS, LSR, and ROR instructions can shift (rotate) the value of a register by a

certain number.

• The RRX instruction rotates the content by a single bit.

1.3 Shifting instructions (operations) that have no C implementations

Class notes for lctr 18 of CEC 3203/6/2019 p. 3

1.4 Simplified arithmetic operations with shifting

Expl 18-02. Examples using SUB and RSB instructions:

Expl 18-01.

Class notes for lctr 18 of CEC 3203/6/2019 p. 4

2. Addition and subtraction instructions

Expl 18-04. Using the C flag in 64-bit addition:

• Note that we use the numbers directly in the example below. In a general case,

we can use union to define the lower and higher words in the long integer.

Expl 18-03. Recall the assembly function to calculate the absolute value of a signed integer:

; int32_t my_abs(int32_t a)

Class notes for lctr 18 of CEC 3203/6/2019 p. 5

Expl 18-05. Change the following code to perform the subtraction of

0x0000 0007 0000 0000 - 0x0000 0004 0000 0001.

Class notes for lctr 18 of CEC 3203/6/2019 p. 6

3. Multiplication and accumulation instructions

3.1 Short multiplication and accumulation instructions to master

r6 = r0 – LSB32(r4 x r1)

3.2 Long multiplication and accumulation instructions to know

Class notes for lctr 18 of CEC 3203/6/2019 p. 7

4. Division instructions

4.1 Short division instructions to master

Expl 18-06. Find the modulo of A and B using C and assembly: M = A mod B. (In computing,

the modulo operation finds the remainder after division of one number by another

(sometimes called modulus).)