Classic Computer Magazine Archive COMPUTE! ISSUE 51 / AUGUST 1984 / PAGE 10

Machine Language Decimal Mode

Can anyone explain the SED command? I used the SYS command to go to a machine language subroutine with the intent of returning to BASIC. The program ran fine until it hit the last statement—which was the RTS—and then it crashed and displayed an OVERFLOW ERROR message.

The machine language subroutine contained several JSR commands, and each one was covered with the RTS. The SED command was used before an addition. When the SED was deleted, everything worked fine. What did (and does) SED do?

E. H. Giles

When programming in machine language, SED stands for set decimal mode. This command sets the decimal flag on the status register and tells the 6502 chip that all addition and subtraction is to be done in "decimal mode" (as opposed to "binary"). In this mode, the carry flag is set when addition exceeds 99.

Setting the processor to the decimal mode has its drawbacks. For example, all additions and subtractions are then done in decimal, but the INC (increment) command still uses the binary mode.

Failing to clear the decimal flag (CLD) before returning to BASIC could cause catastrophic results. This is the reason your computer is freezing up when you exit your machine language subroutine. Try using the CLD (clear decimal) after the addition and before the RTS.

You always need a CLC before an addition, but SED is useful only in highly specialized applications. See Jim Butterfield's column "Machine Language" elsewhere in this issue for more on this topic.