ARM Assembler Structure / Loops Structure / Loops p. 1/12 Loops - - PowerPoint PPT Presentation

arm assembler
SMART_READER_LITE
LIVE PREVIEW

ARM Assembler Structure / Loops Structure / Loops p. 1/12 Loops - - PowerPoint PPT Presentation

Systems Architecture ARM Assembler Structure / Loops Structure / Loops p. 1/12 Loops Four parts to any loop initialisation init i) body body ii) increment or inc iii) decrement dec exit condition


slide-1
SLIDE 1

Systems Architecture

ARM Assembler

Structure / Loops

Structure / Loops – p. 1/12

slide-2
SLIDE 2

Loops

  • Four parts to any loop

i) initialisation

init

ii) body

body

iii) increment or

inc

decrement

dec

iv) exit condition

cond

  • Three basic types of loop

i) Repeat . . . Until ii) While iii) Counted

Structure / Loops – p. 2/12

slide-3
SLIDE 3

The Repeat . . . Until Loop

  • Exit condition tested at end of loop
  • Loop is executed at least once

Pseudocode:

init

Repeat

body inc/dec

Until cond

Structure / Loops – p. 3/12

slide-4
SLIDE 4

The Repeat . . . Until Loop

  • Exit condition tested at end of loop
  • Loop is executed at least once

Pseudocode: C:

init init;

Repeat do {

body body; inc/dec inc/dec;

Until cond } while (cond);

Structure / Loops – p. 3/12

slide-5
SLIDE 5

The Repeat . . . Until Loop

  • Exit condition tested at end of loop
  • Loop is executed at least once

Pseudocode: C: Assembler:

init init; init

Repeat do { label

body body body; inc/dec inc/dec inc/dec; cond

Until cond } while (cond); Bcc label

Structure / Loops – p. 3/12

slide-6
SLIDE 6

The While Loop

  • Exit condition tested at start of loop
  • Loop may never be executed (zero or more times)

Pseudocode:

init

While cond

body inc/dec

End While

Structure / Loops – p. 4/12

slide-7
SLIDE 7

The While Loop

  • Exit condition tested at start of loop
  • Loop may never be executed (zero or more times)

Pseudocode: C:

init init;

While cond while (cond) {

body body; inc/dec inc/dec;

End While }

Structure / Loops – p. 4/12

slide-8
SLIDE 8

The While Loop

  • Exit condition tested at start of loop
  • Loop may never be executed (zero or more times)

Pseudocode: C: Assembler:

init init; init

While cond while (cond) { label1

cond body body;

Bcc label2

inc/dec inc/dec; body

End While }

inc/dec

BAL label1 label2 . . .

Structure / Loops – p. 4/12

slide-9
SLIDE 9

The Counted Loop

Up Counting Down Counting

  • Pseudocode:

For init up to cond For init down to cond body body Next Next

Structure / Loops – p. 5/12

slide-10
SLIDE 10

The Counted Loop

Up Counting Down Counting

  • Pseudocode:

For init up to cond For init down to cond body body Next Next

  • C:

for (init; cond; inc) { for (init; cond; dec) { body; body; } }

Structure / Loops – p. 5/12

slide-11
SLIDE 11

The Counted Loop

Up Counting Down Counting

  • Pseudocode:

For init up to cond For init down to cond body body Next Next

  • C:

for (init; cond; inc) { for (init; cond; dec) { body; body; } }

  • Assembler:

init label body inc cond Bcc label init label body dec cond Bcc label

Structure / Loops – p. 5/12

slide-12
SLIDE 12

Up or Down Counter

  • Zero is easy to detect
  • When counting down we can merge the decrement

and cond code into a single subs instruction.

Up Counting Down Counting

LDR R0, =Table LDR R0, =Table MOV R1, #0 MOV R1, #0 MOV R2, #0 MOV R2, #10 Loop LDRB R3, [R0] Loop LDRB R3, [R0] ADD R1, R1, R3 ADD R1, R1, R3 ADD R0, R0, #1 ADD R0, R0, #1 ADD R2, R2, #1 SUBS R2, R2, #1 CMP R2, #10 BNE Loop BLT Loop

Structure / Loops – p. 6/12

slide-13
SLIDE 13

Up or Down Counter

  • Zero is easy to detect
  • When counting down we can merge the decrement

and cond code into a single subs instruction.

Up Counting Down Counting

LDR R0, =Table LDR R0, =Table MOV R1, #0 MOV R1, #0 MOV R2, #0 MOV R2, #10 Loop LDRB R3, [R0] Loop LDRB R3, [R0] ADD R1, R1, R3 ADD R1, R1, R3 ADD R0, R0, #1 ADD R0, R0, #1 ADD R2, R2, #1 SUBS R2, R2, #1 CMP R2, #10 BNE Loop BLT Loop

Structure / Loops – p. 6/12

slide-14
SLIDE 14

Up or Down Counter

  • Zero is easy to detect
  • When counting down we can merge the decrement

and cond code into a single subs instruction.

Up Counting Down Counting

LDR R0, =Table LDR R0, =Table MOV R1, #0 MOV R1, #0 MOV R2, #0 MOV R2, #10 Loop LDRB R3, [R0] Loop LDRB R3, [R0] ADD R1, R1, R3 ADD R1, R1, R3 ADD R0, R0, #1 ADD R0, R0, #1 ADD R2, R2, #1 SUBS R2, R2, #1 CMP R2, #10 BNE Loop BLT Loop

Structure / Loops – p. 6/12

slide-15
SLIDE 15

Program: sum16.s

