12.07.2015 Views

Lecture 5 More on assembly language programming The S-bit ...

Lecture 5 More on assembly language programming The S-bit ...

Lecture 5 More on assembly language programming The S-bit ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<str<strong>on</strong>g>Lecture</str<strong>on</strong>g> 5 <str<strong>on</strong>g>More</str<strong>on</strong>g> <strong>on</strong> <strong>assembly</strong> <strong>language</strong><strong>programming</strong><strong>The</strong> S-<strong>bit</strong>◆<strong>The</strong> basic branch instructi<strong>on</strong> is:◆<strong>The</strong> loop program can be simplied to:◆◆◆label labelB label label ;; unc<strong>on</strong>diti<strong>on</strong>ally branch to to label label…… ………… …………. …….C<strong>on</strong>diti<strong>on</strong>al branch instructi<strong>on</strong>s can be used to c<strong>on</strong>trol loops:MOV MOV r0, r0, #10 #10 ;; intialize loop loop counted r0 r0loop loop ……. ……. ;; start start of of body body of of loop loop……. …….SUB SUB r0, r0, r0, r0, #1 #1 ;; decrement loop loop counterCMP CMP r0, r0, #0 #0 ;; is is it it zero zero yet? yet?BNE BNE loop loop ;; branch if if r0 r0 ≠ 0c 0c<strong>The</strong> CMP instructi<strong>on</strong> gives no results EXCEPT possibly changingc<strong>on</strong>diti<strong>on</strong>al codes in CPSR.If r0=0, then Z <strong>bit</strong> is set (='1'), else Z <strong>bit</strong> is reset (='0')◆◆◆◆MOV MOV r0, r0, #10 #10 ;; intialize loop loop counted r0 r0loop loop ……. ……. ;; start start of of body body of of loop loop……. …….SUBS SUBS r0, r0, r0, r0, #1 #1 ;; decrement loop loop counterBNE BNE loop loop ;; branch branch if if r0 r0 ≠ 0c 0cSUBS instructi<strong>on</strong> is the same as SUB except that the former canchange the cc in CPSR.After SUBS instructi<strong>on</strong>, Z-<strong>bit</strong> is set or cleared depending <strong>on</strong> the resultof the subtracti<strong>on</strong>.All data processing instructi<strong>on</strong>s such as ADD, ADC etc. can have Sadded to indicate that c<strong>on</strong>diti<strong>on</strong> code should be updated.We have seen how to use a c<strong>on</strong>diti<strong>on</strong>al branch instructi<strong>on</strong> BNE inloops. <strong>The</strong>re are in fact many more c<strong>on</strong>diti<strong>on</strong>al branch instructi<strong>on</strong>s.pykc - 28-Jan-00EE2 ComputingSpring Term <str<strong>on</strong>g>Lecture</str<strong>on</strong>g> 5- 1pykc - 28-Jan-00EE2 ComputingSpring Term <str<strong>on</strong>g>Lecture</str<strong>on</strong>g> 5- 2C<strong>on</strong>diti<strong>on</strong>al Branch Instructi<strong>on</strong>sC<strong>on</strong>diti<strong>on</strong>al Executi<strong>on</strong>◆◆C<strong>on</strong>diti<strong>on</strong>al executi<strong>on</strong> applies not <strong>on</strong>ly to branches, but to all ARMinstructi<strong>on</strong>s.For example:CMP CMP r0, r0, #5 #5 ;; if if (r0 (r0!= != 5) 5) then thenBEQ BEQ BYPASSADD ADD r1, r1, r1, r1, r0 r0 ;; r1 r1 := := r1 r1 + r0 r0 -- r2 r2SUB SUB r1, r1, r1, r1, r2 r2BYPASS ….. …..◆Can be replaced by:BYPASSCMP CMP r0, r0, #5 #5 ;; if if (r0 (r0!=!= 5) 5) then thenADDNE r1, r1, r1, r1, r0 r0 ;; r1 r1 := := r1 r1 + r0 r0 -- r2 r2SUBNE r1, r1, r1, r1, r2 r2….. …..◆Note that BCC = BLO, BCS = BHS◆Here the ADDNE and SUBNE instructi<strong>on</strong>s are executed <strong>on</strong>ly ifZ='0', i.e. the CMP instructi<strong>on</strong> gives n<strong>on</strong>-zero result.pykc - 28-Jan-00EE2 ComputingSpring Term <str<strong>on</strong>g>Lecture</str<strong>on</strong>g> 5- 3pykc - 28-Jan-00EE2 ComputingSpring Term <str<strong>on</strong>g>Lecture</str<strong>on</strong>g> 5- 4


