6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Wed May 08, 2024 3:05 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Programming
PostPosted: Wed Jun 25, 2003 12:26 am 
Offline

Joined: Wed Jun 25, 2003 12:17 am
Posts: 2
I need a little help with some very basic programming, I have to write an assembly program which inputs 2-digit decimal numbers and computes their sum. If the sum is greater or equal to 100 (decimal) the output is 0: if the sum is greater than 50 but less than 100 the output is 1. for all other sum cases the output is neg 1. :arrow: :?: Please help.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Jun 26, 2003 7:41 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8432
Location: Southern California
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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: