Review Exam 1: EE 308 Spring 2011
Review Exam 1: EE 308 Spring 2011
Review Exam 1: EE 308 Spring 2011
Review Exam 1
• Numbers
• Programming Model
• Instructions
• Machine Code
– Reverse Assembly
• Assembly Language
0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 5
0110 6 6
0111 7 7
1000 8 8
1001 9 9
1010 A 10
1011 B 11
1100 C 12
1101 D 13
1110 E 14
1111 F 15
Division Q R
Decimal Hex
721/16 45 1 1
45/16 2 13 D
2/16 0 2 2
721 10 = 2D1 16
If the most significant bit (MSB) is 0 (most significant hex digit 0−7), then the number
is positive.
Get decimal equivalent by converting number to decimal, and use the positive (+) sign.
If the most significant bit is 1 (most significant hex digit 8−F), then the number is
negative.
Get decimal equivalent by taking 2’s complement of number, converting to decimal,
and using negative (−) sign.
One’s complement
One’s complement
2F3C + 1 = 2F3D
EE 308 Spring 2011
ADDITION:
7A AC
+52 +72
----- ------
CC 1E
C: 0 C: 1
V: 1 V: 0
N: 1 N: 0
Z: 0 Z: 0
EE 308 Spring 2011
SUBTRACTION:
C bit set on borrow (when the magnitude of the subtrahend is greater than the
minuend
7A 2C
-5C -72
----- ------
1E BA
C: 0 C: 1
V: 0 V: 0
N: 0 N: 1
Z: 0 Z: 0
EE 308 Spring 2011
Programming Model
EE 308 Spring 2011
CLRA ; Clear A 0 → A
87
Branch instruction: One byte following op code specifies how far to branch
Treat the offset as a signed number; add the offset to the address following the
current instruction to get the address of the instruction to branch to
(BRA) 20 35 PC + 2 + 0035 → PC
(BRA) 20 C7 PC + 2 + FFC7 → PC
PC + 2 − 0039 → PC
Long branch instruction: Two bytes following op code specifies how far to branch
Treat the offset as an unsigned number; add the offset to the address following the
current instruction to get the address of the instruction to branch to
When writing assembly language program, you don’t have to calculate offset
You indicate what address you want to go to, and the assembler calculates the offset
EE 308 Spring 2011
• You can determine how many cycles an instruction takes by looking up the CPU cycles
for that instruction in the Core Users Guide.
The program executes the ldab #10 instruction once (which takes one cycle). It then goes
through loop 10 times (which has two instructions, on with one cycle and one with three
cycles), and finishes with the swi instruction (which takes 9 cycles).
1 + 10 × (1 + 3) + 9 = 50
Branch if A > B
Is 0xFF > 0x00?
Reverse Assembly
Disassembly of an HC12 Program
ADDR DATA
---------------------------------------------------------
2000 C6 05 CE 20 00 E6 01 18 06 04 35 EE 3F
• Use up all the bytes for one instruction, then go on to the next instruction.
• When we use subroutines and interrupts it is essential to have such a storage region.
• The Stack Pointer (SP) register is used to indicate the location of the last item put onto
the stack.
• When you put something onto the stack (push onto the stack), the SP is decremented
before the item is placed on the stack.
• When you take something off of the stack (pull from the stack), the SP is incremented
after the item is pulled from the stack.
• Before you can use a stack you have to initialize the Stack Pointer to point to one
value higher than the highest memory location in the stack.
• Use the LDS (Load Stack Pointer) instruction to initialize the stack point.
• The LDS instruction is usually the first instruction of a program which uses the stack.
Subroutines
• A subroutine is a section of code which performs a specific task, usually a task which
needs to be executed by different parts of a program.
• Example:
– Math functions, such as square root
• Because a subroutine can be called from different places in a program, you cannot get
out of a subroutine with an instruction such as jmp label because you would need to
jump to different places depending upon which section of code called the subroutine.
• When you want to call the subroutine your code has to save the address where the
subroutine should return to. It does this by saving the return address on the stack.
– This is done automatically for you when you get to the subroutine by using the
JSR (Jump to Subroutine) or BSR (Branch to Subroutine) instruction. This
instruction pushes the address of the instruction following the JSR/BSR
instruction on the stack.
• After the subroutine is done executing its code it needs to return to the address saved on
the stack.
– This is done automatically for you when you return from the subroutine by
using the RTS (Return from Subroutine) instruction. This instruction pulls the
return address off of the stack and loads it into the program counter, so the
program resumes execution of the program with the instruction following that
which called the subroutine.
The subroutine will probably need to use some MC9S12 registers to do its work.
However, the calling code may be using its registers for some reason - the calling code
may not work correctly if the subroutine changes the values of the HC12 registers.
– To avoid this problem, the subroutine should save the MC9S12 registers before
it uses them, and restore the MC9S12 registers after it is done with them.
EE 308 Spring 2011
EE 308 Spring 2011
• Want inner loop to last for 1 ms. MC9S12 runs at 24,000,000 cycles/second, so 1 ms
is 24,000 cycles.
• To solve this problem, save the values of A and X on the stack before using them, and
restore them before returning.