7 Main 8 LDR R0, =Data1 ;load the address of the lookup table 9 EOR R1, R1, R1 ;clear R1 to store sum 10 LDR R2, Length ;init element count 11 Loop 12 LDR R3, [R0] ;get the data 13 ADD R1, R1, R3 ;add it to r1 14 ADD R0, R0, #+4 ;increment pointer 15 SUBS R2, R2, #1 ;decrement count with zero set 16 BNE Loop ;if zero flag is not set, loop 19 22 Table DCW &2040 ;table of values to be added 24 DCW &1C22 28 TablEnd DCD 29 31 Length DCW (TablEnd - Table) / 4 ;because we’re having to align

Structure / Loops – p. 7/12

slide-16
SLIDE 16

Program: sum16.s

7 Main 8 LDR R0, =Data1 ;load the address of the lookup table 9 EOR R1, R1, R1 ;clear R1 to store sum 10 LDR R2, Length ;init element count 11 Loop 12 LDR R3, [R0] ;get the data 13 ADD R1, R1, R3 ;add it to r1 14 ADD R0, R0, #+4 ;increment pointer 15 SUBS R2, R2, #1 ;decrement count with zero set 16 BNE Loop ;if zero flag is not set, loop 19 22 Table DCW &2040 ;table of values to be added 24 DCW &1C22 28 TablEnd DCD 29 31 Length DCW (TablEnd - Table) / 4 ;because we’re having to align EOR Quick way of setting R1 to zero

Structure / Loops – p. 7/12

slide-17
SLIDE 17

Program: sum16.s

7 Main 8 LDR R0, =Data1 ;load the address of the lookup table 9 EOR R1, R1, R1 ;clear R1 to store sum 10 LDR R2, Length ;init element count 11 Loop 12 LDR R3, [R0] ;get the data 13 ADD R1, R1, R3 ;add it to r1 14 ADD R0, R0, #+4 ;increment pointer 15 SUBS R2, R2, #1 ;decrement count with zero set 16 BNE Loop ;if zero flag is not set, loop 19 22 Table DCW &2040 ;table of values to be added 24 DCW &1C22 28 TablEnd DCD 29 31 Length DCW (TablEnd - Table) / 4 ;because we’re having to align Loop Label the next instruction

Structure / Loops – p. 7/12

slide-18
SLIDE 18

Program: sum16.s

7 Main 8 LDR R0, =Data1 ;load the address of the lookup table 9 EOR R1, R1, R1 ;clear R1 to store sum 10 LDR R2, Length ;init element count 11 Loop 12 LDR R3, [R0] ;get the data 13 ADD R1, R1, R3 ;add it to r1 14 ADD R0, R0, #+4 ;increment pointer 15 SUBS R2, R2, #1 ;decrement count with zero set 16 BNE Loop ;if zero flag is not set, loop 19 22 Table DCW &2040 ;table of values to be added 24 DCW &1C22 28 TablEnd DCD 29 31 Length DCW (TablEnd - Table) / 4 ;because we’re having to align ADD Move pointer (R0) to next word

Structure / Loops – p. 7/12

slide-19
SLIDE 19

Program: sum16.s

7 Main 8 LDR R0, =Data1 ;load the address of the lookup table 9 EOR R1, R1, R1 ;clear R1 to store sum 10 LDR R2, Length ;init element count 11 Loop 12 LDR R3, [R0] ;get the data 13 ADD R1, R1, R3 ;add it to r1 14 ADD R0, R0, #+4 ;increment pointer 15 SUBS R2, R2, #1 ;decrement count with zero set 16 BNE Loop ;if zero flag is not set, loop 19 22 Table DCW &2040 ;table of values to be added 24 DCW &1C22 28 TablEnd DCD 29 31 Length DCW (TablEnd - Table) / 4 ;because we’re having to align LDR/ADD Using Post-index addressing we can remove the ADD: LDR R3, [R0], #4

Structure / Loops – p. 7/12

slide-20
SLIDE 20

Program: sum16.s

7 Main 8 LDR R0, =Data1 ;load the address of the lookup table 9 EOR R1, R1, R1 ;clear R1 to store sum 10 LDR R2, Length ;init element count 11 Loop 12 LDR R3, [R0] ;get the data 13 ADD R1, R1, R3 ;add it to r1 14 ADD R0, R0, #+4 ;increment pointer 15 SUBS R2, R2, #1 ;decrement count with zero set 16 BNE Loop ;if zero flag is not set, loop 19 22 Table DCW &2040 ;table of values to be added 24 DCW &1C22 28 TablEnd DCD 29 31 Length DCW (TablEnd - Table) / 4 ;because we’re having to align SUBS Subtract and set flags Decrement loop counter, R2

Structure / Loops – p. 7/12

slide-21
SLIDE 21

Program: sum16.s

7 Main 8 LDR R0, =Data1 ;load the address of the lookup table 9 EOR R1, R1, R1 ;clear R1 to store sum 10 LDR R2, Length ;init element count 11 Loop 12 LDR R3, [R0] ;get the data 13 ADD R1, R1, R3 ;add it to r1 14 ADD R0, R0, #+4 ;increment pointer 15 SUBS R2, R2, #1 ;decrement count with zero set 16 BNE Loop ;if zero flag is not set, loop 19 22 Table DCW &2040 ;table of values to be added 24 DCW &1C22 28 TablEnd DCD 29 31 Length DCW (TablEnd - Table) / 4 ;because we’re having to align BNE Branch to Loop if counter is not equal to zero

Structure / Loops – p. 7/12

slide-22
SLIDE 22

Program: sum16.s

