Sample Problem Set #1 - SOLUTIONS

EENG 383
Sample Problems
Sample Problem Set #1 - SOLUTIONS
Notes:
These problems are typical exam problems; most are drawn from previous homeworks and exams.
This exam is open book, open notes. It may help to have a calculator.
For partial credit, please show all work, reasoning, and steps leading to solutions.
1.
The meaning of what is stored in memory depends on your interpretation. Assume that memory
locations $800 and $801 contain the machine codes for the instructions “COMA” and “INCA”. Give the
meaning of these values in these locations if you interpret them as:
(a) ASCII characters
The values are $41 and $42. If interpreted as ASCII, these are the characters “A” and “B”.
(b) Unsigned 8-bit integers (i.e., give the decimal values)
These have the decimal values 65 and 66. (it would be the same if they were two’s complement integers)
2.
Give the machine code corresponding to the following HCS12 assembly language program. Indicate the
contents of memory at each address after the program is loaded into memory.
MAIN
HERE
ORG
LDX
STX
CPX
BHS
INX
NOP
$0D00
#MAIN
$3000
$10
HERE
Solution:
 Opcodes are loaded into memory starting at $0D00.
 The value of the label MAIN is 0D00.
 The BHS instruction uses relative addressing for the access to HERE.
 The value of the label HERE is 0D0B.
 The program counter will point to 0D0A just before BHS executes
 So we need an offset of +1 decimal
Address
Contents
$0D00
$0D03
$0D06
$0D08
$0D0A
$0D0B
CE
7E
9E
24
08
A7
0D 00
30 00
10
01
Assembly source code
ORG
$0D00
MAIN
LDX
#MAIN
STX
$3000
CPX
$10
BHS
HERE
INX
HERE
NOP
Starting from $0D00, memory contains $CE, 0D, 00, 7E, etc as shown above.
1
EENG 383
3.
Sample Problems
Fill in the blanks below, indicating the address and the contents of memory for the corresponding
assembly language source code.
Address
0800
____
____
Contents
______
______
Assembly language source code
ORG
$0800
M
DS.B
1
; reserve one byte
N
DC.B
1
; reserve one byte & initialize it
0D00
0D00
____
CC____
______
ORG
LDD
STD
$0D00
#10
$0
____
____
B60100
______
LDAA
STAA
$100
M
____
____
______
26____
HERE
ABA
BNE
HERE
Contents
Assembly language source code
ORG
$0800
M
DS.B
1
; reserve one byte
N
DC.B
1
; reserve one byte & initialize it
Solution:
Address
0800
0800
0801
??
01
0D00
0D00
0D03
CC000A
5C00
ORG
LDD
STD
$0D00
#10
$0
0D05
0D08
B60100
7A0800
LDAA
STAA
$100
M
0D0B
1806
HERE
ABA
0D0D
26FC
BNE
the address of the next instruction is 0D0F
4.
HERE
; rr is 0D0B minus 0D0F = -4
The interface shown can be used for low current LEDs. Assume the LED voltage drop
is 2 V. The resistor is 1000 Ω. When the software outputs a high, the voltage on PP0
becomes 4.9 V. When the software outputs a low, the voltage on PP0 becomes 0.5 V.
What is the LED current when the LED is on?
+5V
Solution:
I = (5-2-0.5V)/1000Ω = 2.5V/1000Ω = 2.5 mA
PP0
5.
Write HCS12 C code that sequentially illuminates a single LED segment in a seven-segment display, and
traces a figure “8”. Specifically, it illuminates the top segment( “a”) for a short time, then illuminates
“b”, “g”, “e”, “d”, “c”, “g”, and “f” in turn.
2
EENG 383
Sample Problems
470  each
H CS12
a
b f
c
74HC244
PB6
PB5
PB4
PB3
PB2
PB1
PB0
a
b
g
d
e e
c
f
d
g
com m on cathode
Figure 4.17 D riving a single seven -segm ent display
Solution:
There are eight codes we have to display in a sequence. Let’s put these in a table.
char table[] =
0x40,
//
0x20,
//
0x01,
//
0x04,
//
0x08,
//
0x10,
//
0x01,
//
0x02}; //
{
a
b
g
e
d
c
g
f
void main(void)
{
int n;
DDRB = 0x7f;
// configure bits 0..6 for output
while (1)
{
PTB = table[n];
// display next code
delay();
// delay a little (use your own function)
if (++n == 8)
// increment n to point to next code
n = 0;
// reset back to 0
}
}
6.
What does the following HCS12 assembly language program do? Describe the result of executing the
program; don’t just say what each instruction is doing.
Instructions:
ldx
ldaa
ldab
loop stab
incb
dbne
#$0800
#5
#0
b,x
a,loop
3
EENG 383
Sample Problems
Solution: this program stores $00, $01, $02, $03, $04, into addresses $0800, $0801, $0802, $0803, $0804.
7.
Predict the result of executing the code below. What do registers a,b,x,y contain?
ldaa
ldab
ldx
ldy
psha
pshb
pshx
pshy
pula
pulb
pulx
puly
#$aa
#$bb
#$1234
#$5678
Solution:
After pushing, the stack contains:
$56
$78
$12
$34
$bb
$aa
We pull off in reverse order. So
a = $56
b = $78
x = $1234
y = $bbaa
8.
Estimate the running time of the following code fragment (assume a 24 MHz clock).
LOOP
ldab
decb
bne
#$10
LOOP
Solution: The time in cycles for each instruction is shown:
(1)
(1) LOOP
(3/1)
ldab
decb
bne
#$10
LOOP
4
EENG 383
Sample Problems
The loop is executed 16 times. The first 15 times, each iteration takes 4 cycles. The last time, it takes 2 cycles.
Plus, we have one cycle from the LDAB instruction. So the total is
1 + 15*4 + 2 = 63 cycles
Each cycle takes (1/24) usec. So the total is 63/24 usec = 2.625 usec.
9.
Give the contents of the indicated registers or memory locations after the execution of each of the
following program modules. Assume that prior to the execution of each of the following parts:
 Memory contains
($0080) = $01
($0081) = $02
($0082) = $03
($0083) = $04
 The M68HC12 registers contain: A = $7F, X = $0080
 The NZVC bits in the CCR are 0001
(Note: Do not treat the program modules as executing sequentially, one following another.)
Program module:
After execution:
(a)
ADCA $80
A=
NZVC =
(b)
ADCA #$80
A=
NZVC =
(c)
BHI
BRA
$E000
$E010
PC =
NZVC =
LDD
ABX
0,X
A=
X=
PC =
($0081) =
(d)
(e)
ORG $D00
LDS #$82
JSR $0C10
Solution:
Program module:
After execution:
(a)
ADCA $80
A = $81
HNZVC = 1010
this is 1 + 0111 1111 + 0000 0001 = 1000 0001
(b)
ADCA #$80
A = $00
HNZVC = 0101
this is 1 + 0111 1111 + 1000 0000 = 0000 0000
(c)
BHI
BRA
$E000
$E010
PC = $E010
HNZVC = 0001
BHI will branch if C or Z=0. It is not, so we go to $E010. CCR bits are not changed.
(d)
LDD
ABX
0,X
(e)
A = $01
X = $0082
The first instruction loads A with 01, B with 02. The 2nd instruction adds 02 to X, to get 0082.
ORG $D00
LDS #$82
JSR $0C10
PC = $0C10
($0081) = 06
The LDS instruction is 3 bytes long and the JSR instruction is 3 bytes long. The return address is $0D06.
5
EENG 383
Sample Problems
10. Assume that the stack pointer has the value $0a00. A HCS12 program calls a subroutine, and the
subroutine pushes registers A, X, and Y onto the stack. What does the stack pointer contain now?
The subroutine call pushes the program counter (2 bytes) onto the stack. The A, X, and Y registers are 1, 2, and 2
bytes, respectively. A total of 2+1+2+2 = 7 bytes are pushed onto the stack. The new stack pointer is $0a00 – 7 =
$09f9.
11. The C function below is called with the following input arguments: an array M containing N 8-bit
numbers, and the size N. Describe what the function does.
int func(int M[], int N)
{
int i;
int x = M[0];
for (i=1; i<N; i++)
if (M[i] < x)
x = M[i];
return x;
}
Solution:
The function finds the smallest element of the given array and returns it.
12. Write C code (using a loop) to compute the sum of the squares of the first 100 odd integers.
Solution:
int i, j;
long sq_sum;
sq_sum = 0;
for (i = 0; i < 100; i++) {
j = 2 * i + 1;
sq_sum += (long)j * (long)j;
}
13. Write a C function that converts all uppercase ASCII letters in a string, to lowercase. The string is
passed into the function as an input argument.
Solution:
void upper2lower (char *ptr)
{
while(*ptr++)
if ((*ptr =< 0x5A) && (*ptr >= 0x41))
*ptr = *ptr + 0x20;
}
6
EENG 383
Sample Problems
14. Write C code that takes an array of 10 integers called “buff”, computes the difference between the
maximum and minimum values in the array, and stores it into an integer variable called “diff”. When
calculating the difference, don’t worry about possible overflow:
Solution:
// assume buff is defined elsewhere
int i, max, min, diff;
max = buff[i];
// initialize max and min to 1st element
min = buff[i];
for (i=1; i<10; i++)
{
if (buff[i] > max) max = buff[i];
if (buff[i] < min) min = buff[i];
}
diff = max-min;
7