We don't want to just do students' homework for them, but I'll try to give a little more useful, possibly unexpected information, along with some tips.
The inputting routine will depend on the hardware (what kind of keyboard or whatever, and how it is interfaced) and any system software already in place. In most cases, input numbers are converted to hexadecimal and worked with in hex and not converted back to decimal until it is time to output them. The 6502 does afford a way to efficiently do decimal arithmetic though, with decimal mode (D flag set). No second "decimal adjust" operation is needed as it is on most other processors. One possible reason to keep numbers in decimal is that conversion of fractions in one base (hex or decimal) to fractions in the other may result in approximation errors.
The standard byte-addtion form will look like:
Code:
SED ; Wow-- I haven't used that one in over a decade!
CLC ; No previous addition's carry should affect this one.
LDA variable_1
ADC variable_2
(Darn-- it wrapped around. It didn't do that in the "Preview" before posting!)
This will do the decimal-mode addition and leave the sum in the accumulator. Now if you're working with a CMOS 6502 (65c02) instead of an NMOS one, the flags will be correct after the operation. Unfortunately the NMOS '02 had a bug here, so you would have to follow that with
Code:
CMP #0
to get the negative and zero flags fixed. It won't help for the overflow flag. I should mention that the NMOS did do the carry flag right in decimal operations.
From there, it's a simple comparison matter. Since you'll probably need the decimal flag cleared for subsequent operations anyway, it might be good to clear it now. It won't affect these operations.
Code:
CLD
BCS carry ; If C flag got set, branch down to output the 0.
CMP #$51 ; Compare A to the bottom of the 51-99 range.
BCC LEQ50 ; If C is left clear, go down to output -1 to mean result<=50.
LDA #1 ; By default, arriving here means 50<result<100 above,
RTS ; so put 1 in A and exit.
LEQ50: LDA #$99 ; $99 in A would represent -1 in decimal.
RTS ; If you want it in hex, use LDA #$FF.
carry: LDA #0
RTS
CMP (compare accumulator) above leaves the original value intact for subsequent comparisons or other operations and does not keep the result of the comparison. Since all it does is set or clear flags based on the result of an unsigned comparison, it doesn't matter if we're still in decimal mode or not. In fact, it does not pay any attention to the D flag. It doesn't need to, since the magnitude relationships are maintained regardless of base. For example, 95d>79d just as 95H>79H. (Remember-- unsigned.) Be aware though that the N flag result reflects only the high bit of the resulting subtraction made, which is why we can't use it to automatically assume that N=1 means the result was in the range of 50-99 (let alone 51-99). CMP, although it does a subtraction, does not care what the initial C flag value was, so you don't have to do SEC first.
I don't think I've ever had to do decimal arithmetic on the NMOS 6502 (just the CMOS, and that was in the late 80's); so if I missed something and someone else wants to jump in, please do.
Garth