7 Main 8 LDR R0, =Data1 ;load the address of the lookup table 9 EOR R1, R1, R1 ;clear R1 to store sum 10 LDR R2, Length ;init element count 11 Loop 12 LDR R3, [R0] ;get the data 13 ADD R1, R1, R3 ;add it to r1 14 ADD R0, R0, #+4 ;increment pointer 15 SUBS R2, R2, #1 ;decrement count with zero set 16 BNE Loop ;if zero flag is not set, loop 19 22 Table DCW &2040 ;table of values to be added 24 DCW &1C22 28 TablEnd DCD 29 31 Length DCW (TablEnd - Table) / 4 ;because we’re having to align DCW Assembler will calculate the length of data table for me

Structure / Loops – p. 7/12

slide-23
SLIDE 23

Program: sum16b.s

8 Main 9 LDR R0, =Data1 ;load the address of the lookup table 10 EOR R1, R1, R1 ;clear R1 to store sum 11 LDR R2, Length ;init element count 12 CMP R2, #0 ;zero length table ? 13 BEQ Done ;yes => skip over sum loop 14 Loop 15 LDR R3, [R0] ;get the data that R0 points to 16 ADD R1, R1, R3 ;add it to R1 17 ADD R0, R0, #+4 ;increment pointer 18 SUBS R2, R2, #0x1 ;decrement count with zero set 19 BNE Loop ;if zero flag is not set, loop 20 Done 21 STR R1, Result ;otherwise done - store result 22 SWI &11

Structure / Loops – p. 8/12

slide-24
SLIDE 24

Program: sum16b.s

8 Main 9 LDR R0, =Data1 ;load the address of the lookup table 10 EOR R1, R1, R1 ;clear R1 to store sum 11 LDR R2, Length ;init element count 12 CMP R2, #0 ;zero length table ? 13 BEQ Done ;yes => skip over sum loop 14 Loop 15 LDR R3, [R0] ;get the data that R0 points to 16 ADD R1, R1, R3 ;add it to R1 17 ADD R0, R0, #+4 ;increment pointer 18 SUBS R2, R2, #0x1 ;decrement count with zero set 19 BNE Loop ;if zero flag is not set, loop 20 Done 21 STR R1, Result ;otherwise done - store result 22 SWI &11 EOR Quick way of setting R1 to zero

Structure / Loops – p. 8/12

slide-25
SLIDE 25

Program: sum16b.s

8 Main 9 LDR R0, =Data1 ;load the address of the lookup table 10 EOR R1, R1, R1 ;clear R1 to store sum 11 LDR R2, Length ;init element count 12 CMP R2, #0 ;zero length table ? 13 BEQ Done ;yes => skip over sum loop 14 Loop 15 LDR R3, [R0] ;get the data that R0 points to 16 ADD R1, R1, R3 ;add it to R1 17 ADD R0, R0, #+4 ;increment pointer 18 SUBS R2, R2, #0x1 ;decrement count with zero set 19 BNE Loop ;if zero flag is not set, loop 20 Done 21 STR R1, Result ;otherwise done - store result 22 SWI &11 CMP Is table length zero?

Structure / Loops – p. 8/12

slide-26
SLIDE 26

Program: sum16b.s

8 Main 9 LDR R0, =Data1 ;load the address of the lookup table 10 EOR R1, R1, R1 ;clear R1 to store sum 11 LDR R2, Length ;init element count 12 CMP R2, #0 ;zero length table ? 13 BEQ Done ;yes => skip over sum loop 14 Loop 15 LDR R3, [R0] ;get the data that R0 points to 16 ADD R1, R1, R3 ;add it to R1 17 ADD R0, R0, #+4 ;increment pointer 18 SUBS R2, R2, #0x1 ;decrement count with zero set 19 BNE Loop ;if zero flag is not set, loop 20 Done 21 STR R1, Result ;otherwise done - store result 22 SWI &11 BEQ Skip zero length tables Protects from processing an empty list

Structure / Loops – p. 8/12

slide-27
SLIDE 27

Program: sum16b.s

8 Main 9 LDR R0, =Data1 ;load the address of the lookup table 10 EOR R1, R1, R1 ;clear R1 to store sum 11 LDR R2, Length ;init element count 12 CMP R2, #0 ;zero length table ? 13 BEQ Done ;yes => skip over sum loop 14 Loop 15 LDR R3, [R0] ;get the data that R0 points to 16 ADD R1, R1, R3 ;add it to R1 17 ADD R0, R0, #+4 ;increment pointer 18 SUBS R2, R2, #0x1 ;decrement count with zero set 19 BNE Loop ;if zero flag is not set, loop 20 Done 21 STR R1, Result ;otherwise done - store result 22 SWI &11 LDR/ADD Using Post-index addressing we can remove the ADD: LDR R3, [R0], #4

Structure / Loops – p. 8/12

slide-28
SLIDE 28

Program: sum16b.s

8 Main 9 LDR R0, =Data1 ;load the address of the lookup table 10 EOR R1, R1, R1 ;clear R1 to store sum 11 LDR R2, Length ;init element count 12 CMP R2, #0 ;zero length table ? 13 BEQ Done ;yes => skip over sum loop 14 Loop 15 LDR R3, [R0] ;get the data that R0 points to 16 ADD R1, R1, R3 ;add it to R1 17 ADD R0, R0, #+4 ;increment pointer 18 SUBS R2, R2, #0x1 ;decrement count with zero set 19 BNE Loop ;if zero flag is not set, loop 20 Done 21 STR R1, Result ;otherwise done - store result 22 SWI &11 SUBS/BNE Decrement counter and branch to Loop if not zero

Structure / Loops – p. 8/12

slide-29
SLIDE 29

Program: countneg.s

