random wrote:
How big can A be?
8-bits. That's all. No more, no less.
Quote:
For instance if I wanted to perform larger (> 8-bits) calculations.
This is what the C flag (carry flag) in the processor status register is for. for example, if you want to add 2 16-bit numbers together, you'd add the low 8-bits of each together first, and then if the result was >= $100, the C flag will be set, which will allow you to add it into the addition of the high 8-bits of each number.
That explaination wasn't bery good. Remeber that if C is set at the time of ADC, an extra 1 will be added to the sum:
Code:
CLC ; clear carry initially
; add low bytes together
LDA valueA_low
ADC valueB_low
STA sum_low ; store the sum -- note that if the sum was > $FF, C is set
LDA valueA_hi
ADC valueB_hi
STA sum_hi
Consider you have the following 2 16-bit numbers that you want to add:
$0362
$04F3
first you clear C (CLC) so the extra 1 won't be in your addition... then you add the low bytes of the numbers together:
$62 + $F3 = $55 <-- note that the sum is $55 and not $155 (because it can only be 8-bits). However because it was greater than $FF, C is now set.
So now you add the high bytes together: $03 + $04 = $08 <--- note that it's $08 because C is set by the previous addition, so the extra 1 got added.
therefore... $0362 + $04F3 = $0855.
C works in a similar fashion for multi-byte ASL/ROL/LSR/ROR commands. And works "backwards" for SBC, but the same logic applies.[/quote]
Quote:
I'm kind of hazy on how 6502 handles signed and unsigned numbers, explanation would be helpful. I'm getting the impression that it only has signed ones.
Well, it doesn't handle them at all really. In fact, if anything I'd say the numbers are all unsigned.
What really makes a number signed or unsigned is how your program treats it. The processor doesn't care one way or the other. $FF can be either 255 or -1.... the processor will always treat it the same regardless -- what matters is how your program works with the number.
There is a "N" flag in the processor status reg, which will be set if the result of the last operation was negative and cleared if the result was positive. All "negative" really means here is "the high bit is set". For this purpose, $00-7F are all "positive" numbers, and $80-FF are all "negative". However, again, the processor doesn't treat them any differently if they're positive or negative.... it kind of treats them all like they're positive ($80 + $80 will give you 256, not -256)
Quote:
How do you end a branch?
Branches behave just like jumps, only they will only jump under certain conditions. The condition is the current status of a status flag. Usually you will work with BNE, BEQ, BCC, and BCS.
BNE will jump if and only if the Z status flag is clear
BEQ will jump if and only if the Z status flag is set
BCC will jump if C is clear
BCS will jump is C is set
HOW these flags gets set is determined by the instruction(s) before the branch.
Therefore in your examples:
Quote:
lda some_variable
cmp some_other_variable
beq exe_this_code
that CMP instruction is comparing two values. CMP will set Z if the values equal each other, and will clear Z if the values do not equal each other. If you want to get into details, CMP actually performs a subtraction, and Z is set to the result of that subtraction -- and if the two numbers equal each other, the result is Zero, so Z is set. If the result is nonzero (the numbers don't equal each other), Z will be clear.
Code:
LDA #$03 ; Z=clear
CMP #$03 ; Z=set (3-3 = 0 -- a result of 0 = Z set)
BEQ somewhere ; jump to 'somewhere' only if Z is set (which it was set by last instruction)
LDA #$00 ; value of $00 = Z set
BNE somewhere ; this will not jump anywhere because Z is set by above LDA
BEQ somewhere ; this WILL jump because Z is set by previous LDA
Quote:
But what if I want multiple branches?
You can branch as many times as you want, there's no limit. Remember that all the branches look at is the status flags, so to really understand branches, you have to understand how the status flags are set.