C<strong>on</strong>diti<strong>on</strong>al Executi<strong>on</strong> - moreC<strong>on</strong>diti<strong>on</strong>al Executi<strong>on</strong> - Summary◆Here is another very clever use of this unique feature in ARMinstructi<strong>on</strong> set. Do remember ALL instructi<strong>on</strong>s can be qualified bythe c<strong>on</strong>diti<strong>on</strong> codes.;; if if (((a==b) (a==b) && && (c==d)) (c==d)) then then e := := e + 1; 1;CMP CMP r0, r0, r1 r1 ;; r0 r0 has has a, a, r1 r1 has has bCMPEQ r2, r2, r3 r3;; r2 r2 has has c, c, r3 r3 has has dADDEQ r4, r4, r4, r4, #1 #1 ;; e := := e+1 e+1◆◆◆Note how if the first comparis<strong>on</strong> finds unequal operands, thesec<strong>on</strong>d and third instructi<strong>on</strong>s are both skipped.Also the logical 'and' in the if clause if implemented by making thesec<strong>on</strong>d comparis<strong>on</strong> c<strong>on</strong>diti<strong>on</strong>al.C<strong>on</strong>diti<strong>on</strong>al executi<strong>on</strong> is <strong>on</strong>ly efficient if the c<strong>on</strong>diti<strong>on</strong>al sequence isthree instructi<strong>on</strong>s or fewer. If the c<strong>on</strong>diti<strong>on</strong>al sequence is l<strong>on</strong>ger,use a proper loop.pykc - 28-Jan-00EE2 ComputingSpring Term <str<strong>on</strong>g>Lecture</str<strong>on</strong>g> 5- 5pykc - 28-Jan-00EE2 ComputingSpring Term <str<strong>on</strong>g>Lecture</str<strong>on</strong>g> 5- 6Shifted Register OperandsARM shift operati<strong>on</strong>s - LSL and LSR◆ARM has another very clever feature. In any data processinginstructi<strong>on</strong>s, the sec<strong>on</strong>d register operand can have a shiftoperati<strong>on</strong> applied to it. For example:◆Here are all the six possible ARM shift operati<strong>on</strong>s you can use:ADD ADD r3, r3, r2, r2, r1, r1, LSL LSL #3 #3 ;; r3 r3 := := r2 r2 + 8 x r1 r1◆◆◆◆Herer LSL means 'logical shift left by the specified number of <strong>bit</strong>s.Note that this is still a single ARM instructi<strong>on</strong>, executed in a singleclock cyle.In most processors, this is a separate instructi<strong>on</strong>s, while ARMintegrates this shifting into the ALU.It is also possible to use a register value to specify the number of<strong>bit</strong>s the sec<strong>on</strong>d operand should be shifted by:ADD ADD r5, r5, r5, r5, r3, r3, LSL LSL r2 r2 ;; r5 r5 := := r5 r5 + r3 r3 x 2**r1 2**r1◆◆LSL: logical shift left by 0 to 31 places; fill the vacated <strong>bit</strong>s at theleast significant end of the word with zeros.LSR: logical shift right by 0 to 32 places; fill the vacated <strong>bit</strong>s at themost significant end of the word with zeros.pykc - 28-Jan-00EE2 ComputingSpring Term <str<strong>on</strong>g>Lecture</str<strong>on</strong>g> 5- 7pykc - 28-Jan-00EE2 ComputingSpring Term <str<strong>on</strong>g>Lecture</str<strong>on</strong>g> 5- 8