10 LDR R2, Length ;init element count 11 CMP R2, #0 ;is table empty 12 BEQ Done ;yes => skip loop 13 Loop 14 LDR R3, [R0] ;get the data 15 CMP R3, #0 ;is it positive? 16 BPL Looptest ;yes => skip next line 17 ADD R1, R1, #1 ;increment -ve number count 18 Looptest 19 ADD R0, R0, #+4 ;increment pointer 20 SUBS R2, R2, #0x1 ;decrement count with zero set 21 BNE Loop ;until count is zero 27 28 Table DCD &F1522040 ;table of values to be added 31 TablEnd DCD 34 Length DCW (TablEnd - Table) / 4 ;because we’re having to align

Structure / Loops – p. 9/12

slide-30
SLIDE 30

Program: countneg.s

10 LDR R2, Length ;init element count 11 CMP R2, #0 ;is table empty 12 BEQ Done ;yes => skip loop 13 Loop 14 LDR R3, [R0] ;get the data 15 CMP R3, #0 ;is it positive? 16 BPL Looptest ;yes => skip next line 17 ADD R1, R1, #1 ;increment -ve number count 18 Looptest 19 ADD R0, R0, #+4 ;increment pointer 20 SUBS R2, R2, #0x1 ;decrement count with zero set 21 BNE Loop ;until count is zero 27 28 Table DCD &F1522040 ;table of values to be added 31 TablEnd DCD 34 Length DCW (TablEnd - Table) / 4 ;because we’re having to align CMP/BEQ Skip zero length tables

Structure / Loops – p. 9/12

slide-31
SLIDE 31

Program: countneg.s

10 LDR R2, Length ;init element count 11 CMP R2, #0 ;is table empty 12 BEQ Done ;yes => skip loop 13 Loop 14 LDR R3, [R0] ;get the data 15 CMP R3, #0 ;is it positive? 16 BPL Looptest ;yes => skip next line 17 ADD R1, R1, #1 ;increment -ve number count 18 Looptest 19 ADD R0, R0, #+4 ;increment pointer 20 SUBS R2, R2, #0x1 ;decrement count with zero set 21 BNE Loop ;until count is zero 27 28 Table DCD &F1522040 ;table of values to be added 31 TablEnd DCD 34 Length DCW (TablEnd - Table) / 4 ;because we’re having to align BPL Brach if positive (plus)

Structure / Loops – p. 9/12

slide-32
SLIDE 32

Program: countneg.s

10 LDR R2, Length ;init element count 11 CMP R2, #0 ;is table empty 12 BEQ Done ;yes => skip loop 13 Loop 14 LDR R3, [R0] ;get the data 15 CMP R3, #0 ;is it positive? 16 BPL Looptest ;yes => skip next line 17 ADD R1, R1, #1 ;increment -ve number count 18 Looptest 19 ADD R0, R0, #+4 ;increment pointer 20 SUBS R2, R2, #0x1 ;decrement count with zero set 21 BNE Loop ;until count is zero 27 28 Table DCD &F1522040 ;table of values to be added 31 TablEnd DCD 34 Length DCW (TablEnd - Table) / 4 ;because we’re having to align BPL/ADD Using Conditional Execution we can write: ADDMI R1, R1, #1 ;inc -ve count if -ve This would be faster, as it does not flush the pipeline

Structure / Loops – p. 9/12

slide-33
SLIDE 33

Program: countneg.s

10 LDR R2, Length ;init element count 11 CMP R2, #0 ;is table empty 12 BEQ Done ;yes => skip loop 13 Loop 14 LDR R3, [R0] ;get the data 15 CMP R3, #0 ;is it positive? 16 BPL Looptest ;yes => skip next line 17 ADD R1, R1, #1 ;increment -ve number count 18 Looptest 19 ADD R0, R0, #+4 ;increment pointer 20 SUBS R2, R2, #0x1 ;decrement count with zero set 21 BNE Loop ;until count is zero 27 28 Table DCD &F1522040 ;table of values to be added 31 TablEnd DCD 34 Length DCW (TablEnd - Table) / 4 ;because we’re having to align LDR/ADD Move to next word, can be merged into one: LDR R3, [R0], #4 ; get next value

Structure / Loops – p. 9/12

slide-34
SLIDE 34

Program: countneg.s

10 LDR R2, Length ;init element count 11 CMP R2, #0 ;is table empty 12 BEQ Done ;yes => skip loop 13 Loop 14 LDR R3, [R0] ;get the data 15 CMP R3, #0 ;is it positive? 16 BPL Looptest ;yes => skip next line 17 ADD R1, R1, #1 ;increment -ve number count 18 Looptest 19 ADD R0, R0, #+4 ;increment pointer 20 SUBS R2, R2, #0x1 ;decrement count with zero set 21 BNE Loop ;until count is zero 27 28 Table DCD &F1522040 ;table of values to be added 31 TablEnd DCD 34 Length DCW (TablEnd - Table) / 4 ;because we’re having to align SUBS/BNE Decrement counter and branch to Loop if not zero

Structure / Loops – p. 9/12

slide-35
SLIDE 35

Program: countneg.s

10 LDR R2, Length ;init element count 11 CMP R2, #0 ;is table empty 12 BEQ Done ;yes => skip loop 13 Loop 14 LDR R3, [R0] ;get the data 15 CMP R3, #0 ;is it positive? 16 BPL Looptest ;yes => skip next line 17 ADD R1, R1, #1 ;increment -ve number count 18 Looptest 19 ADD R0, R0, #+4 ;increment pointer 20 SUBS R2, R2, #0x1 ;decrement count with zero set 21 BNE Loop ;until count is zero 27 28 Table DCD &F1522040 ;table of values to be added 31 TablEnd DCD 34 Length DCW (TablEnd - Table) / 4 ;because we’re having to align DCW Assembler will calculate the length of data table

Structure / Loops – p. 9/12

