Flags and Arithmetic
- Addition: carry, half-carry, overflow, sign, zero, parity flags
- Subtraction: carry, half-carry, overflow (underflow), sign,
zero, parity flags
- other flags: interrupt
- Multiplication: 6809, x86
Flags
- multi-precision arithmetic
- controlling loops (for, while, do)
- controlling branches (if...)
- controlling subroutines (callcc)
- controlling the processor (break, interrupt flags)
Carry Flags
- carry: holds the carry out for 8-bit or 16-bit addition, subtraction
- half carry (6502: D flag): holds the carry out for the 4-bit portion of
8-bit addition, subtraction
- ADC adds carry bit into 8- or 16-bit sum
- SBB subtracts carry (borrow) bit from 8- or 16-bit subtraction
- 6502 carry flag is 1 for no borrow, 0 for borrow (inverted)
- half carry is used to adjust result of sum when adding/subtracting
BCD numbers
- 6502 D flag specifies that arithmetic is in Decimal (BCD) mode
Overflow Flag
- carry flag holds the carry out for 8-bit or 16-bit {\em
unsigned} addition, subtraction
- signed addition, subtraction require a different flag,
overflow (V)
- the overflow flag is set when the range of the result is
outside -128...+127 (or -32768...+32767)
- how would we design a circuit to do this?
- hint: what is the relationship of: the MSb's of the two operands
and the MSb of the result?
In-Class exercise
- groups of 2-3
- 5 minutes, plus 5 minutes class-wide discussion
- what is the use of the parity flag?
Negative, Parity, Zero Flag
- set or cleared according to the result of an arithmetic operation
- can also use special instructions to set and clear
- the entire flags register can also be:
- copied to the accumulator
- set from the accumulator
- pushed to the stack
- popped from the stack
Interrupt Flag
- when an interrupt comes, the processor:
- completes execution of the current instruction
- saves the state of the processor
- starts executing at a predefined location (which may depend
on the interrupt or interrupt vector)
- sometimes (e.g. while in the interrupt handler, or modifying state
that is shared among threads) we want to be sure no (other) interrupt
handlers can run (non-reentrant code)
- to do this, we set or clear the interrupt flag
- at the end of the critical section, we reset the interrupt flag
- any interrupts that come while interrupts are suspended are either
ignored or queued