Sample Problem Set #1 - SOLUTIONS

Transcription

Sample Problem Set #1 - SOLUTIONS
EGGN 482
Sample Problems
Fall 2011
Sample Problem Set #1 - SOLUTIONS
October 2011
Notes:
These problems are typical exam problems; most are drawn from previous homeworks and exams.
This exam is open book, open notes.
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. (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
$0D00
LDX
#MAIN
BRCLR $10,$0C,HERE
Answer:
Opcodes are loaded into memory starting at $0D00
The LDX instruction uses immediate addressing
The value of the label MAIN is 0D00
The BRCLR instruction uses direct addressing
The value of the label HERE is 0D03
The program counter will point to 0D07 just before the BRCLR executes
So we need an offset of -4 decimal (or FC hex)
Address Contents
$0D00 CE 0D 00
$0D03 4F 10 0C FC
$0D07
1
EGGN 482
3.
Sample Problems
Fall 2011
Fill in the blanks below, indicating the address and the contents of memory for the corresponding
assembly language source code.
Address
0800
0800
____
____
Contents
______
______
Assembly language source code
ORG
$0800
N1
EQU
$12
N2
RMB
1
; RMB same as ds.b
N3
FCB
1
0D00
0D00
____
CC____
______
ORG
LDD
STD
$0D00
#N1
$0
____
____
B60100
______
LDAA
STAA
$100
N2
____
____
______
26____
HERE
ABA
BNE
HERE
Contents
Assembly language source code
ORG
$0800
N1
EQU
$12
N2
RMB
1
; RMB same as ds.b
N3
FCB
1
Solution:
Address
0800
0800
0800
0801
??
01
0D00
0D00
0D03
CC0012
5C00
ORG
LDD
STD
$0D00
#N1
$0
0D05
0D08
B60100
7A0800
LDAA
STAA
$100
N2
0D0B
1806
HERE
ABA
0D0D
26FC
BNE
the address of the next instruction is 0D0F
HERE
2
; rr is 0D0B minus 0D0F = -4
EGGN 482
4.
Sample Problems
Fall 2011
The following is an assembler list file, showing the memory location in the left column, the contents of
memory in the next column, and then the source line. Fill in the blanks.
Address Contents
4000
40__
4004
4006
40__
40__
400D
400E
____
84__
5A00
______
09
760000
A7
____
4010 ____
Assembly language source code
PORTA
EQU
$0000
ORG
_____
Entry:
ldaa PORTA
____ #%00111100
____ _____
ldx
count
Loop:
___
ror
PORTA
___
bne
Loop
; define constants in ROM
count
dc.w
256
Solution:
Address Contents
4000
4002
4004
4006
4009
400A
400D
400E
9600
843C
5A00
FE4010
09
760000
A7
26F9
4010 0100
5.
Assembly language source code
PORTA
EQU
$0000
ORG
$4000
Entry:
ldaa PORTA
anda #%00111100
staa PORTA
ldx
count
Loop:
dex
ror
PORTA
nop
bne
Loop
; define constants in ROM
count
dc.w
256
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?
Solution:
I = (5-2-0.5V)/1000Ω = 2.5V/1000Ω = 2.5 mA
PP0
3
+5V
EGGN 482
6.
Sample Problems
Fall 2011
Write HCS12 assembly 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.
470  each
H CS12
a
a
74HC244
PB6
PB5
PB4
PB3
PB2
PB1
PB0
b f
c
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, and use indexed addressing to get
the next code. The counter for which code to display is in accumulator A. So A starts at zero, and goes up to 7.
When A increases beyond 7, we need to reset it back to 0. You could do that with a test (such as “cmpa #8”) and
then do a “clra” if A does equal 8. However, even easier is to note that the values of 0..7 are all contained in the
lower order 3 bits of A. So if we just mask off (set to zero) the higher order 5 bits of A, then A will just count up to
7 and then automatically reset to 0.
loop
table
7.
movb
clra
ldx
movb
ldy
dbne
inca
anda
bra
#$7f,DDRB
; configure bits 0..6 for output
#table
a,x,PTB
#$ffff
y,*
; table of codes to display
; get next code to display
dc.b
dc.b
dc.b
dc.b
dc.b
dc.b
dc.b
dc.b
%01000000
%00100000
%00000001
%00000100
%00001000
%00010000
%00000001
%00000010
; delay a little while
#%07
loop
;
;
;
;
;
;
;
;
a
b
g
e
d
c
g
f
Write an HCS12 assembly language program that stores the constant 64 (decimal) into 255 consecutive
locations in memory, starting at location $0800. Note: for full credit on this problem you must use a loop
rather than a long series of “store” instructions.
Solution: We will use indexed addressing mode. We will use B as a counter. There are other ways to do this.
4
EGGN 482
Sample Problems
Instructions:
ldx
ldab
THERE movb
dbne
8.
#$0800
#$FF
#64,1,X+
B,THERE
Fall 2011
; Use B as a counter
Write a HCS12 assembly language subroutine that copies a table. The starting address of the first table
is passed in through register X, and the starting address of the second table is passed in through register
Y. The length of the tables (in bytes) is passed in through register D. If the subroutine modifies registers
X,Y, or D, it should restore them before returning.
Solution:
MYCOPY
LOOP
9.
PSHD
PSHX
PSHY
MOVB 1,X+,1,Y+
SUBD #1
BNE LOOP
PULY
PULX
PULD
RTS
Write HCS12 assembly language code to implement the following pseudo code module. Assume that N,
NCOUNT, and PCOUNT are 8-bit variables that have been previously defined to be some locations in
memory.
IF (N < 0) THEN
increment NCOUNT
ELSE
increment PCOUNT
ENDIF
Solution:
tst
bmi
ELSEPART inc
bra
THENPART inc
ENDIF
N
THENPART
PCOUNT
ENDIF
NCOUNT
;if (N < 0)
10. Estimate the running time of the following code fragment (assume a 24 MHz clock).
LOOP
ldab
decb
bne
#$10
; note: I originally had ldab #$100, but $100 too big
LOOP
5
EGGN 482
Sample Problems
Fall 2011
Solution: The time in cycles for each instruction:
(1)
(1) LOOP
(3/1)
ldab
decb
bne
#$10
LOOP
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.
11. 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
0,X
6
EGGN 482
ABX
(e)
Sample Problems
Fall 2011
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.
12. 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.
13. 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.
14. 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;
}
15. 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)
{
7
EGGN 482
Sample Problems
Fall 2011
while(*ptr++)
if ((*ptr =< 0x5A) && (*ptr >= 0x41))
*ptr = *ptr + 0x20;
}
16. Write a C function that takes a pointer to an array and returns the difference between the maximum and
minimum values. The array contains 16-bit numbers. The first element of the array is the length and
remaining elements are 16-bit signed numbers. For example, here are three such possible arrays:
short buf1[5]={4,1000,-1000,0,33};
short buf2[7]={6,-4,100,200,2,0,44};
short buf3[1]={0};
And this is what your function should return:
Result1 = MaxDiff(buf1); // should return 2000 = 1000 – (-1000)
Result2 = MaxDiff(buf2); // should return 204 = 200 – (-4)
Result3 = MaxDiff(buf3); // should return 0 because array is empty
You are not allowed to add global variables. Don’t worry about overflow calculating the difference.
Solution:
short MaxDiff(short *pt){
short size,max,min,n;
size = *pt; pt++;
if(size == 0) return 0; // empty
max = -32768; min = +32767;
while(size){
n = *pt; pt++;
if(n > max) max = n;
if(n < min) min = n;
size--;
}
return max-min;
}
8