Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithm vs. Assembler - basics. Algorithm vs. Assembler 2/15 Developing software in assembler The best way of developing program in machine language.

Similar presentations


Presentation on theme: "Algorithm vs. Assembler - basics. Algorithm vs. Assembler 2/15 Developing software in assembler The best way of developing program in machine language."— Presentation transcript:

1 Algorithm vs. Assembler - basics

2 Algorithm vs. Assembler 2/15 Developing software in assembler The best way of developing program in machine language is working according to the following scheme: 1.Analyzing the task 2.Defining needed data structures 3.Developing algorithm by: developing own, original ideas; applying already known solution. 3.Describing algorithm in chosen form (text form, decision tree, high-level language syntax, block diagrams) 4.Implementing in target device language.

3 Algorithm vs. Assembler 3/15 … av:=(var1+var2)/2 … A  M[var1] A:=A+ M[var2] CyA:=CyA>>1 M[av]  A … R0:= var1 A  M[R0] R1:= var2 A:=A+ M[R1] CyA:=CyA>>1 R1:=av M[R1]  A …

4 ; variables located inside internal indirectly addressed ; data memory (IM) isegat0x90 var1:ds1;’var1’ in IM[90h] var2:ds1;’var2’ in IM[91h] av:idatavar2+1;’av’ in IM[92h]... MOVR0,#var1;R0->var1 MOVA,@R0;copying var1 to A MOVR1,#var2;R1->var2 ADDA,@R1;adding RRCA;shift right by 1 bit (dividing by 2) MOVR1,#av;R1->av LD@R1,A;storing result to av Algorithm vs. Assembler 4/15

5 ; variables located inside internal directly addressed ; data memory (DM) dsegat0x30 var1:ds1;’var1’ in DM[30h] var2:ds1;’var2’ in DM[31h] av:datavar2+1;’av’ in DM[32h]... MOVA,var1;copying var1 to A ADDA,var2;adding RRCA;shift right by 1 bit (dividing by 2) MOVav,A;storing result to av Algorithm vs. Assembler 5/15

6 ; variables located inside external data memory (XM) xsegat0x8090 var1:ds1;’var1’ in XM[8090h] var2:ds1;’var2’ in XM[8091h] av:xdatavar2+1;’av’ in XM[8092h]... MOVDPTR,#var1;DPTR->var1 MOVXA,@DPTR;copying var1 to A MOVB,A;and to register B MOVDPTR,#var2;DPTR->var2 MOVXA,@DPTR;copying var2 to A ADDA,B;adding RRCA;shift right by 1 bit (dividing by 2) MOVDPTR,#av;DPTR->av MOVX@DPTR,A;storing result to av Algorithm vs. Assembler 6/15

7 Algorithm vs. Assembler 7/15 var1 < var2 ? F T A < var2 ? FT A  M[var1] MOVA,var1;copying variable var1 to A CJNEA,var2,neq;comparing with var2 neq:JClessthan;jump if A<var2 MOVA,var1;copying variable var1 to A CLRC;clearing CY SUBBA,var2 JClessthan; jump if A<var2

8 Algorithm vs. Assembler 8/15 MOVA,var1;copying variable var1 to A CJNEA,var2,neq;comparing to var2 ;no jump if var1=var2... SJMPcont neq:JClessthan;jump if A<var2 ;here if var1>var2... SJMPcont lessthan:... cont: var1-var2 >0<0 =0

9 Determining whether two multi-byte variables are identical can be achieved in the following way: ; assumption: var1, var2 – start addresses of 2 variables ; located in internal data memory MOVA,var1;A<-1st byte of var1 XRLA,var2;A:=A xor var2 JNZdiff;jump if byte differ MOVA,var1+1;A<-2nd byte of var1 XRLA,var2+1;A:=A xor var2 JNZdiff;jump if byte differ... MOVA,var1+k-1;A<-last byte of var1 XRLA,var2+k-1;A:=A xor var2 JNZdiff;jump if byte differ equal: ;here if var1=var2 Algorithm vs. Assembler 9/15

10 The zero value of multi-byte variable can be detecting in one another way. Suggested solution is to calculate logical sum of all bytes of the variable and check whether it is equal zero: LDA,var4;A<-1st byte of var4 ORLA,var4+1;logical sum with following bytes ORLA,var4+2 ORLA,var4+3 JRNZ,nozero;jump if var4 is not equal zero ; here if var4=0 Algorithm vs. Assembler 10/15

11 „Direct” translation to assembler: MOVR3,#0;lc in R3 cleared rep:;loop label ;... other repeated operations INCR3;incrementing lc counter CJNER3,#rn,rep;comparing lc with rn Algorithm vs. Assembler 11/15 … lc:=lc+1 lc < rn ? T F … lc:= 0 lc – loop counter rn – number of repetitions

12 or: MOVR1,#lp;R1->lc in internal data memory MOV@R1,#0;clearing lc rep:;loop label ;... other repeated operations INC@R1;incrementing lc counter CJNE@R1,#rn,rep;comparing lc with rn or: MOVlp,#0;clearing lc rep:;loop label ;... other repeated operations INClp;incrementing lc counter MOVA,#rn;A:=rn CJNEA,lp,rep;comparing A=rn with lc Algorithm vs. Assembler 12/15

13 Better solution is the usage of DJNZ instruction: MOVR6,#rn;initialisation lc in R6 to rn rep:;loop label ;... other repeated operations DJNZR6,rep;lc:=lc-1 & jump if lc>0 or: MOVlc,#rn ;initialisation lc in RAM to rn rep:;loop label ;... other repeated operations DJNZlc,rep;lc:=lc-1 & jump if lc>0 If the value of rn is greater than 256: MOVR7,#(rn-1)/256;external loop counter in R7 MOVR6,#rn MOD 256;internal loop counter in R6 rep:;loop label ;... other repeated operations DJNZR6,rep DJNZR7,rep Algorithm vs. Assembler 13/15

14 Algorithm vs. Assembler 14/15 … A  tab[i,j] … j := j + 1 j < col_nmbr ? T F j := 0 i := i + 1 i < row_nmbr ? T F i := 0 Nested loops:

15 Algorithm vs. Assembler 15/15 Exemplary implementation: col_nmbrequ20 row_nmbrequ33 XSEG ORG0x2000 table:dscol_nmbr*row_nmbr ;allocation of 660 bytes for ;‘table’ inside XDATA ;starting from 2000h in external data memory CSEG;back to CODE segment ;... MOVDPTR,#table MOVR7,#row_nmbr;row counter initialization nxt_row: MOVR6,#col_nmbr;column counter initialization nxt_el: ;... ; access to element of the table, for example: MOVXA,@DPTR ;... INCDPTR;next element of the table DJNZR6,nxt_el DJNZR7,nxt_row


Download ppt "Algorithm vs. Assembler - basics. Algorithm vs. Assembler 2/15 Developing software in assembler The best way of developing program in machine language."

Similar presentations


Ads by Google