ARM Assembler: Structure / Loops
ARM Assembler: Structure / Loops
ARM Assembler
Structure / Loops
Loops
Four parts to any loop i) initialisation init ii) body iii) increment or decrement iv) exit condition body inc dec cond
Three basic types of loop i) Repeat . . . Until ii) While iii) Counted
Structure / Loops p. 2/12
Assembler: init label1 cond B cc label2 body inc / dec BAL label1 label2 . . .
Structure / Loops p. 4/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 MOV MOV Loop LDRB ADD ADD ADD CMP BLT
R0, =Table R1, #0 R2, #0 R3, [R0] R1, R1, R3 R0, R0, #1 R2, R2, #1 R2, #10 Loop
R0, =Table R1, #0 R2, #10 R3, [R0] R1, R1, R3 R0, R0, #1 R2, R2, #1 Loop
Structure / Loops p. 6/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 MOV MOV Loop LDRB ADD ADD ADD CMP BLT
R0, =Table R1, #0 R2, #0 R3, [R0] R1, R1, R3 R0, R0, #1 R2, R2, #1 R2, #10 Loop
R0, =Table R1, #0 R2, #10 R3, [R0] R1, R1, R3 R0, R0, #1 R2, R2, #1 Loop
Structure / Loops p. 6/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 MOV MOV Loop LDRB ADD ADD ADD CMP BLT
R0, =Table R1, #0 R2, #0 R3, [R0] R1, R1, R3 R0, R0, #1 R2, R2, #1 R2, #10 Loop
R0, =Table R1, #0 R2, #10 R3, [R0] R1, R1, R3 R0, R0, #1 R2, R2, #1 Loop
Structure / Loops p. 6/12
Program: sum16.s
7 8 9 10 11 12 13 14 15 16 19 22 24 28 29 31 Main LDR EOR LDR Loop LDR ADD ADD SUBS BNE Table DCW DCW TablEnd DCD Length DCW R3, [R0] ;get the data R1, R1, R3 ;add it to r1 R0, R0, #+4 ;increment pointer R2, R2, #1 ;decrement count with zero set Loop ;if zero ag is not set, loop &2040 &1C22 0 ;table of values to be added R0, =Data1 ;load the address of the lookup table R1, R1, R1 ;clear R1 to store sum R2, Length ;init element count
Program: sum16.s
7 8 9 10 11 12 13 14 15 16 19 22 24 28 29 31 EOR Main LDR EOR LDR Loop LDR ADD ADD SUBS BNE Table DCW DCW TablEnd DCD Length DCW R3, [R0] ;get the data R1, R1, R3 ;add it to r1 R0, R0, #+4 ;increment pointer R2, R2, #1 ;decrement count with zero set Loop ;if zero ag is not set, loop &2040 &1C22 0 ;table of values to be added R0, =Data1 ;load the address of the lookup table R1, R1, R1 ;clear R1 to store sum R2, Length ;init element count
Program: sum16.s
7 8 9 10 11 12 13 14 15 16 19 22 24 28 29 31 Loop Main LDR EOR LDR Loop LDR ADD ADD SUBS BNE Table DCW DCW TablEnd DCD Length DCW R3, [R0] ;get the data R1, R1, R3 ;add it to r1 R0, R0, #+4 ;increment pointer R2, R2, #1 ;decrement count with zero set Loop ;if zero ag is not set, loop &2040 &1C22 0 ;table of values to be added R0, =Data1 ;load the address of the lookup table R1, R1, R1 ;clear R1 to store sum R2, Length ;init element count
Program: sum16.s
7 8 9 10 11 12 13 14 15 16 19 22 24 28 29 31 ADD Main LDR EOR LDR Loop LDR ADD ADD SUBS BNE Table DCW DCW TablEnd DCD Length DCW R3, [R0] ;get the data R1, R1, R3 ;add it to r1 R0, R0, #+4 ;increment pointer R2, R2, #1 ;decrement count with zero set Loop ;if zero ag is not set, loop &2040 &1C22 0 ;table of values to be added R0, =Data1 ;load the address of the lookup table R1, R1, R1 ;clear R1 to store sum R2, Length ;init element count
Program: sum16.s
7 8 9 10 11 12 13 14 15 16 19 22 24 28 29 31 Main LDR EOR LDR Loop LDR ADD ADD SUBS BNE Table DCW DCW TablEnd DCD Length DCW R3, [R0] ;get the data R1, R1, R3 ;add it to r1 R0, R0, #+4 ;increment pointer R2, R2, #1 ;decrement count with zero set Loop ;if zero ag is not set, loop &2040 &1C22 0 ;table of values to be added R0, =Data1 ;load the address of the lookup table R1, R1, R1 ;clear R1 to store sum R2, Length ;init element count
LDR/ADD
Using Post-index addressing we can remove the ADD: LDR R3, [R0], #4
Structure / Loops p. 7/12
Program: sum16.s
7 8 9 10 11 12 13 14 15 16 19 22 24 28 29 31 SUBS Main LDR EOR LDR Loop LDR ADD ADD SUBS BNE Table DCW DCW TablEnd DCD Length DCW R3, [R0] ;get the data R1, R1, R3 ;add it to r1 R0, R0, #+4 ;increment pointer R2, R2, #1 ;decrement count with zero set Loop ;if zero ag is not set, loop &2040 &1C22 0 ;table of values to be added R0, =Data1 ;load the address of the lookup table R1, R1, R1 ;clear R1 to store sum R2, Length ;init element count
Program: sum16.s
7 8 9 10 11 12 13 14 15 16 19 22 24 28 29 31 BNE Main LDR EOR LDR Loop LDR ADD ADD SUBS BNE Table DCW DCW TablEnd DCD Length DCW R3, [R0] ;get the data R1, R1, R3 ;add it to r1 R0, R0, #+4 ;increment pointer R2, R2, #1 ;decrement count with zero set Loop ;if zero ag is not set, loop &2040 &1C22 0 ;table of values to be added R0, =Data1 ;load the address of the lookup table R1, R1, R1 ;clear R1 to store sum R2, Length ;init element count
Program: sum16.s
7 8 9 10 11 12 13 14 15 16 19 22 24 28 29 31 DCW Main LDR EOR LDR Loop LDR ADD ADD SUBS BNE Table DCW DCW TablEnd DCD Length DCW R3, [R0] ;get the data R1, R1, R3 ;add it to r1 R0, R0, #+4 ;increment pointer R2, R2, #1 ;decrement count with zero set Loop ;if zero ag is not set, loop &2040 &1C22 0 ;table of values to be added R0, =Data1 ;load the address of the lookup table R1, R1, R1 ;clear R1 to store sum R2, Length ;init element count
Program: sum16b.s
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 Main LDR EOR LDR CMP BEQ Loop LDR ADD ADD SUBS BNE Done STR SWI R1, Result &11 ;otherwise done - store result R3, [R0] ;get the data that R0 points to R1, R1, R3 ;add it to R1 R0, R0, #+4 ;increment pointer R2, R2, #0x1 ;decrement count with zero set Loop ;if zero ag is not set, loop R0, =Data1 R1, R1, R1 R2, Length R2, #0 Done ;load the address of the lookup table ;clear R1 to store sum ;init element count ;zero length table ? ;yes => skip over sum loop
Program: sum16b.s
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 EOR Main LDR EOR LDR CMP BEQ Loop LDR ADD ADD SUBS BNE Done STR R1, Result ;otherwise done - store result SWI &11 Quick way of setting R1 to zero R3, [R0] ;get the data that R0 points to R1, R1, R3 ;add it to R1 R0, R0, #+4 ;increment pointer R2, R2, #0x1 ;decrement count with zero set Loop ;if zero ag is not set, loop R0, =Data1 R1, R1, R1 R2, Length R2, #0 Done ;load the address of the lookup table ;clear R1 to store sum ;init element count ;zero length table ? ;yes => skip over sum loop
Program: sum16b.s
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 CMP Main LDR EOR LDR CMP BEQ Loop LDR ADD ADD SUBS BNE Done STR R1, Result ;otherwise done - store result SWI &11 Is table length zero? R3, [R0] ;get the data that R0 points to R1, R1, R3 ;add it to R1 R0, R0, #+4 ;increment pointer R2, R2, #0x1 ;decrement count with zero set Loop ;if zero ag is not set, loop R0, =Data1 R1, R1, R1 R2, Length R2, #0 Done ;load the address of the lookup table ;clear R1 to store sum ;init element count ;zero length table ? ;yes => skip over sum loop
Program: sum16b.s
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 BEQ Main LDR EOR LDR CMP BEQ Loop LDR ADD ADD SUBS BNE Done STR R1, Result ;otherwise done - store result SWI &11 Skip zero length tables Protects from processing an empty list
Structure / Loops p. 8/12
;load the address of the lookup table ;clear R1 to store sum ;init element count ;zero length table ? ;yes => skip over sum loop
R3, [R0] ;get the data that R0 points to R1, R1, R3 ;add it to R1 R0, R0, #+4 ;increment pointer R2, R2, #0x1 ;decrement count with zero set Loop ;if zero ag is not set, loop
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 ag 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
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 ag 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
Program: countneg.s
10 11 12 13 14 15 16 17 18 19 20 21 27 28 31 34 LDR CMP BEQ Loop LDR CMP BPL ADD Looptest ADD SUBS BNE Table DCD TablEnd DCD Length DCW R0, R0, #+4 ;increment pointer R2, R2, #0x1 ;decrement count with zero set Loop ;until count is zero &F1522040 ;table of values to be added 0 (TablEnd - Table) / 4 ;because were having to align R3, [R0] R3, #0 Looptest R1, R1, #1 ;get the data ;is it positive? ;yes => skip next line ;increment -ve number count R2, Length R2, #0 Done ;init element count ;is table empty ;yes => skip loop
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 0 34 Length DCW (TablEnd - Table) / 4 ;because were having to align CMP/BEQ Skip zero length tables
Program: countneg.s
10 11 12 13 14 15 16 17 18 19 20 21 27 28 31 34 BPL LDR CMP BEQ Loop LDR CMP BPL ADD Looptest ADD SUBS BNE R0, R0, #+4 ;increment pointer R2, R2, #0x1 ;decrement count with zero set Loop ;until count is zero R3, [R0] R3, #0 Looptest R1, R1, #1 ;get the data ;is it positive? ;yes => skip next line ;increment -ve number count R2, Length R2, #0 Done ;init element count ;is table empty ;yes => skip loop
Table DCD &F1522040 ;table of values to be added TablEnd DCD 0 Length DCW (TablEnd - Table) / 4 ;because were having to align Brach if positive (plus)
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 0 34 Length DCW (TablEnd - Table) / 4 ;because were 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 ush the pipeline
Structure / Loops p. 9/12
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 0 34 Length DCW (TablEnd - Table) / 4 ;because were 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
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 0 34 Length DCW (TablEnd - Table) / 4 ;because were having to align SUBS/BNE Decrement counter and branch to Loop if not zero
Program: countneg.s
10 11 12 13 14 15 16 17 18 19 20 21 27 28 31 34 DCW LDR CMP BEQ Loop LDR CMP BPL ADD Looptest ADD SUBS BNE R0, R0, #+4 ;increment pointer R2, R2, #0x1 ;decrement count with zero set Loop ;until count is zero R3, [R0] R3, #0 Looptest R1, R1, #1 ;get the data ;is it positive? ;yes => skip next line ;increment -ve number count R2, Length R2, #0 Done ;init element count ;is table empty ;yes => skip loop
Table DCD &F1522040 ;table of values to be added TablEnd DCD 0 Length DCW (TablEnd - Table) / 4 ;because were having to align Assembler will calculate the length of data table
Program: countneg16.s
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 Main LDR EOR LDR TEQ BEQ Loop LDR AND CMP BNE ADD Looptest ADD SUBS BNE R3, [R0] ;get the data R3, R3, #0x8000 ;bit wise AND to see if the 16th R3, #0x8000 ;bit is 1 Looptest ;skip next line if zero R1, R1, #1 ;increment -ve number count R0, R0, #+4 R2, R2, #0x1 Loop ;increment pointer ;decrement count with zero set ;if zero ag is not set, loop R0, =Data1 R1, R1, R1 R2, Length R2, #0 Done ;load the address of the lookup table ;clear R1 to store count ;init element count ;is count zero? ;yes => skip loop
Program: countneg16.s
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 TEQ Main LDR EOR LDR TEQ BEQ Loop LDR R3, [R0] ;get the data AND R3, R3, #0x8000 ;bit wise AND to see if the 16th CMP R3, #0x8000 ;bit is 1 BNE Looptest ;skip next line if zero ADD R1, R1, #1 ;increment -ve number count Looptest ADD R0, R0, #+4 ;increment pointer SUBS R2, R2, #0x1 ;decrement count with zero set BNE Loop ;if zero ag is not set, loop Test R2 for 0 R0, =Data1 R1, R1, R1 R2, Length R2, #0 Done ;load the address of the lookup table ;clear R1 to store count ;init element count ;is count zero? ;yes => skip loop
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 ag is not set, loop TEQ/BEQ Protect loop from zero length tables
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 ag is not set, loop AND Clear all but halfword sign Reset lower 15 bits
Structure / Loops p. 10/12
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 ag is not set, loop CMP Is halfword sign bit (bit 15) set (negative)
Program: countneg16.s
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 BNE Main LDR EOR LDR TEQ BEQ Loop LDR R3, [R0] ;get the data AND R3, R3, #0x8000 ;bit wise AND to see if the 16th CMP R3, #0x8000 ;bit is 1 BNE Looptest ;skip next line if zero ADD R1, R1, #1 ;increment -ve number count Looptest ADD R0, R0, #+4 ;increment pointer SUBS R2, R2, #0x1 ;decrement count with zero set BNE Loop ;if zero ag is not set, loop Skip to Looptest if value is positive R0, =Data1 R1, R1, R1 R2, Length R2, #0 Done ;load the address of the lookup table ;clear R1 to store count ;init element count ;is count zero? ;yes => skip loop
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 ag 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
Program: largest16.s
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 Main LDR EOR LDR CMP BEQ Loop LDR CMP BLS MOV Looptest ADD SUBS BNE Done R3, [R0] R3, R1 Looptest R1, R3 ;get the data ;compare to largest ;skip next line if zero ;increment -ve number count R0, =Data1 R1, R1, R1 R2, Length R2, #0 Done ;load the address of the lookup table ;clear R1 to store largest ;init element count ;is it an empty table ;yes => skip loop
R0, R0, #+4 ;increment pointer R2, R2, #0x1 ;decrement count with zero set Loop ;if zero ag is not set, loop
Program: largest16.s
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 EOR Main LDR EOR LDR CMP BEQ Loop LDR R3, [R0] ;get the data CMP R3, R1 ;compare to largest BLS Looptest ;skip next line if zero MOV R1, R3 ;increment -ve number count Looptest ADD R0, R0, #+4 ;increment pointer SUBS R2, R2, #0x1 ;decrement count with zero set BNE Loop ;if zero ag is not set, loop Done Quick way of setting R1 to zero R0, =Data1 R1, R1, R1 R2, Length R2, #0 Done ;load the address of the lookup table ;clear R1 to store largest ;init element count ;is it an empty table ;yes => skip loop
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 ag is not set, loop 22 Done CMP/BEQ Protect loop from empty list (zero length)
Program: largest16.s
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 CMP Main LDR EOR LDR CMP BEQ Loop LDR R3, [R0] ;get the data CMP R3, R1 ;compare to largest BLS Looptest ;skip next line if zero MOV R1, R3 ;increment -ve number count Looptest ADD R0, R0, #+4 ;increment pointer SUBS R2, R2, #0x1 ;decrement count with zero set BNE Loop ;if zero ag is not set, loop Done Compare new value (R3) against current largest (R1) R0, =Data1 R1, R1, R1 R2, Length R2, #0 Done ;load the address of the lookup table ;clear R1 to store largest ;init element count ;is it an empty table ;yes => skip loop
Program: largest16.s
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 BLS Main LDR EOR LDR CMP BEQ Loop LDR R3, [R0] ;get the data CMP R3, R1 ;compare to largest BLS Looptest ;skip next line if zero MOV R1, R3 ;increment -ve number count Looptest ADD R0, R0, #+4 ;increment pointer SUBS R2, R2, #0x1 ;decrement count with zero set BNE Loop ;if zero ag is not set, loop Done Branch if new is less than or same as current R0, =Data1 R1, R1, R1 R2, Length R2, #0 Done ;load the address of the lookup table ;clear R1 to store largest ;init element count ;is it an empty table ;yes => skip loop
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 ag 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
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 ag 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
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 ag 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
Program: normalize.s
1 2 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ; normalize a binary number Main LDR EOR LDR CMP BEQ Loop ADD MOVS BPL Done STR STR SWI R1, Shifted R3, Normal &11 ;otherwise done - store result ;exit R1, R1, #1 ;increment shift counter R3, R3, LSL #0x1 ;shift value by one bit Loop ;loop until upper bit (sign bit) set R0, =Data1 R1, R1, R1 R3, [R0] R3, R1 Done ;load the address of the lookup table ;clear R1 to store shift count ;get the value to normalize ;is it a non-zero value ;yes => already normalised
Program: normalize.s
1 2 7 8 9 10 11 12 13 14 15 16 17 18 19 20 EOR ; normalize a binary number Main LDR EOR LDR CMP BEQ Loop ADD MOVS BPL Done STR R1, Shifted ;otherwise done - store result STR R3, Normal SWI &11 ;exit Quick way of setting R1 to zero R1, R1, #1 ;increment shift counter R3, R3, LSL #0x1 ;shift value by one bit Loop ;loop until upper bit (sign bit) set R0, =Data1 R1, R1, R1 R3, [R0] R3, R1 Done ;load the address of the lookup table ;clear R1 to store shift count ;get the value to normalize ;is it a non-zero value ;yes => already normalised
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
Program: normalize.s
1 2 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ADD ; normalize a binary number Main LDR EOR LDR CMP BEQ Loop ADD MOVS BPL Done STR R1, Shifted ;otherwise done - store result STR R3, Normal SWI &11 ;exit Increment shift counter R1, R1, #1 ;increment shift counter R3, R3, LSL #0x1 ;shift value by one bit Loop ;loop until upper bit (sign bit) set R0, =Data1 R1, R1, R1 R3, [R0] R3, R1 Done ;load the address of the lookup table ;clear R1 to store shift count ;get the value to normalize ;is it a non-zero value ;yes => already normalised
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 ags
Program: normalize.s
1 2 7 8 9 10 11 12 13 14 15 16 17 18 19 20 BPL ; normalize a binary number Main LDR EOR LDR CMP BEQ Loop ADD MOVS BPL Done STR R1, Shifted ;otherwise done - store result STR R3, Normal SWI &11 ;exit Repeat until upper bit (sign bit) is positive R1, R1, #1 ;increment shift counter R3, R3, LSL #0x1 ;shift value by one bit Loop ;loop until upper bit (sign bit) set R0, =Data1 R1, R1, R1 R3, [R0] R3, R1 Done ;load the address of the lookup table ;clear R1 to store shift count ;get the value to normalize ;is it a non-zero value ;yes => already normalised