slide-36
SLIDE 36

Program: countneg16.s

8 Main 9 LDR R0, =Data1 ;load the address of the lookup table 10 EOR R1, R1, R1 ;clear R1 to store count 11 LDR R2, Length ;init element count 12 TEQ R2, #0 ;is count zero? 13 BEQ Done ;yes => skip loop 14 Loop 15 LDR R3, [R0] ;get the data 16 AND R3, R3, #0x8000 ;bit wise AND to see if the 16th 17 CMP R3, #0x8000 ;bit is 1 18 BNE Looptest ;skip next line if zero 19 ADD R1, R1, #1 ;increment -ve number count 20 Looptest 21 ADD R0, R0, #+4 ;increment pointer 22 SUBS R2, R2, #0x1 ;decrement count with zero set 23 BNE Loop ;if zero flag is not set, loop

Structure / Loops – p. 10/12

slide-37
SLIDE 37

Program: countneg16.s

8 Main 9 LDR R0, =Data1 ;load the address of the lookup table 10 EOR R1, R1, R1 ;clear R1 to store count 11 LDR R2, Length ;init element count 12 TEQ R2, #0 ;is count zero? 13 BEQ Done ;yes => skip loop 14 Loop 15 LDR R3, [R0] ;get the data 16 AND R3, R3, #0x8000 ;bit wise AND to see if the 16th 17 CMP R3, #0x8000 ;bit is 1 18 BNE Looptest ;skip next line if zero 19 ADD R1, R1, #1 ;increment -ve number count 20 Looptest 21 ADD R0, R0, #+4 ;increment pointer 22 SUBS R2, R2, #0x1 ;decrement count with zero set 23 BNE Loop ;if zero flag is not set, loop TEQ Test R2 for 0

Structure / Loops – p. 10/12

slide-38
SLIDE 38

Program: countneg16.s

8 Main 9 LDR R0, =Data1 ;load the address of the lookup table 10 EOR R1, R1, R1 ;clear R1 to store count 11 LDR R2, Length ;init element count 12 TEQ R2, #0 ;is count zero? 13 BEQ Done ;yes => skip loop 14 Loop 15 LDR R3, [R0] ;get the data 16 AND R3, R3, #0x8000 ;bit wise AND to see if the 16th 17 CMP R3, #0x8000 ;bit is 1 18 BNE Looptest ;skip next line if zero 19 ADD R1, R1, #1 ;increment -ve number count 20 Looptest 21 ADD R0, R0, #+4 ;increment pointer 22 SUBS R2, R2, #0x1 ;decrement count with zero set 23 BNE Loop ;if zero flag is not set, loop TEQ/BEQ Protect loop from zero length tables

Structure / Loops – p. 10/12

slide-39
SLIDE 39

Program: countneg16.s

8 Main 9 LDR R0, =Data1 ;load the address of the lookup table 10 EOR R1, R1, R1 ;clear R1 to store count 11 LDR R2, Length ;init element count 12 TEQ R2, #0 ;is count zero? 13 BEQ Done ;yes => skip loop 14 Loop 15 LDR R3, [R0] ;get the data 16 AND R3, R3, #0x8000 ;bit wise AND to see if the 16th 17 CMP R3, #0x8000 ;bit is 1 18 BNE Looptest ;skip next line if zero 19 ADD R1, R1, #1 ;increment -ve number count 20 Looptest 21 ADD R0, R0, #+4 ;increment pointer 22 SUBS R2, R2, #0x1 ;decrement count with zero set 23 BNE Loop ;if zero flag is not set, loop AND Clear all but halfword sign Reset lower 15 bits

Structure / Loops – p. 10/12

slide-40
SLIDE 40

Program: countneg16.s

8 Main 9 LDR R0, =Data1 ;load the address of the lookup table 10 EOR R1, R1, R1 ;clear R1 to store count 11 LDR R2, Length ;init element count 12 TEQ R2, #0 ;is count zero? 13 BEQ Done ;yes => skip loop 14 Loop 15 LDR R3, [R0] ;get the data 16 AND R3, R3, #0x8000 ;bit wise AND to see if the 16th 17 CMP R3, #0x8000 ;bit is 1 18 BNE Looptest ;skip next line if zero 19 ADD R1, R1, #1 ;increment -ve number count 20 Looptest 21 ADD R0, R0, #+4 ;increment pointer 22 SUBS R2, R2, #0x1 ;decrement count with zero set 23 BNE Loop ;if zero flag is not set, loop CMP Is halfword sign bit (bit 15) set (negative)

Structure / Loops – p. 10/12

slide-41
SLIDE 41

Program: countneg16.s

8 Main 9 LDR R0, =Data1 ;load the address of the lookup table 10 EOR R1, R1, R1 ;clear R1 to store count 11 LDR R2, Length ;init element count 12 TEQ R2, #0 ;is count zero? 13 BEQ Done ;yes => skip loop 14 Loop 15 LDR R3, [R0] ;get the data 16 AND R3, R3, #0x8000 ;bit wise AND to see if the 16th 17 CMP R3, #0x8000 ;bit is 1 18 BNE Looptest ;skip next line if zero 19 ADD R1, R1, #1 ;increment -ve number count 20 Looptest 21 ADD R0, R0, #+4 ;increment pointer 22 SUBS R2, R2, #0x1 ;decrement count with zero set 23 BNE Loop ;if zero flag is not set, loop BNE Skip to Looptest if value is positive

Structure / Loops – p. 10/12

slide-42
SLIDE 42

Program: countneg16.s

