Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
05/14/03 10:10
Read: times


 
#45408 - find the average of 12 numbers-best way?
Hi I have written a program that finds the average of 12 numbers, and it seams to work fine. But, I can't help thinking that it is not doing it in the most efficient way.

You guys seam to be the experts. Can you have a glance at what I've done and tell me if it can be done in a more effectient way (only using the methods contained within mine).

Thank you so much if you can take the time to have a look see. Cheers M@)
--------------------------------------------


smallest equ 6Ch ; store smallest at mem loc 73h
smallest_stage equ 6Dh ; store smallest_stage at mem loc 74h

org 100h

mov R0, #60h ; point the contents of 70h to R0
mov R1, #61h ; point the contents of 71h to R1
mov R2, #0Ch ; set the value of R2 to be 12

mov 60h, #07 ; set the value of 60h to be 7
mov 61h, #05 ; set the value of 61h to be 5
mov 62h, #04 ; set the value of 62h to be 4
mov 63h, #08 ; set the value of 63h to be 8
mov 64h, #05 ; set the value of 64h to be 5
mov 65h, #02 ; set the value of 65h to be 2
mov 66h, #04 ; set the value of 66h to be 4
mov 67h, #08 ; set the value of 67h to be 8
mov 68h, #04 ; set the value of 68h to be 4
mov 69h, #08 ; set the value of 69h to be 8
mov 6Ah, #03 ; set the value of 6Ah to be 3
mov 6Bh, #09 ; set the value of 6Bh to be 9

mov a, @R0 ; move what is in mem loc ass with R0 to the acc
subb a, @R1 ; subtract and carry what is in mem loc ass with R1 from a and carry

jc RO_is_smallest_stage ; if R1>R0 (carry flag set) go to 'RO_is_smallest'
mov a, @R1 ; else move what is in mem loc ass with R0 to the acc
mov smallest_stage, a ; move what is in the acc to
inc R0

RO_is_smallest_stage:
mov a, @R0 ; else move what is in mem loc ass with R0 to the acc
mov smallest_stage, a ; move what is in the acc to
inc R1

loop:
CLR C ; clear carry
acall find_smallest ; call 'find_smallest'
djnz R2, loop ; if(R2!=0) goto loop
sjmp finish ; else goto finish

find_smallest:
mov a, @R0 ; move what is in mem loc ass with R0 to the acc
subb a, @R1 ; subtract and carry what is in mem loc ass with R1 from the acc and carry
jc RO_is_smallest ; if R1>R0 (carry flag set) go to 'RO_is_smallest'
mov a, @R1 ; else move what is ass with R1 to the acc
inc R0 ; as R1 is smaller than R0 increment R0
mov smallest, a ; move what is in the acc to 'smallest'
acall compare_with_smallest_stage ; call 'compare_with_smallest_stage'
ret ; go back to the caller

RO_is_smallest:
inc R1 ; as R0 is smaller than R1 increment R1
mov a, @R0 ; else move what is in mem loc ass with R0 to the acc
mov smallest, a ; move what is in the acc to
acall compare_with_smallest_stage ; call 'compare_with_smallest_stage'
ret ; go back to the caller

compare_with_smallest_stage:
mov a, smallest_stage ; move what is in smallest_stage to the acc
subb a, smallest ; subtract and carry what is smallest from the acc and carry
jc smallest_stage_is_smallest ; if smallest>smallest_stage (carry flag set) go to 'smallest_stage_is_smallest'
mov smallest_stage, smallest ; else move smallest into smallest_stage (the new smallest val)
ret ; go back to the caller

smallest_stage_is_smallest:
ret ; go back to the caller

finish:
mov smallest, smallest_stage ; move the val that is currently the smallest (kept in the stage) to 'smallest'
mov a, smallest ; display the smallest val in the acc as well

end


List of 9 messages in thread
TopicAuthorDate
find the average of 12 numbers-best way?            01/01/70 00:00      
   RE: find the average of 12 numbers-best            01/01/70 00:00      
   RE: find the average of 12 numbers-best way?            01/01/70 00:00      
      RE: find the average of 12 numbers-best way?            01/01/70 00:00      
   RE: find the average of 12 numbers-best way?            01/01/70 00:00      
   RE: find the average of 12 numbers-best way?            01/01/70 00:00      
      RE: find the average of 12 numbers-best way?            01/01/70 00:00      
   RE: find the average of 12 numbers-best way?            01/01/70 00:00      
      RE: find the average of 12 numbers-best way?            01/01/70 00:00      

Back to Subject List