Addressing Modes, Branches and Loops
- 6502 addressing modes
- 6800 addressing modes
- 8085 addressing modes
- x86 addressing modes
- unconditional branches
- conditional branches
- compare and test
- test and set
- 6502 example
- 6800 example
- 8085 example
- x86 example
6502 Addressing Modes
- relative addressing only available for branches
- indirect addressing only available for JMP instruction: JMP has
absolute and indirect modes
- in-class exercise: what is the indirect jump useful for?
More 6502 Addressing Modes
- zero page, X (Y): 8-bit offset added to 8-bit X (Y) register,
truncated to 8 bits
- absolute, X (Y): 16-bit offset added to 8-bit X (Y) register
- indirect indexed LDA (aa), Y: aa and aa+1
contain a 16-bit pointer which is added to Y to give the effective
address
- indexed indirect LDA (aa,X): aa+X and aa+X+1
contain a 16-bit pointer to the effective address
- in-class exercise: find a use for indirect indexed and a use
for indexed indirect
6800 and 8085 Addressing Modes
- 6800 relative addressing: jumps and subroutine calls
- 6800 indexed addressing LDAA aa, X: the effective address
is the 16-bit sum aa + X (index registers on 6800 are 16 bits)
- 8085 register indirect: 16-bit register pair, usually HL, holds
the effective address
x86 Addressing Modes
- register indirect addressing MOV AL, [BX]: registers
BP (uses stack segment), BX, SI, DI (data segment)
EA is the contents of the register (plus segment)
- register relative addressing MOV AL, [BX + 89AB]:
EA is the 16- (32-) bit sum of the contents of BX
and the constant offset (plus segment)
- program relative addressing: JMP 333 at location 300,
when compiled to machine code actually encodes the offset, 31
- program indirect addressing: JMP [BX+100] jumps to
the address found at memory location pointed to by (BX + 100)
- base plus index addressing: MOV AX, [BX+DI]
- base relative plus index addressing: MOV AX, [BX+SI+10]
Unconditional Branches
- generally, jumps are absolute and branches are relative
- some architectures have a "skip" instruction which
(conditionally) skips over the next instruction (often but not
always a jump). For example, the PIC processors in Project 8 has skip
instructions.
- unconditional branches are used:
- to skip over else clause,
- at end of while statements, and
- (depending on the language) at the end of for statements
Conditional Branches
- jump if any one of the flag bits is (or is not) set
- different mnemonics: BEQ (Z flag is set), JNLE/JG (sign flag is clear),
- used for if, while, do, for statements
- different kinds of for statements:
- C and Java: for is a specialized while statement
- Pascal and others: count up or down a given number of times (but
what if you change the loop variable within the loop body?)
- for specific count, usually easiest to count down to zero
Compare, Test and Set
- Compare: like subtract, but only changes the flags not the registers
- Test and Set: load a value from memory, "at the same time" setting
it or one of its bits
- after the updated value is back in memory, we can check
whether it was already set
- variants: load and increment, load and decrement
- useful for synchronization, as long as the operation is atomic
- processor does not release the memory bus during the operation
6502 Example
What does this program do? (5 minutes, groups of 2-4)
start: LDX #00
LDA #00
loop: INX
CMP $03A0, X
BNE loop
done: DEX
STX $03A0
BRK
Write your answer on a slip of paper, then pass it to the instructor at the
end of the exercise.
6800 Example
What does this program do? (5 minutes, groups of 2-4)
start: LDX #0000
STX $0180
LDX #0064
STX $0182
loop: LDX $0180
LDAA $01A0,X
INX
STX $0180
LDX $0182
STAA $029F,X
DEX
STX $0182
BNE loop
done: WAI
8080/8085 Example
What does this program do? (5 minutes, groups of 2-3)
start: LXI SP, 2000H
MVI C, 14H
LXI HL, 1234H
loop1: MOV A, M
PUSH PSW
INX H
DCR C
JNZ loop1
MVI C, 14H
LXI HL, 1234H
loop2: POP PSW
MOV M, A
INX H
DCR C
JNZ loop2
x86 Example
What does this program do? (5 minutes, groups of 2-3)
start: CLD
MOV SI, A000H
MOV DI, B000H
MOV CX, 0010H
REP LODSB