8 Main 9 LDR R0, =Data1 ;load the address of the lookup table 10 EOR R1, R1, R1 ;clear R1 to store count 11 LDR R2, Length ;init element count 12 TEQ R2, #0 ;is count zero? 13 BEQ Done ;yes => skip loop 14 Loop 15 LDR R3, [R0] ;get the data 16 AND R3, R3, #0x8000 ;bit wise AND to see if the 16th 17 CMP R3, #0x8000 ;bit is 1 18 BNE Looptest ;skip next line if zero 19 ADD R1, R1, #1 ;increment -ve number count 20 Looptest 21 ADD R0, R0, #+4 ;increment pointer 22 SUBS R2, R2, #0x1 ;decrement count with zero set 23 BNE Loop ;if zero flag is not set, loop SUBS/BNE Using subtract and set to automatically detect zero Branch to Loop if counter is not zero

Structure / Loops – p. 10/12

slide-43
SLIDE 43

Program: countneg16.s (Revised)

8 Main 9 LDR R0, =Data1 ;load the address of the lookup table 10 EOR R1, R1, R1 ;clear R1 to store count 11 LDR R2, Length ;init element count 12 TEQ R2, #0 ;is count zero? 13 BEQ Done ;yes => skip loop 14 Loop 15 LDR R3, [R0] ;get the data 16 AND R3, R3, #0x8000 ;bit wise AND to see if the 16th 17 CMP R3, #0x8000 ;bit is 1 18 BNE Looptest ;skip next line if zero 19 ADD R1, R1, #1 ;increment -ve number count 20 Looptest 21 ADD R0, R0, #+4 ;increment pointer 22 SUBS R2, R2, #0x1 ;decrement count with zero set 23 BNE Loop ;if zero flag is not set, loop LDR/ADD By using Post-Index addressing we can write: LDR R3, [R0], #4 ; read data and move on

Structure / Loops – p. 10/12

slide-44
SLIDE 44

Program: countneg16.s (Revised)

8 Main 9 LDR R0, =Data1 ;load the address of the lookup table 10 EOR R1, R1, R1 ;clear R1 to store count 11 LDR R2, Length ;init element count 12 TEQ R2, #0 ;is count zero? 13 BEQ Done ;yes => skip loop 14 Loop 15 LDR R3, [R0] ;get the data 16 AND R3, R3, #0x8000 ;bit wise AND to see if the 16th 17 CMP R3, #0x8000 ;bit is 1 18 BNE Looptest ;skip next line if zero 19 ADD R1, R1, #1 ;increment -ve number count 20 Looptest 21 ADD R0, R0, #+4 ;increment pointer 22 SUBS R2, R2, #0x1 ;decrement count with zero set 23 BNE Loop ;if zero flag is not set, loop AND/CMP Using the “Set” option we can combine these into: ANDS R3, R3, #0x8000 Zero flag is set if R3 is positive

Structure / Loops – p. 10/12

slide-45
SLIDE 45

Program: countneg16.s (Revised)

8 Main 9 LDR R0, =Data1 ;load the address of the lookup table 10 EOR R1, R1, R1 ;clear R1 to store count 11 LDR R2, Length ;init element count 12 TEQ R2, #0 ;is count zero? 13 BEQ Done ;yes => skip loop 14 Loop 15 LDR R3, [R0] ;get the data 16 AND R3, R3, #0x8000 ;bit wise AND to see if the 16th 17 CMP R3, #0x8000 ;bit is 1 18 BNE Looptest ;skip next line if zero 19 ADD R1, R1, #1 ;increment -ve number count 20 Looptest 21 ADD R0, R0, #+4 ;increment pointer 22 SUBS R2, R2, #0x1 ;decrement count with zero set 23 BNE Loop ;if zero flag is not set, loop BEQ/ADD Using conditional execution we can avoid the branch: ADDEQ R1, R1, #1 ; Inc counter if -ve

Structure / Loops – p. 10/12

slide-46
SLIDE 46

Program: countneg16.s (Revised)

8 Main 9 LDR R0, =Data1 ;load the address of the lookup table 10 EOR R1, R1, R1 ;clear R1 to store count 11 LDR R2, Length ;init element count 12 TEQ R2, #0 ;is count zero? 13 BEQ Done ;yes => skip loop 14 Loop 15 LDR R3, [R0] ;get the data 16 AND R3, R3, #0x8000 ;bit wise AND to see if the 16th 17 CMP R3, #0x8000 ;bit is 1 18 BNE Looptest ;skip next line if zero 19 ADD R1, R1, #1 ;increment -ve number count 20 Looptest 21 ADD R0, R0, #+4 ;increment pointer 22 SUBS R2, R2, #0x1 ;decrement count with zero set 23 BNE Loop ;if zero flag is not set, loop LDRSH If we could use LDRSH this would be as simple as countnet.s – but we can’t

Structure / Loops – p. 10/12

slide-47
SLIDE 47

Program: countneg16.s (Revised)

8 Main 9 LDR R0, =Data1 ;load the address of the lookup table 10 EOR R1, R1, R1 ;clear R1 to store count 11 LDR R2, Length ;init element count 12 TEQ R2, #0 ;is count zero? 13 BEQ Done ;yes => skip loop 14 Loop 15 LDR R3, [R0], #4 ;get the data 16 ANDS R3, R3, #0x8000 ;is halfword sign set 17 18 19 ADDNE R1, R1, #1 ;increment -ve number count 20 21 22 SUBS R2, R2, #0x1 ;decrement count with zero set 23 BNE Loop ;if zero flag is not set, loop

Structure / Loops – p. 10/12

slide-48
SLIDE 48

Program: largest16.s