ARM shift operati<strong>on</strong>s - ASL and ASRARM shift operati<strong>on</strong>s - ROR and RRX◆◆ASL: arithmetic shift left; this is the same as LSLASR: arithmetic shift right by 0 to 32 places; fill the vacated <strong>bit</strong>s atthe most significant end of the word with zeros if the sourceoperand was positive, and with <strong>on</strong>es it is negative. That is, signextend while shifting right.◆◆ROR: rotate right by 0 to 32 places; the <strong>bit</strong>s which fall off the leastsignificant end are used to fill the vacated <strong>bit</strong>s at the mostsignificant end of the word.RRX: rotate right extended by 1 place; the vacated <strong>bit</strong> (<strong>bit</strong> 31) isfilled with the old value of the C flag and the operand is shifted <strong>on</strong>eplace to the right. This is effectively a 33 <strong>bit</strong> rotate using theregister and the C flag.pykc - 28-Jan-00EE2 ComputingSpring Term <str<strong>on</strong>g>Lecture</str<strong>on</strong>g> 5- 9pykc - 28-Jan-00EE2 ComputingSpring Term <str<strong>on</strong>g>Lecture</str<strong>on</strong>g> 5- 10A simple <strong>assembly</strong> <strong>language</strong> program- Hello world!◆We will now c<strong>on</strong>sider two simple <strong>assembly</strong> <strong>language</strong> programs.<strong>The</strong> first outputs "Hello World!" <strong>on</strong> the c<strong>on</strong>sole window:AREA AREA helloW, helloW, CODE, CODE, READONLY READONLY ;; declare declare code code area areaSWI_WriteC SWI_WriteC EQU EQU &0 &0 ;; output output character character in in r0 r0SWI_Exit SWI_Exit EQU EQU &11 &11 ;; finish finish program programENTRY ENTRY;; code code entry entry point pointSTART START ADR ADR r1, r1, TEXT TEXT ;; r1 r1 -> ->"Hello World!" World!"LOOP LOOP LDRB LDRB r0, r0, [r1], [r1],#1 #1 ;; get get the the next next byte byteCMP CMP r0, r0, #0 #0 ;; check check for for 'null' 'null' character characterSWINE SWINE SWI_WriteC SWI_WriteC ;; if if not not end, end, print print …. ….BNE BNE LOOP LOOP ;; … and and loop loop back backSWI SWI SWI_Exit SWI_Exit ;; end end of of executi<strong>on</strong> executi<strong>on</strong>TEXT TEXT = "Hello "Hello World!", World!", &0a, &0a,&0d, &0d, 0 ;; string string + CR CR + LF LF + null nullEND ENDAnother Example: Block copy◆Here is another example to block copy from <strong>on</strong>e address (TABLE1)to another (TABLE2), then write it out:AREAAREABlkCpy,BlkCpy,CODE,CODE,READONLYREADONLY;;declaredeclarecodecodeareaareaSWI_WriteCSWI_WriteCEQUEQU&0&0;;outputoutputcharactercharacterininr0r0SWI_ExitSWI_ExitEQUEQU&11&11;;finishfinishprogramprogramENTRYENTRY;;codecodeentryentrypointpointSTARTSTARTADRADRr1,r1,TABLE1TABLE1;;r1r1->->TABLE1TABLE1ADRADRr2,r2,TABLE2TABLE2;;r2r2->->TABLE2TABLE2ADRADRr3,r3,T1ENDT1END;;r3r3->->endendofofTABLE1TABLE1LOOP1LOOP1LDRLDRr0,r0,[r1],[r1],#4#4;;getgetTABLE1TABLE11st1stwordwordSTRSTRr0,r0,[r2],[r2],#4#4;;copycopyintointoTABLE2TABLE2CMPCMPr1,r1,r3r3;;finished?finished?BLTBLTLOOP1LOOP1;;ififnot,not,dodomore,more,elseelseprintprintADRADRr1,r1,TABLE2TABLE2;;r1r1->->TABLE2TABLE2LOOP2LOOP2CMPCMPr0,r0,#0#0;;checkcheckforforendendofoftexttextstringstringSWINESWINESWI_WriteCSWI_WriteC;;ififnotnotend,end,printprint…BNEBNELOOP2LOOP2;;….….andandlooploopbackbackSWISWISWI_ExitSWI_Exit;;finishfinishTABLE1TABLE1=="This"Thisisisthetherightrightstring!",string!",&0a,&0a,&0d,&0d,00T1ENDT1ENDALIGNALIGN;;ensureensurewordwordalignmentalignmentTABLE2TABLE2=="This"Thisisisthethewr<strong>on</strong>gwr<strong>on</strong>gstring!",string!",00ENDENDpykc - 28-Jan-00EE2 ComputingSpring Term <str<strong>on</strong>g>Lecture</str<strong>on</strong>g> 5- 11pykc - 28-Jan-00EE2 ComputingSpring Term <str<strong>on</strong>g>Lecture</str<strong>on</strong>g> 5- 12

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!