You are on page 1of 10

Initials: ________

CprE 288 Introduction to Embedded Systems


Fall 2014
Exam 3

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: ________

1. Basics [10 pts]

a. [4 pts] True or False (F)

_F__ All AVR instructions are 32-bit long.

_T__ An AVR arithmetic instruction may not use a memory variable as an operand.

_T__ The AVR architecture is a form of Harvard architecture.

_T__ The X, Y, and Z registers are designed to hold pointer values.

b. [2 pts] Which of the following is a valid AVR assembly instructions? Mark Y (Yes) or N
(No).

_Y__ ADD r1, r0

_Y__ ADIW r24, 1

_N__ SUBI r1, 5

_N__ LDS r24, X+

c. [2 pts] What are the values of those SREG flags right after executing the following code?

LDI R16, 0x80


LDI R17, 0x20
ADD R16, R17

N __1___ Z __0___ S_1___ V __0__ C __0__ H __0__

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]

a. [5 pts] Arithmetic operations.

extern signed int a; // Assume a is located at 0x1000


extern signed int b; // Assume b is located at 0x1002
extern signed int c; // Assume c is located at 0x1004

c = c - (a + b);

; Load variables into registers


LDS R0, a OR LDS R0, 0x1000
LDS R1, a+1 OR LDS R0, 0x1001
LDS R2, b OR LDS R0, 0x1002
LDS R3, b+1 OR LDS R0, 0x1003
LDS R4, c OR LDS R0, 0x1004
LDS R5, c+1 OR LDS R0, 0x1005

; a + b
ADD R0, R2
ADC R1, R3

; c – (a + b)
SUB R4, R0
SBC R5, R1

; Store back to variable


STS c, R4 OR STS 0x1004, R4;
STS c+1, R5 OR STS 0x1005, R5;

3
Initials: ________

b. [5 pts] Logic operations with immediate value. You have to use the minimum number of
instructions.

extern signed long flag; // Assume flag is at 0x2000

flag = flag & 0xFF773300;

; 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

; flag & 0xFF773300


ANDI R20, 0x00
ANDI R21, 0x33
ANDI R22, 0x77
ANDI R23, 0xFF

; 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: ________

c. [10 pts] If statement.

extern unsigned char m; // Assume m is at 0x3000


extern unsigned char n; // Assume n is at 0x3001
extern unsigned char p; // Assume p is at 0x3002

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

; check (n <= p) 1st convert to p >= n -> p < n


CP R16, R0
BRLO 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: ________

3. Functions in assembly [15].

Recall the rules of GCC-AVR function call convention:


Function parameters
• R25:R24, R23:R22, ..., R9:R8
• All aligned to start in even-numbered register, i.e. char will take two registers (use
the even one)
• A long type uses two pairs
• Extra parameters go to stack

Function return values


• 8-bit in r24 (with r25 cleared to zero), or
• 16-bit in R25:R24, or
• 32-bit in R25-R22, or
• 64-bit in R25-R18

Non-volatile: R2-R17, R28-R29


• Calling function (Caller) may use them for free, callee must keep their old values

Volatile: R18-R27, R30-R31


• Called function (Callee) may use them for free, caller must save and restore their
old values if it using them

Fixed registers
• R0: Temporary register used by gcc (no need to save), R1: Should be zero

The following two sub-questions are independent.

6
Initials: ________

a. [5 pts] Making a function call: With the following declarations:

// USART datum buffer for receiving


extern char datum;

// USART receiving status


extern int status;

// USART receive function, storing the received character


// in *p_datum and returning any error status
extern int serial_getchar(char *p_datum);

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);

; Place your assembly code below.

; Push volatile registers r19/r18


PUSH r19
PUSH r18

; Set up parameter p_datum


LDS r24, lo8(datum)
LDS r25, hi8(datum)

RCALL serial_getc

; Save return value to status


STS status, r24
STS status+1, r25

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).

void serial_puts(char *msg)


{
char ch;

while ((ch = *msg++) != ‘\0’)


serial_putchar(ch);
}

The prototype of function serial_putchar() is as follows:


extern void serial_putchar(char ch);

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’.

YOUR CODE HERE


serial_puts:
; parameter: msg=>r25:r24
; usage: ch=>r24, msg=>Y-reg

; Push non-volatile register r29/r28 (Y-reg)


PUSH r29
PUSH r28

; Move msg to Y-reg and jump to cond. check


MOVW r28, r24
RJMP cond
loop:
; Loop body: serial_putchar(ch)
RCALL serial_putc
cond:
; Cond. check: (ch = *msg++) != ‘\0’
LD r24, Y+
TST r24
BRNZ loop

POP r28
POP r29
RET

4. Analyzing Assembly [15 pts]

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]

LDS R7, 0xFF00 Address Value (16-bits)


CP R0, R15
BRLT 0x0A 0x0100 1001 0000 0111 0000 (0x9070)
0x0101 1111 1111 0000 0000 (0xFF00)
0x0102 0001 0100 0000 1111 (0x140F)
0x0103 1111 0000 0101 0100 (0xF054)

The format for LDS is:


Operation: Rd ← (k)

Syntax: Operands: Program Counter:


LDS Rd, k 0 ≤ d ≤ 31, 0 ≤ k ≤ 65535 PC ← PC + 2

32-bit Opcode:
1001 000d dddd 0000
kkkk kkkk kkkk Kkkk

The format CP is:


Operation: Rd - Rr

Syntax: Operands: Program Counter:


CP Rd, Rr 0 ≤ d ≤ 31, 0 ≤ r ≤ 31 PC ← PC + 1
16-bit Opcode:
0001 01rd dddd rrrr

The format for BRLT is:


Operation: If Rd < Rr (N XOR V = 1), then PC ← k + 1, else PC ← PC+1

Syntax: Operands: Program Counter:


BRLT k -64 ≤ k ≤ 63 PC ← PC + k + 1
PC ← PC + 1, if condition false
16-bit Opcode:
1111 00kk kkkk k100

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

Register File Data Memory


Reg Value Reg Value Address Value
R31 (0xFF) R15 0xFFFF
R30 (0xF0) 0xF1 R14 0xFFFE
R29 (0xFF) R13 0xFFFD
R28 (0xF6) R12 0xFFFC
R27 (0x00) 0x66 R11 0xFFFB 0x77
R26 (0xFF) R10 0xFFFA
R25 R9 (0x99) 0xFFF9 (0xF4)
R24 R8 (0x88)0x0A 2pt 0xFFF8 (0x0A)
R23 R7 (0x77) 0xFFF7 (0x07)
R22 R6 (0x66) 0xFFF6 (0x03)
R21 R5 (0x55) 0xFFF5 (0x01)
R20 R4 (0x44) 0xFFF4 (0x04)
R19 R3 0xFFF3 (0xC0)
R18 R2 0x07 1pt 0xFFF2 (0x00) 0x55
R17 R1 0xFFF1
R16 R0 0xFFF0 0x44

10

You might also like