7 Main 8 LDR R0, =Data1 ;load the address of the lookup table 9 EOR R1, R1, R1 ;clear R1 to store largest 10 LDR R2, Length ;init element count 11 CMP R2, #0 ;is it an empty table 12 BEQ Done ;yes => skip loop 13 Loop 14 LDR R3, [R0] ;get the data 15 CMP R3, R1 ;compare to largest 16 BLS Looptest ;skip next line if zero 17 MOV R1, R3 ;increment -ve number count 18 Looptest 19 ADD R0, R0, #+4 ;increment pointer 20 SUBS R2, R2, #0x1 ;decrement count with zero set 21 BNE Loop ;if zero flag is not set, loop 22 Done

Structure / Loops – p. 11/12

slide-49
SLIDE 49

Program: largest16.s

7 Main 8 LDR R0, =Data1 ;load the address of the lookup table 9 EOR R1, R1, R1 ;clear R1 to store largest 10 LDR R2, Length ;init element count 11 CMP R2, #0 ;is it an empty table 12 BEQ Done ;yes => skip loop 13 Loop 14 LDR R3, [R0] ;get the data 15 CMP R3, R1 ;compare to largest 16 BLS Looptest ;skip next line if zero 17 MOV R1, R3 ;increment -ve number count 18 Looptest 19 ADD R0, R0, #+4 ;increment pointer 20 SUBS R2, R2, #0x1 ;decrement count with zero set 21 BNE Loop ;if zero flag is not set, loop 22 Done EOR Quick way of setting R1 to zero

Structure / Loops – p. 11/12

slide-50
SLIDE 50

Program: largest16.s

7 Main 8 LDR R0, =Data1 ;load the address of the lookup table 9 EOR R1, R1, R1 ;clear R1 to store largest 10 LDR R2, Length ;init element count 11 CMP R2, #0 ;is it an empty table 12 BEQ Done ;yes => skip loop 13 Loop 14 LDR R3, [R0] ;get the data 15 CMP R3, R1 ;compare to largest 16 BLS Looptest ;skip next line if zero 17 MOV R1, R3 ;increment -ve number count 18 Looptest 19 ADD R0, R0, #+4 ;increment pointer 20 SUBS R2, R2, #0x1 ;decrement count with zero set 21 BNE Loop ;if zero flag is not set, loop 22 Done CMP/BEQ Protect loop from empty list (zero length)

Structure / Loops – p. 11/12

slide-51
SLIDE 51

Program: largest16.s

7 Main 8 LDR R0, =Data1 ;load the address of the lookup table 9 EOR R1, R1, R1 ;clear R1 to store largest 10 LDR R2, Length ;init element count 11 CMP R2, #0 ;is it an empty table 12 BEQ Done ;yes => skip loop 13 Loop 14 LDR R3, [R0] ;get the data 15 CMP R3, R1 ;compare to largest 16 BLS Looptest ;skip next line if zero 17 MOV R1, R3 ;increment -ve number count 18 Looptest 19 ADD R0, R0, #+4 ;increment pointer 20 SUBS R2, R2, #0x1 ;decrement count with zero set 21 BNE Loop ;if zero flag is not set, loop 22 Done CMP Compare new value (R3) against current largest (R1)

Structure / Loops – p. 11/12

slide-52
SLIDE 52

Program: largest16.s

7 Main 8 LDR R0, =Data1 ;load the address of the lookup table 9 EOR R1, R1, R1 ;clear R1 to store largest 10 LDR R2, Length ;init element count 11 CMP R2, #0 ;is it an empty table 12 BEQ Done ;yes => skip loop 13 Loop 14 LDR R3, [R0] ;get the data 15 CMP R3, R1 ;compare to largest 16 BLS Looptest ;skip next line if zero 17 MOV R1, R3 ;increment -ve number count 18 Looptest 19 ADD R0, R0, #+4 ;increment pointer 20 SUBS R2, R2, #0x1 ;decrement count with zero set 21 BNE Loop ;if zero flag is not set, loop 22 Done BLS Branch if new is less than or same as current

Structure / Loops – p. 11/12

slide-53
SLIDE 53

Program: largest16.s

7 Main 8 LDR R0, =Data1 ;load the address of the lookup table 9 EOR R1, R1, R1 ;clear R1 to store largest 10 LDR R2, Length ;init element count 11 CMP R2, #0 ;is it an empty table 12 BEQ Done ;yes => skip loop 13 Loop 14 LDR R3, [R0] ;get the data 15 CMP R3, R1 ;compare to largest 16 BLS Looptest ;skip next line if zero 17 MOV R1, R3 ;increment -ve number count 18 Looptest 19 ADD R0, R0, #+4 ;increment pointer 20 SUBS R2, R2, #0x1 ;decrement count with zero set 21 BNE Loop ;if zero flag is not set, loop 22 Done SUBS/BNE Using subtract to automatically detect zero Branch to Loop if counter is not zero

Structure / Loops – p. 11/12

slide-54
SLIDE 54

Program: largest16.s

7 Main 8 LDR R0, =Data1 ;load the address of the lookup table 9 EOR R1, R1, R1 ;clear R1 to store largest 10 LDR R2, Length ;init element count 11 CMP R2, #0 ;is it an empty table 12 BEQ Done ;yes => skip loop 13 Loop 14 LDR R3, [R0] ;get the data 15 CMP R3, R1 ;compare to largest 16 BLS Looptest ;skip next line if zero 17 MOV R1, R3 ;increment -ve number count 18 Looptest 19 ADD R0, R0, #+4 ;increment pointer 20 SUBS R2, R2, #0x1 ;decrement count with zero set 21 BNE Loop ;if zero flag is not set, loop 22 Done LDR/ADD With Post-Index addressing we can write: LDR R3, [R0], #4 ; read data and move on

Structure / Loops – p. 11/12

slide-55
SLIDE 55

