Vous êtes sur la page 1sur 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
MVI B,8 ;loop 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
dcr b ;i--
jnz NextI ;goto Loop if i>0
sta 1
hlt

------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 2000
mov c,a
lxi h,0 ;HL=0
mov d,m ;D=E=M[0]. first datum
mov e,m
dcr c ;N-1
;loop N-1 times
loop:
inx h ;HL++ next byte address in Memory
mov a,d ;current max
cmp m ;compare current max with M
jnc skip1 ;if D>=M. no change to D
mov d,m ;D<M, so D=M. a new high
skip1:
mov a,e ;current min
cmp m ;compare current min with M
jc skip2 ;if E<M. no change to E
mov e,m ;E>=M, so E=M. a new low
skip2:
dcr c ;started at N.
jnz loop ;until ==0
mov a,d
sta 3000
mov a,e
sta 3001
hlt

------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
;i in C
;sum in D
lda 0 ;get N
mov b,a ;B is N
mvi c,0 ;C is i=0
mvi d,0 ;D is sum=0
loop: inr c ;i++
mov a,d ;sum to A
add c ;A=sum(A) + i
mov d,a ;new sum back to D
mov a,c ;i to A
cmp b ;A-B ie. i-N
jnz loop ;goto loop if i!=N
mov a,d ;sum to A
sta 1
hlt
;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 x
Loop: dcr a ;A--
jnz loop ;goto Loop if x!=0
hlt

----Swap hex digits ------


;swap hex digits in a byte
LDA 0 ;get value
MOV C,A ;copy to C
ANI F0h ;mask off low nibble
RRC ;right shift to low nibble
RRC
RRC
RRC
MOV B,A ;to B
MOV A,C ;original value back from C
RLC ;shift left 4 places
RLC
RLC
RLC
ORA B ;OR with first
STA 1
HLT

------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
;N in B
;i in C, starts with M
;sum in D, starts with M
lda 0 ;get M
mov c,a ;C is i=M
mov d,a ;D is sum=M
lda 1 ;get N
mov b,a ;B is N
loop: inr c ;i++
mov a,d ;sum to A
add c ;A=sum(A) + i
mov d,a ;new sum back to D
mov a,c ;i to A
cmp b ;A-B ie. i-N
jnz loop ;goto loop if i!=N
mov a,d ;sum to A
sta 2
hlt

Vous aimerez peut-être aussi