Calculating an Average in HCS08 Assembly

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Calculating an Average in HCS08 Assembly

跳至解决方案
2,296 次查看
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

标签 (1)
标记 (1)
0 项奖励
回复
1 解答
1,054 次查看
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 项奖励
回复
2 回复数
1,055 次查看
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 项奖励
回复
1,054 次查看
rsheldon
Contributor II

Thanks! This works perfectly

0 项奖励
回复