Calculating an Average in HCS08 Assembly

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Calculating an Average in HCS08 Assembly

Jump to solution
1,647 Views
rsheldon
Contributor II

Hello,

 

I am wondering the simplest and best way is to calculate an average of 3 8-bit numbers using HCS08 Assembly code.  I'm getting stuck on what happens when adding the first two numbers results in an overflow and how to proceed on getting the final sum before dividing by 3 (which I was planning on doing using the DIV instruction, but that depends on your suggestions).

 

Thanks,

 

RTS

Labels (1)
Tags (1)
0 Kudos
1 Solution
405 Views
peg
Senior Contributor IV

Hello rsheldon,

 

Here is one way to find the average of 3 8-bit numbers:

 

 

  CLRA  PSHA   ;zero a place on the stack for high byte of addition result  LDA value1  ADD value2         ;add first two values  BCC XXX1  INC 1,SP  ;store possible rolloverXXX1  ADD value3   ;add third value  BCC XXX2  INC 1,SP  ;add possible rollover to high byteXXX2  PULH   ;low byte of result already in A, put high byte in H  LDX #3  ;set divisor  DIV   ;do the division     ;the result is in A with any remainder in H

 

 

View solution in original post

0 Kudos
2 Replies
406 Views
peg
Senior Contributor IV

Hello rsheldon,

 

Here is one way to find the average of 3 8-bit numbers:

 

 

  CLRA  PSHA   ;zero a place on the stack for high byte of addition result  LDA value1  ADD value2         ;add first two values  BCC XXX1  INC 1,SP  ;store possible rolloverXXX1  ADD value3   ;add third value  BCC XXX2  INC 1,SP  ;add possible rollover to high byteXXX2  PULH   ;low byte of result already in A, put high byte in H  LDX #3  ;set divisor  DIV   ;do the division     ;the result is in A with any remainder in H

 

 

0 Kudos
405 Views
rsheldon
Contributor II

Thanks! This works perfectly

0 Kudos