`Switches and Lights` Assembly version

Embedded Systems Programming
Assembly language
Vs
Embedded C
Examples and Comparison
1
First example:
A very simple program - ‘Switches and lights’
2
Simple
Example
Program
‘Switches and
Lights’
Assembly
Version
Page 1
3
Simple
Example
Program
‘Switches and
Lights’
Assembly
Version
Page 2
4
Memory Image
‘Switches and Lights’
Assembly version
Reset vector (program starts here), rjmp INIT
Set port B (ldi, out, out)
The first four
instructions load
the stack pointer
value
Set port D (ldi, out, ldi, out)
rjmp main
Org 0x020 ‘INIT’
label
Delay subroutine
Main loop
(ldi, ldi, ldi, dec, brne, dec, brne, dec, brne, ret) (in, out, rcall delay, rjmp main)
Assembly code instructions are converted into ‘machine code’.
One assembly instruction = 2 byte machine code instruction (op-code).
Examples: ldi = 0xE, val (hi byte) RegNum -16, val (lo byte)
out = 0xB, Port addr (hi byte) RegNum -16, Port addr (lo byte)
ret = 0x95 (hi byte) 0x08 (lo byte)
5
Simple Example Program
‘Switches and Lights’
Embedded ‘C’
(1)
Simple Example Program
‘Switches and Lights’
Embedded ‘C’
(2)
Assembly version of Program ‘Switches and Lights’
(1)
Automatically generated from the Embedded ‘C’ version (.lss file)
Note that this is much longer than the hand-crafted assembly version
00000000 <__vectors>:
0:
14 c0
rjmp
.+40
; 0x2a <__ctors_end>
2:
1b c0
rjmp
.+54
; 0x3a <__bad_interrupt>
4:
1a c0
rjmp
.+52
; 0x3a <__bad_interrupt>
(some vectors skipped for brevity)
24:
0a c0
rjmp
.+20
; 0x3a <__bad_interrupt>
26:
09 c0
rjmp
.+18
; 0x3a <__bad_interrupt>
28:
08 c0
rjmp
.+16
; 0x3a <__bad_interrupt>
0000002a <__ctors_end>:
2a:
11 24
eor
2c:
1f be
out
2e:
cf e5
ldi
30:
d2 e0
ldi
32:
de bf
out
34:
cd bf
out
36:
02 d0
rcall
38:
57 c0
rjmp
All unused interrupt
vectors are pointed to
the same place, which
in turn points back to
the reset vector
r1, r1
0x3f, r1 ; 63
These four statements set the stack
r28, 0x5F ; 95
pointer, they are placed here automatically
r29, 0x02 ; 2
by the C compiler so the programmer does
0x3e, r29 ; 62
not need to do it manually
0x3d, r28 ; 61
.+4
; 0x3c <main>
.+174
; 0xe8 <_exit>
0000003a <__bad_interrupt>:
3a:
e2 cf
rjmp
.-60
; 0x0 <__vectors>
Assembly version of Program ‘Switches and Lights’
Automatically generated from the Embedded ‘C’ version (.lss file)
(2)
First part of main()
0000003c <main>:
// Declare functions (these will be in a separate header file in larger programs)
void InitialiseGeneral();
int main( void )
{
3c:
df 93
push
r29
3e:
cf 93
push
r28
40:
0f 92
push
r0
42:
cd b7
in
r28, 0x3d ; 61
44:
de b7
in
r29, 0x3e ; 62
unsigned char Switches_Value; // Declare a local variable
46:
InitialiseGeneral();
0f d0
rcall
.+30
; 0x66 <InitialiseGeneral>
Assembly version of Program ‘Switches and Lights’
Automatically generated from the Embedded ‘C’ version (.lss file)
(3)
Second part of main()
while(1)
// Loop
{
Switches_Value = PIND; // Read value from the switches into the variable
48:
80 e3
ldi
r24, 0x30 ; 48
4a:
90 e0
ldi
r25, 0x00 ; 0
4c:
fc 01
movw
r30, r24
4e:
80 81
ld
r24, Z
50:
89 83
std
Y+1, r24 ; 0x01
PORTB = Switches_Value;
// Output the value from the variable onto the LEDs
52:
88 e3
ldi
r24, 0x38 ; 56
54:
90 e0
ldi
r25, 0x00 ; 0
56:
29 81
ldd
r18, Y+1 ; 0x01
58:
fc 01
movw
r30, r24
5a:
20 83
st
Z, r18
Delay();
5c:
8d e4
ldi
r24, 0x4D; 77
5e:
90 e0
ldi
r25, 0x00 ; 0
60:
fc 01
movw
r30, r24
62:
09 95
icall
}
64:
f1 cf
rjmp
.-30
; 0x48 <__SREG__+0x9>
Assembly version of Program
void InitialiseGeneral()
{
66:
df 93
push
68:
cf 93
push
6a:
cd b7
in
6c:
de b7
in
DDRB = 0xFF;
6e:
87 e3
ldi
70:
90 e0
ldi
72:
2f ef
ldi
74:
fc 01
movw
76:
20 83
st
PORTB = 0xFF;
78:
7a:
7c:
7e:
80:
r29
r28
r28, 0x3d
r29, 0x3e
r24, 0x37
r25, 0x00
r18, 0xFF
r30, r24
Z, r18
r24, 0x38
r25, 0x00
r18, 0xFF
r30, r24
Z, r18
8a:
8c:
8e:
90:
92:
94:
96:
cf 91
df 91
r28
r29
r24, 0x31
r25, 0x00
r30, r24
Z, r1
; 61
; 62
// Configure PortB direction for Output
; 55
;0
; 255
; 56
;0
; 255
// Configure PortD direction for Input
; 49
;0
// Set pullup resistors on
r24, 0x32
; 50
r25, 0x00
;0
r18, 0xFF
; 255
r30, r24
Z, r18
// (ensures switches cause change from a '1' to a '0' when pressed)
}
pop
pop
(4)
// Set all LEDs initially off (inverted on the board, so '1' = off)
88 e3
ldi
90 e0
ldi
2f ef
ldi
fc 01
movw
20 83
st
DDRD = 0x00;
81 e3
ldi
90 e0
ldi
fc 01
movw
10 82
st
PORTD = 0xFF;
82 e3
ldi
90 e0
ldi
2f ef
ldi
fc 01
movw
20 83
st
82:
84:
86:
88:
‘Switches and Lights’
Assembly version of Program
void Delay()
{
9a:
9c:
9e:
a0:
a2:
a4:
a6:
a8:
aa:
ac:
ae:
b0:
b2:
b4:
b6:
b8:
ba:
bc:
be:
c0:
c2:
‘Switches and Lights’
df 93
push
r29
cf 93
push
r28
00 d0
rcall
.+0
; 0xa0 <Delay+0x6>
0f 92
push
r0
cd b7
in
r28, 0x3d
; 61
de b7
in
r29, 0x3e
; 62
unsigned char DelayCount1, DelayCount2, DelayCount3;
// Declare local variables
for(DelayCount1 = 0X04; DelayCount1 > 0; DelayCount1--)
84 e0
ldi
r24, 0x04
;4
89 83
std
Y+1, r24
; 0x01
15 c0
rjmp
.+42
; 0xd6 <Delay+0x3c>
{
for(DelayCount2 = 0XFF; DelayCount2 > 0; DelayCount2--)
8f ef
ldi
r24, 0xFF
; 255
8a 83
std
Y+2, r24
; 0x02
0c c0
rjmp
.+24
; 0xca <Delay+0x30>
{
for(DelayCount3 = 0XFF; DelayCount3 > 0; DelayCount3--);
8f ef
ldi
r24, 0xFF
; 255
8b 83
std
Y+3, r24
; 0x03
03 c0
rjmp
.+6
; 0xbe <Delay+0x24>
8b 81
ldd
r24, Y+3
; 0x03
81 50
subi
r24, 0x01
;1
8b 83
std
Y+3, r24
; 0x03
8b 81
ldd
r24, Y+3
; 0x03
88 23
and
r24, r24
d1 f7
brne
.-12
; 0xb8 <Delay+0x1e>
{
unsigned char DelayCount1, DelayCount2, DelayCount3;
c4:
c6:
c8:
ca:
cc:
ce:
// Declare local variables
for(DelayCount1 = 0X04; DelayCount1 > 0; DelayCount1--)
{
for(DelayCount2 = 0XFF; DelayCount2 > 0; DelayCount2--)
8a 81
ldd
r24, Y+2
; 0x02
81 50
subi
r24, 0x01
;1
8a 83
std
Y+2, r24
; 0x02
8a 81
ldd
r24, Y+2
; 0x02
88 23
and
r24, r24
89 f7
brne
.-30
; 0xb2 <Delay+0x18>
(5)
Memory Image
‘Switches and Lights’
Reset vector
C version
While loop
InitialiseGeneral()
Main()
Delay()
13
Advantages of Assembly language
• Code is more concise – requires less storage space
(can be very important e.g. ATmega8535 only has enough program
memory for 4000 instructions, ATtiny25 holds only 1000 instructions.
• Code executes faster – the programmer can avoid some surplus code
that a C compiler automatically inserts, and thus some unnecessary run
time steps are removed.
• Simpler instructions and syntax (but more instructions needed).
• Can achieve precise timing, and can determine the timing of code
execution by examining the instruction sequence (each instruction
takes exactly 1, 2 or 3 clock cycles).
Examples of the need for such precision: PWM, Pulsing an ultrasonic
transmitter, serial communications handler (clocking bits in / out).
Advantages of Embedded C
• Derived from C – a well known, sophisticated standard.
• Simpler to develop (general opinion) because the instructions are at a
‘higher level’ and generally need less instructions because each one
covers more functionality.
• Potentially less error prone (syntactically), because fewer statements.
• Potentially less error prone (semantically), as the higher level
language is a closer representation of the application logic than
assembly is, and the compiler enforces structure.
• It is possible to insert assembly code functions into Embedded C
programs, using the ASM keyword.
• Large programs can be modularised more easily, using headers and
libraries.
• Programs are more portable (i.e. Movable from one platform to
another).
Second example:
Configuring a programmable timer to generate a
1Hz pulse – and flash an LED at that rate
‘TimerDemo1_SingleTimerSingleLEDFlash’ - Assembly version
(1)
‘TimerDemo1_SingleTimerSingleLEDFlash’ - Assembly version
(2)
‘TimerDemo1_SingleTimerSingleLEDFlash’ - Assembly version
(3)
‘TimerDemo1_SingleTimerSingleLEDFlash’ - Assembly version
(4)
Memory image for TimerDemo1_SingleTimerSingleLEDFlash.asm
‘TimerDemo1_SingleTimerSingleLEDFlash’ - ‘C’ version
(1)
‘TimerDemo1_SingleTimerSingleLEDFlash’ - ‘C’ version
(2)
‘TimerDemo1_SingleTimerSingleLEDFlash’ - ‘C’ version
(3)
‘TimerDemo1_SingleTimerSingleLEDFlash’ - ‘C’ version
(4)
Memory image for C_TimerDemo1_SingleTimerSingleLEDFlash.c