Assembly question, classwork - Page 1
Forum section: electronics / general

author: prophoss
author_rank: Contributor
timestamp: November 17, 2020, 05:54:06 pm
content: 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 EntryABSENTRY EntryRAMStart EQU $2000ROMStart EQU $8000ORG RAMStartList1 DC.B $FF, $01, $FE, $02List2 DC.B $4ORG ROMStartEntry:CLRACLRBLDX #List1LDY #List2There ADDB 0,XBCSADDA #1INXDECYBNE ThereCodeEnds 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.

author: Peabody
author_rank: Super Contributor
timestamp: November 17, 2020, 07:10:38 pm
content: 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.

author: prophoss
author_rank: Contributor
timestamp: November 17, 2020, 09:14:10 pm
content: 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

author: Peabody
author_rank: Super Contributor
timestamp: November 17, 2020, 10:31:48 pm
content: 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,XADCA #0INX- etc -You add a zero to A each time, which does nothing, but if the carry flag is set, it will add that too.

author: prophoss
author_rank: Contributor
timestamp: November 24, 2020, 05:59:56 pm
content: 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.