EEVblog Electronics Community Forum

Products => Computers => Programming => Topic started by: prophoss on November 17, 2020, 05:54:06 pm

Title: Assembly question, classwork
Post by: prophoss on November 17, 2020, 05:54:06 pm
I am having trouble with creating a program in my class. They want us add an array and store the sum in REG B.
Also they asked us to check the carry bit each iteration and if the carry bit is clear add 1 to REG A. We are using HCS12 family of chips and Code warrior IDE.
Here is what I have so far.
----------------------------------------------------------------------------------------------
XDEF      Entry
              ABSENTRY  Entry

RAMStart      EQU $2000
ROMStart      EQU $8000 


              ORG  RAMStart
List1         DC.B  $FF, $01, $FE, $02
List2         DC.B  $4
             
              ORG ROMStart
             
Entry:
              CLRA
              CLRB
             
              LDX #List1
              LDY #List2
There         ADDB 0,X
             
 
              BCS
              ADDA #1   
             
              INX
              DECY
              BNE       There
             
             
             
             
CodeEnds      BRA     CodeEnds
-----------------------------------------------------------------------------------------------------------
I tried several different things but it keeps throwing a fault at BCS which is supposed to check the carry bit. Any help would be appreciated.
Title: Re: Assembly question, classwork
Post by: Peabody on November 17, 2020, 07:10:38 pm
BCS is a conditional branch instruction - branch if carry set.  So you need to add where you want it to branch to if the carry flag is set.  I think you need a label for the code at INX - maybe Carry.  Then you would say BCS Carry.  It works just like the BNE There at the bottom.

You should also check whether labels need to end with a colon.  Entry has one, but There does not.

You say you're supposed to increment A when the carry flag is clear.  Did you perhaps get that backward?  Normally you would increment the high byte of the sum if the carry is set after an addition, which indicates that the result has overflowed and wrapped around.  It just seems you want to end up with B = 0 and A = 2 since the sum of the four bytes is actually $200.


Title: Re: Assembly question, classwork
Post by: prophoss on November 17, 2020, 09:14:10 pm
Actually you are correct I do want it to check if the carry is set and then add 1 to REG A.

Ok I think I see what you are talking about now. REG A and B are both 8 bits so to get $200 you will need to overflow into REG A. Now how to do that is what I am trying to figure out. Thanks @peabody
Title: Re: Assembly question, classwork
Post by: Peabody on November 17, 2020, 10:31:48 pm
Well then change the BCS to BCC, with the branch location being the INX instruction.  So if the carry flag is clear, you skip over the increment to B.  But if there was an overflow and the carry flag is set, then the BCC condition will fail, and the branch will not take place, so the increment will be executed.

There is another way to do this, which is to use the ADC instruction - add with carry - and not use the conditional branch.  So it would read:

There:
              ADDB 0,X
              ADCA #0
 
              INX
              - etc -

You add a zero to A each time, which does nothing, but if the carry flag is set, it will add that too.
Title: Re: Assembly question, classwork
Post by: prophoss on November 24, 2020, 05:59:56 pm
I did finally fix my code, thanks to your help. I realized what I was supposed to do had to and got it to check the overflow bit and add 1 each time it overflowed, which was twice, for a total of $200 just like it was supposed to! Thanks Peabody.