Program: largest16.s

7 Main 8 LDR R0, =Data1 ;load the address of the lookup table 9 EOR R1, R1, R1 ;clear R1 to store largest 10 LDR R2, Length ;init element count 11 CMP R2, #0 ;is it an empty table 12 BEQ Done ;yes => skip loop 13 Loop 14 LDR R3, [R0] ;get the data 15 CMP R3, R1 ;compare to largest 16 BLS Looptest ;skip next line if zero 17 MOV R1, R3 ;increment -ve number count 18 Looptest 19 ADD R0, R0, #+4 ;increment pointer 20 SUBS R2, R2, #0x1 ;decrement count with zero set 21 BNE Loop ;if zero flag is not set, loop 22 Done BLS/ADD Using conditional execution we can avoid the branch: MOVGT R1, R3 ; Save new current largest

Structure / Loops – p. 11/12

slide-56
SLIDE 56

Program: normalize.s

1 ; normalize a binary number 2 7 Main 8 LDR R0, =Data1 ;load the address of the lookup table 9 EOR R1, R1, R1 ;clear R1 to store shift count 10 LDR R3, [R0] ;get the value to normalize 11 CMP R3, R1 ;is it a non-zero value 12 BEQ Done ;yes => already normalised 13 Loop 14 ADD R1, R1, #1 ;increment shift counter 15 MOVS R3, R3, LSL #0x1 ;shift value by one bit 16 BPL Loop ;loop until upper bit (sign bit) set 17 Done 18 STR R1, Shifted ;otherwise done - store result 19 STR R3, Normal 20 SWI &11 ;exit

Structure / Loops – p. 12/12

slide-57
SLIDE 57

Program: normalize.s

1 ; normalize a binary number 2 7 Main 8 LDR R0, =Data1 ;load the address of the lookup table 9 EOR R1, R1, R1 ;clear R1 to store shift count 10 LDR R3, [R0] ;get the value to normalize 11 CMP R3, R1 ;is it a non-zero value 12 BEQ Done ;yes => already normalised 13 Loop 14 ADD R1, R1, #1 ;increment shift counter 15 MOVS R3, R3, LSL #0x1 ;shift value by one bit 16 BPL Loop ;loop until upper bit (sign bit) set 17 Done 18 STR R1, Shifted ;otherwise done - store result 19 STR R3, Normal 20 SWI &11 ;exit EOR Quick way of setting R1 to zero

Structure / Loops – p. 12/12

slide-58
SLIDE 58

Program: normalize.s

1 ; normalize a binary number 2 7 Main 8 LDR R0, =Data1 ;load the address of the lookup table 9 EOR R1, R1, R1 ;clear R1 to store shift count 10 LDR R3, [R0] ;get the value to normalize 11 CMP R3, R1 ;is it a non-zero value 12 BEQ Done ;yes => already normalised 13 Loop 14 ADD R1, R1, #1 ;increment shift counter 15 MOVS R3, R3, LSL #0x1 ;shift value by one bit 16 BPL Loop ;loop until upper bit (sign bit) set 17 Done 18 STR R1, Shifted ;otherwise done - store result 19 STR R3, Normal 20 SWI &11 ;exit CMP/BEQ Protect from zero entry Otherwise we will enter a never ending loop

Structure / Loops – p. 12/12

slide-59
SLIDE 59

Program: normalize.s

1 ; normalize a binary number 2 7 Main 8 LDR R0, =Data1 ;load the address of the lookup table 9 EOR R1, R1, R1 ;clear R1 to store shift count 10 LDR R3, [R0] ;get the value to normalize 11 CMP R3, R1 ;is it a non-zero value 12 BEQ Done ;yes => already normalised 13 Loop 14 ADD R1, R1, #1 ;increment shift counter 15 MOVS R3, R3, LSL #0x1 ;shift value by one bit 16 BPL Loop ;loop until upper bit (sign bit) set 17 Done 18 STR R1, Shifted ;otherwise done - store result 19 STR R3, Normal 20 SWI &11 ;exit ADD Increment shift counter

Structure / Loops – p. 12/12

slide-60
SLIDE 60

Program: normalize.s

1 ; normalize a binary number 2 7 Main 8 LDR R0, =Data1 ;load the address of the lookup table 9 EOR R1, R1, R1 ;clear R1 to store shift count 10 LDR R3, [R0] ;get the value to normalize 11 CMP R3, R1 ;is it a non-zero value 12 BEQ Done ;yes => already normalised 13 Loop 14 ADD R1, R1, #1 ;increment shift counter 15 MOVS R3, R3, LSL #0x1 ;shift value by one bit 16 BPL Loop ;loop until upper bit (sign bit) set 17 Done 18 STR R1, Shifted ;otherwise done - store result 19 STR R3, Normal 20 SWI &11 ;exit MOVS/LSL Shift value up by one bit and set flags

Structure / Loops – p. 12/12

slide-61
SLIDE 61

Program: normalize.s

1 ; normalize a binary number 2 7 Main 8 LDR R0, =Data1 ;load the address of the lookup table 9 EOR R1, R1, R1 ;clear R1 to store shift count 10 LDR R3, [R0] ;get the value to normalize 11 CMP R3, R1 ;is it a non-zero value 12 BEQ Done ;yes => already normalised 13 Loop 14 ADD R1, R1, #1 ;increment shift counter 15 MOVS R3, R3, LSL #0x1 ;shift value by one bit 16 BPL Loop ;loop until upper bit (sign bit) set 17 Done 18 STR R1, Shifted ;otherwise done - store result 19 STR R3, Normal 20 SWI &11 ;exit BPL Repeat until upper bit (sign bit) is positive

Structure / Loops – p. 12/12