Title: ECE 3430
1ECE 3430 Introduction to Microcomputer
SystemsUniversity of Colorado at Colorado Springs
- Lecture 11
- Agenda Today
- More on condition testing and branching
- More Bit-Wise Instructions and Some Others
- The Stack (Push, Pop) and the Stack Pointer
-
2Condition Testing and Branching
- In addition to CMP and TST instructions, you can
use BIT to test - certain bits. The source and destination are
ANDed together - and the N, V, C, and Z flags updated in the
status register. The - Destination is not modified!
- Test bits (accompanies CMP and TST instructions)
- BIT ltsrcgt,ltdstgt
- When JMP is out-of-range, use BR as an
alternative. - Unlimited, unconditional jump
- BR ltdstgt
3More Instructions
- Decrement and Increment- Often used in loops to
adjust counters- These add or subtract 1 or 2
from the destination (effective address) - DEC ltdstgt x x 1 INC ltdstgt x x 1
DECD ltdstgt x x 2 INCD ltdstgt x x 2 - Ex) COUNT EQU 10FLASH EQU 0xF800 ORG FLASH
- mov.b COUNT,R4 initialize loop counter
- LOOP ltloop bodygt
- dec.b R4 loop 10 times
jne LOOP - DONE jmp DONE
- END
4More Instructions
- Arithmetic Shift- used to shift bits- note the
characteristics of the end bits - - preserves sign when shifting rightLEFT
- RLA ltdstgt Note LSb is filled with
0, MSb shift into carry RIGHT - RRA ltdstgt Note MSb is shifted into
itself LSb is shifted into carry
C
N-1 ? 0
?
?
0
N-1? 0
?
?
C
5More Instructions
- Rotate- used to rotate bits- note the
characteristics of the end bits, different from
arithmetic shiftsLEFT - RLC ltdstgt Note full loop, MSb goes to
carry RIGHT - RRC ltdstgt Note full loop, carry goes
to MSb
N-1 ? 0
?
?
C
?
N-1? 0
?
?
C
?
6More Instructions
- Logical Shift- no explicit instructions in
MSP430 to do this - - to go left, use RLA
- - to go right, clear C flag (CLRC) and then use
RRCLEFT - Note LSb is filled with 0, MSb
shift into carry -
- RIGHT
- Note 0 is shifted into MSb
LSb is shifted into carry
C
N-1 ? 0
?
?
0
N-1? 0
?
?
C
0
7More Instructions
- ExampleWhat are the contents of R4 after each
instruction?mov.b 10110110b, R4 R4 Carry - rra.b R4 1101 1011 0 rrc.b R4
0110 1101 1 rrc.b R4 1011 0110
1What CCR bits are altered and how? N Z
V C - RLA
- RLC
- RRA 0 RRC
-
- set to one or cleared to zero depending on
run-time circumstances
8More Instructions
- 1s and 2s compliment
- INV ltdstgt ? 1s compliment
- INV ltdstgt
- ADD 1, ltdstgt ? 2s compliment
- Examplemov.b 11110000b, R4 ? R4 00000000
11110000b - inv.w R4 ? R4 11111111 00001111b
- add.w 1, R4 ? R4 11111111 00010000b
9More Instructions
- Clearing destinationCLR ltdstgt ? zero
out destination - Clearing C, N, Z flags
- CLRC ? BIC 1,SR
- CLRN ? BIC 4,SR
- CLRZ ? BIC 2,SR
- Setting C, N, Z flags
- SETC ? BIS 1,SR
- SETN ? BIS 4,SR
- SETZ ? BIS 2,SR
10More Instructions
- Swap bytes (little to big endian or vice versa)
- -gt destination must be 16-bit
- SWPB ltdstgt
- Sign extend byte to word (8-bit to 16-bit signed
cast) - -gt must be room for extension to 16-bits
- SXT ltdstgt
- No-operation (waste time, delay 1 cycle)
- NOP
11The Stack
- - This is just managed RAM. The stack can live
anywhere in RAM. - - This is a last in, first out (LIFO) data
structure. - - This is a first in, last out (FILO) data
structure. - - Items can only be added or removed from the top
of the stack. - - Conceptually like a stack of cafeteria trays or
a PEZ dispenser. -
- PUSH inserting something onto the TOP of the
STACK - POP removing something from the TOP of the
STACKEx) PUSH 0x0011, 0x0022, 0x0033 POP,
we will receive 0x0033, 0x0022, 0x0011 in that
order
TOP
Last In
This structure is useful because the order of
data is inherently kept. We dontneed to worry
about setting up dedicated memory for each data
item. This is also a necessary structure for
subroutines to work.
First In
BOTTOM
TOP
0x0033
0x0022
0x0011
12The Stack
The size of the arguments pushed to the stack can
vary. In the MSP430, push operations can be 8 or
16-bit. Regardless, a full 16-bit value is
allocated on the stack! In other words, the
stack pointer is always even! All MSP430 pop
operations deallocate 16-bits of data into the
destination.
13The Stack
- What the stack really is- A section of memory
with an address pointer (stack pointer SP in
CPU).- The SP contains the address of the top
element of the stack.- In the MSP430, the SP is
pre-decremented as information is PUSHED.- In
the MSP430, the SP is post-incremented as
information is POPPED.- We define where to
place the stack data structure (using mov
instruction to initialize SP).- The stack is
variable in size and only limited by RAM
availability.- The standard is to place the
STACK at the end of RAM (for our MSP430,
0x0280). - - The MSP430 provides instructions to manipulate
the stack.- How do we initialize the stack?
mov.w 0280h,SP
RAM
Keep global variables close to the beginning of
RAM
0x0200 0x027F
Initialize stack at the end of RAM
0x0280
ltno RAM heregt
14The Stack
- Stack Overflow- If we push too much information
onto the stack, it may start overriding data
stored in pre-defined variables. - - Since there is no operating system running on
our MSP430, so you have to - be the memory manager!
- MSP430 Stack Instructions PUSH (.b or .w)
ltsrcgt ? always adds 16-bits (only touches 8
bits if .b) - POP ltdstgt ? always removes 16-bits
RAM
variables
0x0200 0x027F
Stack creeps backwards through RAM as data is
pushed onto it
15The Stack
- ExampleRemember that the SP points to the top
word on the stack!mov.w 0280h,
SPmov.b 0AAh, R4mov.w 0BBCCh, R5push.b
R4push.w R5pop R4 R4 0xBBCCpop R5
R5 0x??AA NOTE The data in RAM is not
actually erased!
SP
0x0280
xx
0x027E0x027F
SP
AA
??
0x027C 0x027D0x027E0x027F
SP
CC
BB
AA
??
0x027E0x027F
SP
AA
??
SP
0x0280
xx
16The Stack
- Dumb Example Program using the STACKSample port
1 10 times as fast as you can, then sum the
result (in 2 loops)FLASH EQU 0xF800STACK EQU 0x
0280RAM EQU 0x0200COUNT EQU 10
ORG RAMRESULT DS 2 allocate memory in RAM
to hold result ORG FLASH start code at
beginning of FLASH - mov.w STACK,SP initialize stack point
to end of RAM mov.b COUNT,R5 initialize
loop counter - clr.w RESULT initialize
result to zero - SAMPLE mov.b P1IN,R4 sample data on port 1
into R4 push R4 store the sample on the
stack dec R5 decrement loop counter
jne SAMPLE perform this task 10 times
continued ?
17The Stack
- mov.b COUNT,R5 reinitialize the
loop counter - SUM pop R4 bring in the
information off the top of the stack
add.w R4,RESULT continually sum this
with RESULT - dec R5 decrement loop counter jne SUM
do this 10 timesDONE jmp DONE - END When the program finishes, the sum of
the 10 samples is in RESULT.What is the value
of SP after the sample loop completes?What is
the value of SP after the sum loop
completes? Did stack overflow occur? How
could it have occurred?
18The Stack
- mov.b COUNT,R5 reinitialize the
loop counter - SUM pop R4 bring in the
information off the top of the stack
add.w R4,RESULT continually sum this
with RESULT - dec R5 decrement loop counter jne SUM
do this 10 timesDONE jmp DONE - END When the program finishes, the sum of
the 10 samples is in RESULT.What is the value
of SP after the sample loop completes? 0x026CW
hat is the value of SP after the sum loop
completes? 0x0280Did stack overflow
occur? NOHow could it have occurred? Push
more than 63 words on the stack, the 64th
item would have overwritten RESULT