8085_exambles

4
8085 EXAMBLES -----Count #1's ----- ;count number of 1's in x ;x at 0 ;result to 1 ;A is shifting x ;B is loop couonter, init to 8 ;C is 1's counter LDA 0 ;get x MV I B,8 ;loo p counter MVI C,0 ;count of 1's Loop: RLC ;shift left into Carry JNC skip ;if not 1 skip next INR C ;increment 1's counter Skip: DCR B ;decrement loop counter JNZ Loop ;not done all 8 MOV A,C STA 1 HLT ------DOUBLE A NUMBER------ ;double.85 ;double a number lda 0 ;A=M[0] mov b,a ;copy to B add b ;A=A+B sta 0 hlt -------IF EXAMBLE------ ;if.85 ;if x+y==0 then M[2]=0 else M[2]=1 lda 0 ;load x mov b,a ;B=x lda 1 ;load y add b ;A=x+y jnz Else ;if x+y!=0 jump Then: mvi a,0 jmp Endif ;skip over Else part Else: mvi a,1 Endif: sta 2 hlt -------SUM OF FIRST N INTEGERS------ ;sum_N_ints.85 ;sum of first N integers ;N=M[0], sum=M[1] lda 0 ;load N mov b,a ;B=N=i mvi a,0 NextI: add b ;A+=i

Upload: gardener10

Post on 08-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 8085_EXAMBLES

8/7/2019 8085_EXAMBLES

http://slidepdf.com/reader/full/8085exambles 1/4

Page 2: 8085_EXAMBLES

8/7/2019 8085_EXAMBLES

http://slidepdf.com/reader/full/8085exambles 2/4

dcr b ;i--jnz NextI ;goto Loop if i>0sta 1hlt

------FINDS HIGHEST AND LOWEST---

;Aman's "highest and lowest.85";modified and commented by DFW;Finds min and max bytes of N bytes starting at 0;N at 2000;D max;E min; for (i=1; i<N; i++); if (M[i] < D) D=M[i]; if (M[i] > D) E=M[i]

;lxi h,2000 ;HL=2000;mov c,m ;C=M[HL] =N

;easier to do this:lda 2000mov c,a

lxi h,0 ;HL=0mov d,m ;D=E=M[0]. first datummov e,m

dcr c ;N-1

;loop N-1 timesloop:

inx h ;HL++ next byte address in Memory

mov a,d ;current maxcmp m ;compare current max with Mjnc skip1 ;if D>=M. no change to Dmov d,m ;D<M, so D=M. a new high

skip1:mov a,e ;current mincmp m ;compare current min with Mjc skip2 ;if E<M. no change to Emov e,m ;E>=M, so E=M. a new low

skip2:dcr c ;started at N.jnz loop ;until ==0

mov a,dsta 3000mov a,esta 3001hlt

------Sum i to N integers -------;sum of first N integers;looping from 1 to N; for i=1 to N

; sum += i

;N in B

Page 3: 8085_EXAMBLES

8/7/2019 8085_EXAMBLES

http://slidepdf.com/reader/full/8085exambles 3/4

;i in C;sum in D

lda 0 ;get Nmov b,a ;B is Nmvi c,0 ;C is i=0mvi d,0 ;D is sum=0

loop: inr c ;i++mov a,d ;sum to Aadd c ;A=sum(A) + imov d,a ;new sum back to Dmov a,c ;i to Acmp b ;A-B ie. i-Njnz loop ;goto loop if i!=N

mov a,d ;sum to Asta 1hlt

;avoid memory accesses: slower than accessing registers

;8085 limitation of result to A forces the excessive moves

------LOOP X TIMES----;loop_x_times.85

lda 0 ;load xLoop: dcr a ;A--

jnz loop ;goto Loop if x!=0hlt

----Swap hex digits ------;swap hex digits in a byte

LDA 0 ;get valueMOV C,A ;copy to CANI F0h ;mask off low nibbleRRC ;right shift to low nibbleRRCRRCRRCMOV B,A ;to BMOV A,C ;original value back from CRLC ;shift left 4 placesRLCRLCRLCORA B ;OR with firstSTA 1HLT

------Sum M to N -------;sum of integers from M to N, inclusive

;looping M-N+1 from M thru N; for i=M to N; sum += i

Page 4: 8085_EXAMBLES

8/7/2019 8085_EXAMBLES

http://slidepdf.com/reader/full/8085exambles 4/4

;N in B;i in C, starts with M;sum in D, starts with M

lda 0 ;get Mmov c,a ;C is i=M

mov d,a ;D is sum=Mlda 1 ;get Nmov b,a ;B is N

loop: inr c ;i++mov a,d ;sum to Aadd c ;A=sum(A) + imov d,a ;new sum back to Dmov a,c ;i to Acmp b ;A-B ie. i-Njnz loop ;goto loop if i!=N

mov a,d ;sum to Asta 2hlt