All About Circuits Forum  

Go Back   All About Circuits Forum > Software, Microcomputing, and Communications Forums > Programmer's Corner

Notices

Programmer's Corner Discussion forum for all aspects of programming and software engineering. Any software programming language welcome: C, C++, C#, Fortran, Java, Matlab, etc.

Reply   Post New Thread
 
Thread Tools Display Modes
  #1  
Old 02-18-2012, 07:57 PM
justtrying's Avatar
justtrying justtrying is offline
Senior Member
 
Join Date: Mar 2011
Location: ring of fire, GMT -7
Posts: 320
Send a message via Skype™ to justtrying
Default assembly - summing positive numbers

I am trying to write a subroutine that will find an average of positive numbers in an array. I am stuck on adding the numbers. The operations only seem to work with A and B registers. In this case I am not sure if I am executing addition properly. This is the best way that I came up with. Also in my branching, when I ran the program, it counted negative numbers as well, I am not sure where the problem is. (HCS12, WINIDE32)

Code:
*          Subroutine: AvgPositive
*          Scope: count average of positive numbers using stack
*          Input parameters: Index reg X points to the array, Index reg Y holds size of array
*          Output parameters: result is returned to reg A
*          Stack is used therefore no registers are modified

AVGPOSITIVE
      
        LDX   #ARRAY
        CLRB
        LDY   #SIZE_ARRAY               ;counter to end of subroutine                           
 
LOOP3
        BRCLR 0,X,80,LOOP4                          ;branch if contents are positive
LOOP4
        LDAB    0,X
        ABA
        INX
        DBNE Y,LOOP3

        RTS

        SWI
Select All
p.s. SWI we supposed to use for reasons to be explained to us later on...

rest of the program:

Code:
PROGRAM_ADD     EQU     $0800
ARRAY_ADD       EQU     $0900
STACK_ADD       EQU     $2000
SIZE_ARRAY      EQU     $4

         ORG     ARRAY_ADD
COUNT_POS    DS    1
AVG_POS   DS  1
ARRAY     DB       $23,$24,$86,$26


        ORG     PROGRAM_ADD
MAIN1
        LDS     #STACK_ADD
        LDAB    #SIZE_ARRAY                             ;counter
PART1
          LDX    #ARRAY
    BSR    COUNTPOSITIVE

PART2
        LDX #ARRAy
        BSR AVGPOSITIVE
Select All
__________________
Consistency is the last refuge of the unimaginative
Reply With Quote
  #2  
Old 02-18-2012, 11:03 PM
MrChips's Avatar
MrChips MrChips is offline
Super Moderator
 
Join Date: Oct 2009
Posts: 8,355
Blog Entries: 23
Default

Firstly, I will point out where you have errors.
In another post I will offer some solutions.

Code:
LOOP3
        BRCLR 0,X,80,LOOP4                          ;branch if contents are positive
LOOP4
Select All
You have a number of problems here. 80 is a decimal value. To test the MSB (bit-7) you meant to use the mask $80.

Branching to LOOP4 is redundant. The next statement to be executed will be at LOOP4 if the branch is not taken, i.e. the logic flow is not correct. I will show you how to correct this later.

You are still using RTS and SWI incorrectly.
__________________
Most computer problems can be attributed to a simple problem - a loosewire behind the keyboard.

Reply With Quote
  #3  
Old 02-18-2012, 11:29 PM
justtrying's Avatar
justtrying justtrying is offline
Senior Member
 
Join Date: Mar 2011
Location: ring of fire, GMT -7
Posts: 320
Send a message via Skype™ to justtrying
Default

yes, I see what you mean about the mask. I think I know what you mean about SWI, it should be at the end of main, before the subroutines.

I realize the problem with the loop, but I haven't been able to figure out how to correct it. I obviously want the program to go into the next loop only if the number it sees is positive to add them, otherwise I want it to go back to test the next number. Originally I had a next line in loop 3 testing for negative number and branching back to itself if true, but you already know that the number is negative from the other test, so I don't really know how to do.
__________________
Consistency is the last refuge of the unimaginative
Reply With Quote
  #4  
Old 02-18-2012, 11:45 PM
MrChips's Avatar
MrChips MrChips is offline
Super Moderator
 
Join Date: Oct 2009
Posts: 8,355
Blog Entries: 23
Default

I am not familiar with the HC12 instruction set but it has many enhanced instructions and shortcuts that help to simplify the programming.

Firstly, you have to think about overflows. If you are summing 8-bit positive values, you will need a 16-bit accumulator. You will be limited to summing a total of 256 values.
So right off the bat you will need the 16-bit D register (A:B).

Here is one solution:

Code:
SUMPOSITIVE:

    LDX #ARRAY
    LDY #SIZE_ARRAY
    CLRA                      ; clear D register
    CLRB

LOOP:
    BRSET 0,X, $80, NEXT       ; skip if negative
    ADDD 0,X                   ; accumulate sum

NEXT:
    INX
    DBNE Y, LOOP   ; repeat

    SWI            ;return to debug
Select All
__________________
Most computer problems can be attributed to a simple problem - a loosewire behind the keyboard.

Reply With Quote
  #5  
Old 02-18-2012, 11:53 PM
justtrying's Avatar
justtrying justtrying is offline
Senior Member
 
Join Date: Mar 2011
Location: ring of fire, GMT -7
Posts: 320
Send a message via Skype™ to justtrying
Default

Thank you, I had something similar yesterday in the lab, but it wasn't working because I was using A and B as counters, which of course is D. I realized today that I can use other registers as counters as well... But I couldn't see that putting ADDD inside the first loop would make such a big difference.

Greatly appreciate your patience
__________________
Consistency is the last refuge of the unimaginative
Reply With Quote
Reply   Post New Thread

Tags
, , ,


Similar Threads
Thread Thread Starter Forum Replies Last Post
positive output terminal on full wave rectifier EL7819 Homework Help 8 11-16-2011 01:41 AM
MPLAB Error with Assembly ELECTRONERD Embedded Systems and Microcontrollers 21 01-18-2011 10:33 PM
Complex Numbers 6: Dividing Complex Numbers Nirvana Math 2 01-14-2008 09:58 AM
Detection of "Overflow" of negative numbers e.g. -2(1 10) , -4 (1 100) etc. lemuel rapsuk Math 0 09-01-2006 10:35 AM

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 06:06 AM.


User-posted content, unless source quoted, is licensed under a Creative Commons Public Domain License.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.