User Tools

Site Tools


sd-8516_assembly_language_part_ii

This is an old revision of the document!


SD-8516 Assembly Language Part II

Introduction to Part II

In part 1 we learned some basics about the ISA (instruction set architecture) and the architecture of the CPU. In part II we will wrap up the most important opcodes, but we will also lean into how they are used to get things done.

Lesson 8 : Special Flags

  • Lesson 8: “Special Flags”
  • Time: 10 min
  • Learn: All Available Flags

In the previous lesson on flags you learned about the Z, N, C and V flags. These are used by the CPU to indicate the status of various operations. For example, the zero flag is used to indicate the last operation produced a zero. Therefore if you are looking for the zero at the end of a string,

      LDC #0    ; zero C (string starts at length 0)
      
  strlen_loop:
      LDAL [ELM]
      JZ @strlen_end
      INC C                ; we found a non-zero character in the string.
      JMP @strlen_loop
      
  strlen_end:
      RET                  ; C now contains the COUNT of all non-zero characters in a string

…you will notice that the JZ works with LOAD instructions (here, LDAL loads one byte). ; if the byte retreived is a zero, it will set the zero flag. You do not need to CMP AL, 0 – it's automatic.

However, there are other flags; The first four user-facing flags are E, F, B and U. You can set these flags and unset them in the same way as Z N C V – ex. setting ZNCV is done with SEZ, SEN, SEC and SEV; unsetting them is done with CLZ, CLN, CLC and CLV. The E F B U flags are set and unset with:

  • SEE and CLE for the E (extended, or 'extra') flag.
  • SEF and SEB for the F flag (or 'flag' flag).
  • SEB and SEU, CLB and CLU for the B (bonus) and U (user) flags.

On a technical level the E flag is reserved as it is used to deal with BCD; but since we deprecated BCD instructions it is currently an unused flag. In any case, the F, B and U flags are never set by the CPU and may be used by user functions. A common use is to return a 1 bit status; 0 for no error and set (1) for error. Since these flags are never set by the CPU they are easy to control. Using the Z or C flags is dangerous since some instructions may corrupt those flags.

Your programs can also use them as 1 bit status variables.

next, the D flag, or debug flag. When set, it will dump instruction data to the javascript console. This significantly slows down the machine; in fact just having the instructions inline slows down the machine so debug is often removed and ignored in a production or release distribution of the SD-8516. Therefore, for all intents and purposes, you can use SED and CLD as a user flag, just be aware it does affect performance in debug releases.

The I flag (interrupt enable) prevents INT from being called, and is reserved for system use. Not sure what I want to do with it.

The S flag is almost useless; it was intended to turn off a memory trap in the sound system; I found it to be completely useless, maybe a 2% speedup or penalty. it is essentially a user facing flag.

The only flags that you cannot access are the TR (trace), BR (breakpoint) and PR (protected mode) flags. They are so named after the first two letters of their name; but interestingly enough you might as well consider the R to mean restricted. You can't usually set these flags. They are reserved for system use.

  // Arithmetic & User Flags (low byte 0-7)
  Z = 0,   // Zero
  N = 1,   // Negative
  C = 2,   // Carry
  V = 3,   // Overflow
  E = 4,   // Extended carry -- not used/reserved
  F = 5,   // Fast Flags mode. When on, flags are not implicitly checked.
  B = 6,   // BCD/"Bonus" flag. Have fun!
  U = 7,   // User flag. For users to use.
  // Control & Operation Flags (high byte 8-15)
  D = 8,   // Debug mode
  TR = 9,   // Trace mode
  BR = 10,  // Breakpoint mode
  ER = 11,  // Error/Exception (i.e. return code 0 = ok, 1 = error) 'SER' -- set err
  PR = 12,  // Protected mode
  I = 13,  // Interrupt enable
  S = 14   // Sound auto-updates

The key of this lesson is merely to be aware of the flags and the instructions used to set and unset them. In general, they follow the pattern of SEZ and CLZ;; SE(T) and CL(EAR) with the flag letter replacing the parentheses.

Testing Flags

Oh, there's one more thing. If you use flags like F, B or U you may notice there is no JF or JNF (jump if F set and jump if F not set). That's because we don't want to add 50 different opcodes to deal with all the flags. What you can do is this:

      ; Some operation that sets the F flag
      TESTF 0x20
      JZ              ; Jump if F is set
      JNZ             ; Jump if F is not set

TESTF works by setting the Z flag if all the bits set in the parameter are also set in the FLAGS register. if you give it a byte it only tests against the bottom 8 bits.

Here's a chart of the bit values for each flag:

  Z = 0x0001 as u16,   // Bit 0
  N = 0x0002 as u16,   // Bit 1
  C = 0x0004 as u16,   // Bit 2
  V = 0x0008 as u16,   // Bit 3
  E = 0x0010 as u16,   // Bit 4 (was X - Extended carry) -- SEE and CLE can be used as a user-flag (is never set by an opcode)
  F = 0x0020 as u16,   // Bit 5 (Fast/deprecated) -- SEF and CLF can be used as a user-flag (is never set by an opcode)
  B = 0x0040 as u16,   // Bit 6 (Bonus/BCD) -- SEB and CLB can be used as a user-flag (is never set by an opcode)
  U = 0x0080 as u16,   // Bit 7 (User flag) -- SEU and CLU can be used as a user-flag (is never set by an opcode)
  D = 0x0100 as u16,   // Bit 8 (Debug)
  TR = 0x0200 as u16,  // Bit 9 (Trace)
  BR = 0x0400 as u16,  // Bit 10 (Breakpoint)
  ER = 0x0800 as u16,  // Bit 11 (Error/Exception)
  PR = 0x1000 as u16,  // Bit 12 (Protected Mode)
  I = 0x2000 as u16,   // Bit 13 (Interrupt)
  S = 0x4000 as u16    // Bit 14 (Sound)
sd-8516_assembly_language_part_ii.1771031766.txt.gz · Last modified: by appledog

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki