minerscale Posted February 13, 2017 Share Posted February 13, 2017 Hi guys, I'm just starting 2600 after making a game for the genesis in C. I'm quite enjoying the ridiculous challenge of doing it and i'm interested in the properties of adc and the carry flag. Why does adc add one more to the accumulator when the carry flag is set? It seems quite random that it would be doing that. It's sort of useful but a pain in the ass at the same time. I end up having to call clc wasting 2 cycles in the process. I'm very confused as to why. So any insight into that would be great. Quote Link to comment Share on other sites More sharing options...
cd-w Posted February 13, 2017 Share Posted February 13, 2017 (edited) If you want to add numbers that are longer than 8-bits it is very useful. A number larger than 8-bits is represented using multiple 8-bit values. To add them together, you clear the carry and start adding them together from the lowest pair. If the addition overflows, the carry-out becomes the carry-in for the next addition. The code below shows how to add two 16-bit numbers. Notice that there is no CLC after the first addition as the carry flag needs to be preserved: add16: clc ; clear carry lda num1lo adc num2lo ; add low numbers sta reslo ; store low sum lda num1hi adc num2hi ; add high numbers with carry sta reshi ; store high sum Edited February 13, 2017 by cd-w 1 Quote Link to comment Share on other sites More sharing options...
cd-w Posted February 13, 2017 Share Posted February 13, 2017 When writing 6502 code, you can often optimize out the CLC instructions as you can infer that the carry will not be set at that point in the code, e.g. you have already performed an addition that cannot overflow. Chris Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.