Professional Documents
Culture Documents
SOLUTIONS
Note: The solutions are provided for your self-study. Do
NOT keep or distribute the solutions. Delete your local copy
each time after you read the solutions.
Instructions:
• Do not open this exam booklet until told to do so.
• Open book, open notes.
• Calculator allowed.
• Do all work in the space provided. You must show your work to receive credit.
• Clearly indicate the answer you want counted.
• Make assumptions if necessary to work problems.
• Be sure to complete the four lines above.
• Cheating will not be tolerated by the instructor or your classmates and will result in a
severe penalty (e.g. failing the class).
• This is a 75-minute exam.
Score:
Q1 Q2 Q3 Q4 Total
10 20 15 15 60
NOTEs:
• All questions are regarding the ATmega128 microcontroller and the AVR Studio
platform unless stated otherwise. You may assume <avr/io.h> is included.
• Use the syntax of GCC/AVR assembly language.
• Assembly code with poor programming style or bad run-time efficiency may receive
partial credit.
1
Initials: ________
_T__ An AVR arithmetic instruction may not use a memory variable as an operand.
b. [2 pts] Which of the following is a valid AVR assembly instructions? Mark Y (Yes) or N
(No).
c. [2 pts] What are the values of those SREG flags right after executing the following code?
d. [2 pts] In an ANDI instruction in form of “ANDI Rd, K”, the register Rd has to be one from
R16-R31. What is the reason, within the AVR instruction set design, for this limit?
2
Initials: ________
2. Compiling C into Assembly: Write the equivalent assembly code for each of the
following C code fragments. Assume that the declarations of global C variables are
properly imported into the assembly program. Deductions will be given for inefficient code
[20 pts]
c = c - (a + b);
; a + b
ADD R0, R2
ADC R1, R3
; c – (a + b)
SUB R4, R0
SBC R5, R1
3
Initials: ________
b. [5 pts] Logic operations with immediate value. You have to use the minimum number of
instructions.
; Load variables
LDS R20, flag OR LDS R20, 0x2000
LDS R21, flag + 1 OR LDS R21, 0x2001
LDS R22, flag + 2 OR LDS R22, 0x2002
LDS R22, flag + 3 OR LDS R23, 0x2003
; store variable
STS flag, R20 OR STS 0x2000, R20
STS flag+1, R21 OR STS 0x2001, R21
STS flag+2, R22 OR STS 0x2002, R22
STS flag+3, R23 OR STS 0x2003, R23
4
Initials: ________
if ( (n == m) && (n <= p) )
{
p = 7;
}
else
{
p = 0;
}
; Load variables
LDS R0, n OR LDS R0, 0x3001
LDS R1, m OR LDS R1, 0x3000
LDS R16, p OR LDS R16, 0x3002
; check n == m
CP R0, R1
BRNE else
; if body
LDI R16, 7
STS p, R16 OR LDS 0x3002, R16
JMP endif
else:
; else body,
LDI R16, 0
STS p, R16 OR LDS 0x3002, R16
endif:
5
Initials: ________
Fixed registers
• R0: Temporary register used by gcc (no need to save), R1: Should be zero
6
Initials: ________
Assume all those symbols (names) have been declared properly in the beginning of the
assembly program file.
Write assembly code to 1) set up the parameters and, 2) make (i.e. RCALL) the
following function call and, 3) save the return value. Do NOT implement the function.
Also assume that at the time of the function call, registers R16, R17, R18 and R19 are
being used by the caller function (the function making the call), and thus their values
should be kept the same across (i.e. just before and just after) the function call.
Hint: You may use the hi8() and lo8() assembly macros.
status = serial_getchar(&datum);
RCALL serial_getc
POP r18
POP r19
7
Initials: ________
b. [10] Write the following C function in assembly (i.e. your assembly function should be
functionally equivalent to the C function).
Note:
1) Make sure to indicate which register is used for what purpose.
2) Do NOT to write the assembly code for calling function serial_puts().
3) Do NOT write the assembly code for function serial_putchar().
4) Expression (ch = *msg++) puts the dereferenced value of pointer msg into ch,
increments msg, and returns the dereferenced value to be compared with ‘\0’.
POP r28
POP r29
RET
8
Initials: ________
a) Write the binary encoding in code memory for the following sequence of assembly
instructions. Assume 1) that this sequence of code starts at address 0x0100 in code memory,
2) for binary encoding the most significant bit is the left most bit [6 pts]
32-bit Opcode:
1001 000d dddd 0000
kkkk kkkk kkkk Kkkk
9
Initials: ________
b) Fill the final vales of the Register file and Data memory after the following assemble
code has been executed. Vales in ( ) are the initial values before the assemble code is
executed. Show temporary values to help with giving partial credit, and if an entry is
unknown after executing the code then leave blank. [9 pts]
STD Z+2, R5
ST Z+, R4
LDS R2, 0xFFF7
LDD R8, Y+2
OR R27, R6
STS 0xFFFB, R7
10