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
Solved! Go to Solution.
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
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
Thanks! This works perfectly