CPE 323 Introduction to Embedded Computer Systems MSP
CPE 323 Introduction to Embedded Computer Systems: MSP 430: Assembly Language and C Instructor: Dr Aleksandar Milenkovic Lecture Notes CPE 323 Intro 2 Embedded. Systems
Outline n Assembly Language Programming n n n Subroutines n n n Adding two 32 -bit numbers (decimal, integers) Counting characters ‘E’ CALL&RETURN Subroutine Nesting Passing parameters Stack and Local Variables C and the MSP 430 CPE 323 Intro 2 Embedded. Systems 2
Assembly Language Programming: Decimal/Integer Addition of 32 -bit Numbers n Problem n Write an assembly program that finds a sum of two 32 -bit numbers n n n Data: n n n Input numbers are decimal numbers (8 -digit in length) Input numbers are signed integers in two’s complement lint 1: DC 32 0 x 45678923 lint 2: DC 32 0 x 23456789 Decimal sum: 0 x 69135712 Integer sum: 0 x 68 adf 0 ac Approach n n Input numbers: storage, placement in memory Results: storage (ABSOLUTE ASSEMBLER) Main program: initialization, program loops Decimal addition, integer addition CPE 323 Intro 2 Embedded. Systems 3
Decimal/Integer Addition of 32 -bit Numbers /*---------------------------------------* Program : Program demonstrates addition of two operands lint 1 and lint 2. * Operands are first interpreted as 32 -bit decimal numbers and * and their sum is stored into lsumd; * Next, the operands are interpreted as 32 -bit signed integers * in two's complement and their sum is stored into lsumi. * Input : Input integers are lint 1 and lint 2 (constants in flash) * Output : Results are stored in lsumd (decimal sum) and lsumi (int sum) * Written by : A. Milenkovic * Date : September 10, 2008; Updated September 14, 2009 * Description: MSP 430 IAR EW; Demonstration of the MSP 430 assembler *---------------------------------------*/ #include "msp 430. h" lint 1: lint 2: lsumd: lsumi: ; #define controlled include file NAME main ; module name PUBLIC main ORG 0 x. F 000 DC 32 0 x 45678923 DC 32 0 x 23456789 ; ; ; ORG DC 16 ; set reset vector to 'init' label 0 x. FFFE main ORG 0 x 0400 DS 32 1 make the main label visible outside this module move location pointer to 0 x. F 000 operand 1 operand 2 ; allocates space for lsumd (2 words) ; allocate space for lsumi (2 words) CPE 323 Intro 2 Embedded. Systems 4
Decimal/Integer Addition of 32 -bit Numbers (cont’d) main: ldeca: ORG 0 x. E 000 NOP MOV. W #WDTPW+WDTHOLD, &WDTCTL MOV #lint 1, R 4 MOV #lsumd, R 8 MOV #2, R 5 CLR R 10 MOV 4(R 4), R 7 MOV R 10, R 2 DADD @R 4+, R 7 MOV R 2, R 10 MOV R 7, 0(R 8) ADD #2, R 8 DEC R 5 JNZ ldeca ; ; ; ; starting address of the program main program Stop watchdog timer pointer to lint 1 pointer to lsumd (decimal sum) R 5 is a counter (2 words) clear R 10 (used as a backup for SR) load lint 2 (@R 4+4) into R 7 bring original R 2 decimal add to lint 1 (@R 4) backup R 2 in R 10 store result back into lsumd R 8 points to the next word in lsumd decrement R 5 jump if not zero to ldeca CPE 323 Intro 2 Embedded. Systems 5
Decimal/Integer Addition of 32 -bit Numbers (cont’d) lia: MOV #lint 1, R 4 MOV #lsumd, R 8 MOV #2, R 5 CLR R 10 MOV 4(R 4), R 7 MOV R 10, R 2 ADDC @R 4+, R 7 MOV R 2, R 10 MOV R 7, 4(R 8) ADD #2, R 8 DEC R 5 JNZ lia ; ; ; pointer to lint 1 pointer to lsumd R 5 is a counter clear R 10 load lint 2 load original SR add lint 1 (with carry) backup R 2 in R 10 store result into lsumi (@R 8+4) update R 8 decrement R 5 jump if not zero to lia JMP $ ; jump to current location '$' ; (endless loop) END CPE 323 Intro 2 Embedded. Systems 6
Assembly Language Directives b 1: ORG 0 x. F 000 DB 5 tf ; ; DB -122 ; DB 10110111 b DB 0 x. A 0 ; DB 123 q ; EVEN ; EQU 25 w 1: DW 32330 w 2: dw 1: DW DL -32000 100000 dw 2: dw 3: dw 4: s 1: s 2: DL DL DL DB DB b 2: b 3: b 4: b 5: allocates a byte in memory and initialize it with constant 5; equivalent to DC 8 5 allocates a byte with constant -122 ; binary value of a constant hexadecimal value of a constant octal value of a constant move a location pointer to the first even address ; allocates a a word size constant in memory; ; equivalent to DC 16 32330 ; allocates a long word size constant in memory; ; equivalent to DC 32 100000 -10000 0 x. FFFF tf 'ABCD' ; allocates 4 bytes in memory with string ABCD "ABCD" ; allocates 5 bytes in memory with string ABCD ; and character at the end CPE 323 Intro 2 Embedded. Systems 7
Assembly Language Directives (cont’d) v 1 b v 2 b v 3 w ORG DS DS DS 0 x 0200 1 1 2 v 4 b DS 32 4 ; allocates a byte in memory; equivalent to DS 8 ; allocates a byte in memory; ; allocates a word of 2 bytes in memory; ; equivalent to DS 8 2 or DS 16 ; allocates a buffer of 4 long words; ; 4 x 4=16 bytes in memory CPE 323 Intro 2 Embedded. Systems 8
Assembly Language Programming: Count Characters ‘E’ n Problem n n n Example: n n n Write an assembly program that processes an input string to find the number of characters ‘E’ in the string The number of characters is “displayed” on the port 1 of the MSP 430 mystr=“HELLO WORLD, I AM THE MSP 430!” P 1 OUT=0 x 02 Approach n n Input string: storage, placement in memory Main program: initialization, main program loop Program loop: iterations, counter, loop exit Output: control of ports CPE 323 Intro 2 Embedded. Systems 9
Programmer’s View of Parallel Ports n n Six parallel ports: x=1, 2, 3, 4, 5, 6 Each can be configured as: n n n Input: Px. DIR=0 x 00 (default) Output: Px. DIR=0 x. FF Writing to an output port: n n Port Registers Px. OUT=x 02 Reading from an input port: n P 1 OUT P 1 DIR P 1 IN My_port=P 1 IN CPE 323 Intro 2 Embedded. Systems 10
Count Characters ‘E’ /*---------------------------------* Program : Counts the number of characters E in a string * Input : The input string is the my. Str * Output : The port one displays the number of E's in the string * Written by : A. Milenkovic * Date : August 14, 2008 * Description: MSP 430 IAR EW; Demonstration of the MSP 430 assembler *-----------------------------------*/ #include "msp 430. h" NAME PUBLIC main ORG DC 16 0 FFFEh init RSEG CSTACK CODE ; #define controlled include file ; module name ; make the main label visible ; outside this module ; set reset vector to 'init' label ; pre-declaration of segment CSTACK ; place program in 'CODE' segment CPE 323 Intro 2 Embedded. Systems 11
Count Characters ‘E’ (cont’d) init: MOV #SFE(CSTACK), SP main: gnext: NOP ; main program MOV. W #WDTPW+WDTHOLD, &WDTCTL ; stop watchdog timer BIS. B #0 FFh, &P 1 DIR ; configure P 1. x output ; load the starting address of the string into the register R 4 MOV. W #my. Str, R 4 CLR. B R 5 ; register R 5 will serve as a counter MOV. B @R 4+, R 6 ; get a new character CMP #0, R 6 JEQ lend ; go to the end CMP. B #'E', R 6 JNE gnext INC R 5 ; increment counter JMP gnext lend: MOV. B JMP R 5, &P 1 OUT $ ; set up stack ; set all P 1 pins ; jump to itself my. Str DB "HELLO WORLD, I AM THE MSP 430!" ; the string is placed in the flash ; the null character is automatically added after the '!' END CPE 323 Intro 2 Embedded. Systems 12
Outline n Assembly Language Programming n n n Subroutines n n n Adding two 32 -bit numbers (decimal, integers) Counting characters ‘E’ CALL&RETURN Subroutine Nesting Passing parameters Stack and Local Variables C and the MSP 430 CPE 323 Intro 2 Embedded. Systems 13
The Case for Subroutines: An Example n Problem n n n Sum up elements of two integer arrays Display results on P 2 OUT&P 1 OUT and P 4 OUT&P 3 OUT Example n n n arr 1 DC 16 arr 2 DC 16 Results n n 1, 2, 3, 4, 1, 2, 3, 4 1, 1, -1, -1 ; the first array ; the second array P 2 OUT&P 1 OUT=0 x 000 A, P 4 OUT&P 3 OUT=0 x 0001 Approach n n Input numbers: arrays Main program (no subroutines): initialization, program loops CPE 323 Intro 2 Embedded. Systems 14
Sum Up Two Integer Arrays (ver 1) /*---------------------------------------* Program : Find a sum of two integer arrays; * Input : The input arrays are signed 16 -bit integers in arr 1 and arr 2 * Output : Display sum of arr 1 on P 1 OUT&P 2 OUT and sum of arr 2 on P 3 OUT&P 4 OUT * Modified by: A. Milenkovic, milenkovic@computer. org * Date : September 14, 2008 * Description: MSP 430 IAR EW; Demonstration of the MSP 430 assembler *---------------------------------------*/ #include "msp 430. h" init: ; #define controlled include file NAME main ; module name PUBLIC main ; make the main label visible ; outside this module ORG DC 16 0 FFFEh init ; set reset vector to 'init' label RSEG CSTACK CODE ; pre-declaration of segment ; place program in 'CODE' segment MOV #SFE(CSTACK), SP ; set up stack CPE 323 Intro 2 Embedded. Systems 15
Sum up two integer arrays (ver 1) main: NOP MOV. W BIS. B #WDTPW+WDTHOLD, &WDTCTL #0 x. FF, &P 1 DIR #0 x. FF, &P 2 DIR #0 x. FF, &P 3 DIR #0 x. FF, &P 4 DIR MOV #arr 1, R 4 Sum arr 1 and display CLR R 7 MOV #8, R 10 lnext 1: ADD @R 4+, R 7 DEC R 10 JNZ lnext 1 MOV. B R 7, P 1 OUT SWPB R 7 MOV. B R 7, P 2 OUT ; Sum arr 2 and display MOV #arr 2, R 4 CLR R 7 MOV #7, R 10 lnext 2: ADD @R 4+, R 7 DEC R 10 JNZ lnext 2 MOV. B R 7, P 3 OUT SWPB R 7 MOV. B R 7, P 4 OUT ; JMP arr 1 arr 2 DC 16 ; ; ; main program Stop watchdog timer configure P 1. x as output configure P 2. x as output configure P 3. x as output configure P 4. x as output ; load the starting address of the array 1 into the register R 4 ; Holds the sum ; number of elements in arr 1 ; get next element ; display sum of arr 1 ; Holds the sum ; number of elements in arr 2 ; get next element ; display sum of arr 1 $ 1, 2, 3, 4, 1, 2, 3, 4 1, 1, -1, -1 ; the first array ; the second array END CPE 323 Intro 2 Embedded. Systems 16
Subroutines n n n A particular sub-task is performed many times on different data values Frequently used subtasks are known as subroutines Subroutines: How do they work? n n n Only one copy of the instructions that constitute the subroutine is placed in memory Any program that requires the use of the subroutine simply branches to its starting location in memory Upon completion of the task in the subroutine, the execution continues at the next instruction in the calling program CPE 323 Intro 2 Embedded. Systems 17
Subroutines (cont’d) n n CALL instructions: perform the branch to subroutines RETURN instruction: the last instruction in the subroutine CPE 323 Intro 2 Embedded. Systems 18
Subroutine Nesting CPE 323 Intro 2 Embedded. Systems 19
Mechanisms for Passing Parameters n n Through registers Through stack n By value n n n Actual parameter is transferred If the parameter is modified by the subroutine, the “new value” does not affect the “old value” By reference n n n The address of the parameter is passed There is only one copy of parameter If parameter is modified, it is modified globally CPE 323 Intro 2 Embedded. Systems 20
Subroutine: SUMA_RP n n Subroutine for summing up elements of an integer array Passing parameters through registers n n n R 12 - starting address of the array R 13 - array length R 14 - display id (0 for P 2&P 1, 1 for P 4&P 3) CPE 323 Intro 2 Embedded. Systems 21
Subroutine: SUMA_RP /*---------------------------------------* Program : Subroutine for that sums up elements of an integer array * Input : The input parameters are passed through registers: R 12 - starting address of the array R 13 - array length R 14 - display id (0 for P 2&P 1, 1 for P 4&P 3) * Output : No output parameters *---------------------------------------*/ #include "msp 430. h" ; #define controlled include file PUBLIC suma_rp RSEG CODE suma_rp: lnext: lp 34: lend: ; save the registers on the stack PUSH R 7 ; temporal sum CLR R 7 ADD @R 12+, R 7 DEC R 13 JNZ lnext BIT #1, R 14 ; display on P 1&P 2 JNZ lp 34 ; it's P 3&P 4 MOV. B R 7, P 1 OUT SWPB R 7 MOV. B R 7, P 2 OUT JMP lend MOV. B R 7, P 3 OUT SWPB R 7 MOV. B R 7, P 4 OUT POP R 7 ; restore R 7 RET END CPE 323 Intro 2 Embedded. Systems 22
Sum Up Two Integer Arrays (ver 2) /*---------------------------------------* Program : Find a sum of two integer arrays using a subroutine (suma_rp. s 43) * Input : The input arrays are signed 16 -bit integers in arr 1 and arr 2 * Output : Display sum of arr 1 on P 1 OUT&P 2 OUT and sum of arr 2 on P 3 OUT&P 4 OUT * Modified by: A. Milenkovic, milenkovic@computer. org * Date : September 14, 2008 * Description: MSP 430 IAR EW; Demonstration of the MSP 430 assembler *---------------------------------------*/ #include "msp 430. h" init: ; #define controlled include file NAME main ; module name PUBLIC main ; make the main label visible ; outside this module EXTERN suma_rp ORG DC 16 0 FFFEh init ; set reset vector to 'init' label RSEG CSTACK CODE ; pre-declaration of segment ; place program in 'CODE' segment MOV #SFE(CSTACK), SP ; set up stack CPE 323 Intro 2 Embedded. Systems 23
Sum Up Two Integer Arrays (ver 2) main: arr 1 arr 2 NOP MOV. W BIS. B #WDTPW+WDTHOLD, &WDTCTL #0 x. FF, &P 1 DIR #0 x. FF, &P 2 DIR #0 x. FF, &P 3 DIR #0 x. FF, &P 4 DIR MOV MOV CALL #arr 1, R 12 #8, R 13 #0, R 14 #suma_rp ; put address into R 12 ; put array length into R 13 ; display #0 (P 1&P 2) MOV MOV CALL JMP #arr 2, R 12 #7, R 13 #1, R 14 #suma_rp $ ; put address into R 12 ; put array length into R 13 ; display #0 (P 3&P 4) DC 16 1, 2, 3, 4, 1, 2, 3, 4 1, 1, -1, -1 ; ; ; main program Stop watchdog timer configure P 1. x as output configure P 2. x as output configure P 3. x as output configure P 4. x as output ; the first array ; the second array END CPE 323 Intro 2 Embedded. Systems 24
Subroutine: SUMA_SP n n Subroutine for summing up elements of an integer array Passing parameters through the stack n The calling program prepares input parameters on the stack CPE 323 Intro 2 Embedded. Systems 25
Subroutine: SUMA_SP /*---------------------------------------* Program : Subroutine for that sums up elements of an integer array * Input : The input parameters are passed through the stack: starting address of the array length display id * Output : No output parameters *---------------------------------------*/ #include "msp 430. h" ; #define controlled include file PUBLIC suma_sp RSEG CODE suma_sp: lnext: lp 34: lend: ; save the registers on the stack PUSH R 7 ; temporal sum PUSH R 6 ; array length PUSH R 4 ; pointer to array CLR R 7 MOV 10(SP), R 6 ; retrieve array length MOV 12(SP), R 4 ADD @R 4+, R 7 DEC R 6 JNZ lnext MOV 8(SP), R 4 ; get id from the stack BIT #1, R 4 ; display on P 1&P 2 JNZ lp 34 ; it's P 3&P 4 MOV. B R 7, P 1 OUT SWPB R 7 MOV. B R 7, P 2 OUT JMP lend MOV. B R 7, P 3 OUT SWPB R 7 MOV. B R 7, P 4 OUT POP R 4 ; restore R 4 POP R 6 POP R 7 RET END CPE 323 Intro 2 Embedded. Systems 26
Sum Up Two Integer Arrays (ver 3) *---------------------------------------* Program : Find a sum of two integer arrays * Input : The input arrays are signed 16 -bit integers in arr 1 and arr 2 * Output : Display sum of arr 1 on P 1 OUT&P 2 OUT and sum of arr 2 on P 3 OUT&P 4 OUT * Modified by: A. Milenkovic, milenkovic@computer. org * Date : September 14, 2008 * Description: MSP 430 IAR EW; Demonstration of the MSP 430 assembler *---------------------------------------*/ #include "msp 430. h" init: ; #define controlled include file NAME main ; module name PUBLIC main ; make the main label visible ; outside this module EXTERN suma_sp ORG DC 16 0 FFFEh init ; set reset vector to 'init' label RSEG CSTACK CODE ; pre-declaration of segment ; place program in 'CODE' segment MOV #SFE(CSTACK), SP ; set up stack CPE 323 Intro 2 Embedded. Systems 27
Sum Up Two Integer Arrays (ver 3) main: arr 1 arr 2 NOP MOV. W BIS. B #WDTPW+WDTHOLD, &WDTCTL #0 x. FF, &P 1 DIR #0 x. FF, &P 2 DIR #0 x. FF, &P 3 DIR #0 x. FF, &P 4 DIR PUSH CALL ADD #arr 1 #8 #0 #suma_sp #6, SP ; push the address of arr 1 ; push the number of elements ; push display id PUSH CALL ADD #arr 2 #7 #1 #suma_sp #6, SP ; push the address of arr 1 ; push the number of elements ; push display id JMP $ DC 16 ; ; ; main program Stop watchdog timer configure P 1. x as output configure P 2. x as output configure P 3. x as output configure P 4. x as output ; collapse the stack 1, 2, 3, 4, 1, 2, 3, 4 1, 1, -1, -1 ; the first array ; the second array END CPE 323 Intro 2 Embedded. Systems 28
The Stack and Local Variables n n Subroutines often need local workspace We can use a fixed block of memory space – static allocation – but: n n The code will not be relocatable The code will not be reentrant The code will not be able to be called recursively Better solution: dynamic allocation n Allocate all local variables on the stack STACK FRAME = a block of memory allocated by a subroutine to be used for local variables FRAME POINTER = an address register used to point to the stack frame CPE 323 Intro 2 Embedded. Systems 29
Subroutine: SUMA_SPSF /*---------------------------------------* Program : Subroutine for that sums up elements of an interger array * Subroutine variables are allocated on the stack frame * counter (SFP+2) * sum (SFP+4) * Input : The input parameters are passed through the stack: starting address of the array length display id * Output : No output parameters * *---------------------------------------*/ #include "msp 430. h" ; #define controlled include file PUBLIC suma_sp RSEG CODE suma_sp: ; save the registers on the stack PUSH R 12 ; save R 12 - R 12 is stack frame pointer MOV SP, R 12 ; R 12 points on the bottom of the stack frame SUB #4, SP ; allocate 4 bytes for local varaibles PUSH R 4 ; pointer register CLR -4(R 12) ; clear sum, sum=0 MOV 6(R 12), -2(R 12) ; init count MOV 8(R 12), R 4 ; R 4 points to the array starting address lnext: lp 34: lend: ADD DEC JNZ BIT JNZ MOV. B JMP MOV. B POP ADD POP RET END @R 4+, -4(R 12) -2(R 12) lnext #1, 4(R 12) lp 34 -4(R 12), P 1 OUT -3(R 12), P 2 OUT lend -4(R 12), P 3 OUT -3(R 12), P 4 OUT R 4 #4, SP R 12 ; add next element ; decrement counter ; get id from the stack ; it's P 3&P 4 ; restore R 4 ; colapse the stack frame ; restore stack frame pointer CPE 323 Intro 2 Embedded. Systems 30
Sum Up Two Integer Arrays (ver 4) /*---------------------------------------* Program : Find a sum of two integer arrays * Input : The input arrays are signed 16 -bit integers in arr 1 and arr 2 * Output : Display sum of arr 1 on P 1 OUT&P 2 OUT and sum of arr 2 on P 3 OUT&P 4 OUT * Modified by: A. Milenkovic, milenkovic@computer. org * Date : September 14, 2008 * Description: MSP 430 IAR EW; Demonstration of the MSP 430 assembler *---------------------------------------*/ #include "msp 430. h" init: ; #define controlled include file NAME main ; module name PUBLIC main ; make the main label visible ; outside this module EXTERN suma_sp ORG DC 16 0 FFFEh init ; set reset vector to 'init' label RSEG CSTACK CODE ; pre-declaration of segment ; place program in 'CODE' segment MOV #SFE(CSTACK), SP ; set up stack CPE 323 Intro 2 Embedded. Systems 31
Sum Up Two Integer Arrays (ver 3) main: arr 1 arr 2 NOP MOV. W BIS. B #WDTPW+WDTHOLD, &WDTCTL #0 x. FF, &P 1 DIR #0 x. FF, &P 2 DIR #0 x. FF, &P 3 DIR #0 x. FF, &P 4 DIR PUSH CALL ADD #arr 1 #8 #0 #suma_sp #6, SP ; push the address of arr 1 ; push the number of elements ; push display id PUSH CALL ADD #arr 2 #7 #1 #suma_sp #6, SP ; push the address of arr 1 ; push the number of elements ; push display id JMP $ DC 16 ; ; ; main program Stop watchdog timer configure P 1. x as output configure P 2. x as output configure P 3. x as output configure P 4. x as output ; collapse the stack 1, 2, 3, 4, 1, 2, 3, 4 1, 1, -1, -1 ; the first array ; the second array END CPE 323 Intro 2 Embedded. Systems 32
Outline n Assembly Language Programming n n n Subroutines n CALL&RETURN Subroutine Nesting Passing parameters n Stack and Local Variables n n n Adding two 32 -bit numbers (decimal, integers) Counting characters ‘E’ C and the MSP 430 CPE 323 Intro 2 Embedded. Systems 33
Assembly Language and C n We are interested in: n n n How a high-level language uses low-level language features? C: System programming, device drivers, … Use of addressing modes by compilers Parameter passing in assembly language Local storage CPE 323 Intro 2 Embedded. Systems 34
C and the MSP 430 n n n Compiler and the MSP 430 instruction set C data types and implementation Storage classes Functions and parameters Pointers CPE 323 Intro 2 Embedded. Systems 35
Compiling a C Program: Example #1 #include "io 430. h" int main( void ) { int i 1, i 2; unsigned int ui 1; short int sint 1; long int lint 2; int a[4]; // Stop watchdog timer to prevent time out reset WDTCTL = WDTPW + WDTHOLD; i 1 = 2; i 2 = -2; ui 1=65535; sint 1=127; lint 2=128243; a[0]=20; a[1]=9; return 0; } CPE 323 Intro 2 Embedded. Systems 36
Example #1 Compiler Generated List File (no optimization) ######################################## # IAR MSP 430 C/C++ Compiler V 4. 11 C/W 32 [ Kickstart] 21/Sep/2008 20: 24: 33 # # Copyright 1996 -2008 IAR Systems. All rights reserved. # # __rt_version = 3 # # __double_size = 32 # # __reg_r 4 = free # # __reg_r 5 = free # # __pic = no # # __core = 430 # # Source file = C: Documents and SettingsAleksandarMy # # DocumentsWorkteachingcpe 323 -08 Ftutorialtest_dtypes # #. c # # Command line = "C: Documents and SettingsAleksandarMy # # DocumentsWorkteachingcpe 323 -08 Ftutorialtest_dtypes # #. c" - l. C "C: Documents and SettingsAleksandarMy # # DocumentsWorkteachingcpe 323 -08 FtutorialDebugList # # " -o "C: Documents and SettingsAleksandarMy # # DocumentsWorkteachingcpe 323 -08 FtutorialDebugObj" # # -- no_cse --no_unroll --no_inline --no_code_motion # # -- no_tbaa --debug -D__MSP 430 F 149__ -e --double=32 -I # # "C: Program FilesIAR SystemsEmbedded Workbench # # 5. 0430INC" - Ol --multiplier=16 # # List file = C: Documents and SettingsAleksandarMy # # DocumentsWorkteachingcpe 323 -08 FtutorialDebugList # # test_dtypes. lst # # Object file = C: Documents and SettingsAleksandarMy # # DocumentsWorkteachingcpe 323 -08 FtutorialDebugObjt # # est_dtypes. r 43 # # ######################################## CPE 323 Intro 2 Embedded. Systems 37
Example #1 Compiler Generated List File (no optimization) 1 #include "io 430. h" In segment DATA 16_AN, at 0 x 120 union <unnamed> volatile __data 16 _A_WDTCTL: 000000 DS 8 2 In segment CODE, align 2 2 int main( void ) { main: 000000 0 A 12 PUSH. W R 10 000002 0812 PUSH. W R 8 000004 0912 PUSH. W R 9 000006 3182 SUB. W #0 x 8, SP 3 int i 1, i 2; ^ Warning[Pe 550]: variable "i 1" was set but never used int i 1, i 2; ^ "C: Documents and SettingsAleksandarMy DocumentsWorkteachingcpe 32308 Ftutorialtest_dtypes. c", 3 Warning[Pe 550]: variable "i 2" was set but never used 4 unsigned int ui 1; ^ Warning[Pe 550]: variable "ui 1" was set but never used 5 short int sint 1; ^ Warning[Pe 550]: variable "sint 1" was set but never used 6 long int lint 2; ^ Warning[Pe 550]: variable "lint 2" was set but never used 7 int a[4]; ^ Warning[Pe 550]: variable CPE "a" 323 was Intro 2 Embedded. Systems set but never used 38
Example #1 Compiler Generated List File (no optimization) 8 9 000008 10 00000 E 000010 11 000014 12 000016 13 00001 A 00001 E 14 000020 000026 15 16 00002 C 00002 E 000030 000032 000034 000036 000038 // Stop watchdog timer to prevent time out reset WDTCTL = WDTPW + WDTHOLD; B 240805 A 2001 MOV. W #0 x 5 a 80, &0 x 120 i 1 = 2; i 2 = -2; 2 F 43 MOV. W #0 x 2, R 15 3 E 40 FEFF MOV. W #0 xfffe, R 14 ui 1=65535; 3 D 43 MOV. W #0 xffff, R 13 sint 1=127; 3 A 407 F 00 MOV. W #0 x 7 f, R 10 Maximum stack usage in bytes: lint 2=128243; 3840 F 3 F 4 MOV. W #0 xf 4 f 3, R 8 Function CSTACK 1943 MOV. W #0 x 1, R 9 -----a[0]=20; a[1]=9; main 16 B 14014000000 MOV. W #0 x 14, 0(SP) B 14009000200 MOV. W #0 x 9, 0 x 2(SP) return 0; Segment part sizes: 0 C 43 MOV. W #0 x 0, R 12 3152 ADD. W #0 x 8, SP Function/Label Bytes 3941 POP. W R 9 -------3841 POP. W R 8 _A_WDTCTL 2 3 A 41 POP. W R 10 main 56 3041 RET REQUIRE _A_WDTCTL } 56 bytes in segment CODE 2 bytes in segment DATA 16_AN 56 bytes of CODE memory 0 bytes of DATA memory (+ 2 bytes shared) Errors: none Warnings: 6 CPE 323 Intro 2 Embedded. Systems 39
C Data Types Data type bool char signed char unsigned char signed short unsigned short signed int unsigned int signed long unsigned long Size 8 bits 16 bits 32 bits 64 bits float double 32 bits 64 bits Range Alignment 0 to 1 1 to 255 1 -128 to 127 1 0 to 255 1 -32768 to 32767 2 0 to 65535 2 -231 to 231 -1 2 32 0 to 2 -1 2 63 63 -2 to 2 -1 2 0 to 264 -1 2 2 2 (*) CPE 323 Intro 2 Embedded. Systems 40
C Data Types, cont’d n Local variables n n Global variables n n n Defined inside a function Cannot be accessed from outside the function Normally lost when a return from the function is made Defined outside a function Can be accessed both from inside and outside the function Variables defined in a block exist only within that block int i; /*global variable, visible to everything from this point*/ void function_1(void) /*A function with no parameters*/ { int k; /*Integer k is local to function_1*/ { int q; /*Integer q exists only in this block*/ int j; /*Integer j is local and not the same as j in main*/ } } void main(void) { int j; /*Integer j is local to this block within function main*/ } /*This is the point at which integer j ceases to exist*/ CPE 323 Intro 2 Embedded. Systems 41
Storage Class Specifiers n auto n n register n n Ask compiler to allocate the variable to a register Also is automatic Cannot be accessed by means of pointers static n n n Variable is no longer required once a block has been left; Default Allows local variable to retain its value when a block is reentered Initialized only once, by the compiler! extern n n Indicates that the variable is defined outside the block The same global variable can be defined in more than one module CPE 323 Intro 2 Embedded. Systems 42
Storage Class Modifiers n volatile n n const n n n To define variables that can be changed externally Compiler will not put them in registers Think about Status Registers ! Variable may not be changed during the execution of a program Cannot be changed unintentionally, but CAN be changed externally (as a result of an I/O, or OS operations external to the C program) Type conversion n In C, done either automatically or explicitly (casting) CPE 323 Intro 2 Embedded. Systems 43
Compiling a C Program: Example #2 #include "io 430. h" int main( void ) { volatile int i 1, i 2; volatile unsigned int ui 1; volatile short int sint 1; volatile long int lint 2; volatile int a[4]; // Stop watchdog timer to prevent time out reset WDTCTL = WDTPW + WDTHOLD; i 1 = 2; i 2 = -2; ui 1=65535; sint 1=127; lint 2=128243; a[0]=20; a[1]=9; return 0; } CPE 323 Intro 2 Embedded. Systems 44
Example #2 Compiler Generated List File (no optimization) C: Documents and SettingsAleksandarMy DocumentsWorkteachingcpe 32308 Ftutorialtest_dtypes. c 1 #include "io 430. h" In segment DATA 16_AN, at 0 x 120 union <unnamed> volatile __data 16 _A_WDTCTL: 000000 DS 8 2 In 2 000000 3 4 5 6 7 8 9 000004 10 00000 A 00000 E 11 000014 12 000018 13 00001 E 000024 segment CODE, align 2 int main( void ) { main: 31801400 SUB. W #0 x 14, SP volatile int i 1, i 2; volatile unsigned int ui 1; volatile short int sint 1; volatile long int lint 2; volatile int a[4]; // Stop watchdog timer to prevent time out reset WDTCTL = WDTPW + WDTHOLD; B 240805 A 2001 MOV. W #0 x 5 a 80, &0 x 120 i 1 = 2; i 2 = -2; A 1430000 MOV. W #0 x 2, 0(SP) B 140 FEFF 0200 MOV. W #0 xfffe, 0 x 2(SP) ui 1=65535; B 1430400 MOV. W #0 xffff, 0 x 4(SP) sint 1=127; B 1407 F 000600 MOV. W #0 x 7 f, 0 x 6(SP) lint 2=128243; B 140 F 3 F 40800 MOV. W #0 xf 4 f 3, 0 x 8(SP) 91430 A 00 MOV. W #0 x 1, 0 xa(SP) CPE 323 Intro 2 Embedded. Systems 45
Example #2 Compiler Generated List File (no optimization) 14 000028 00002 E a[0]=20; a[1]=9; B 14014000 C 00 MOV. W B 14009000 E 00 MOV. W return 0; 0 C 43 MOV. W 31501400 ADD. W 3041 RET REQUIRE } usage in bytes: 15 000034 000036 00003 A 00003 C 16 Maximum stack #0 x 14, 0 xc(SP) #0 x 9, 0 xe(SP) #0 x 0, R 12 #0 x 14, SP _A_WDTCTL Function CSTACK -----main 22 Segment part sizes: Function/Label Bytes -------_A_WDTCTL 2 main 60 60 bytes in segment CODE 2 bytes in segment DATA 16_AN 60 bytes of CODE memory 0 bytes of DATA memory (+ 2 bytes shared) Errors: none Warnings: none CPE 323 Intro 2 Embedded. Systems 46
Factorial #include "stdio. h" #include "io 430. h" int fact(int n); int main(void) { int n = 5; int nf; nf = fact(n); printf("n=%d, nf=%dn", n, nf); } return 0; int fact(int n) { } if(n>1) return n*fact(n-1); else return 1; CPE 323 Intro 2 Embedded. Systems 47
Factorial: List File 1 2 4 # include " stdio. h" #include "io 430. h" int fact(int n); 6 0000002 int main(void) { main: 0 A 12 PUSH. W 0 B 12 PUSH. W 000004 int n = 5; 3 A 400500 MOV. W 7 8 9 10 11 12 In segment CODE, align 2 R 10 R 11 #0 x 5, R 10 int nf; 000008 00000 A 00000 E nf = fact(n); 0 C 4 A MOV. W B 012. . CALL 0 B 4 C MOV. W 000010 000012 000014 000018 printf("n=%d, nf=%dn", n, nf); 0 B 12 PUSH. W R 11 0 A 12 PUSH. W R 10 3 C 40. . MOV. W #`? <Constant "n=%d, nf=%d\n">`, R 12 B 012. . CALL # printf 13 14 15 16 17 00001 C 00001 E 000020 000022 000024 return 0; 0 C 43 MOV. W 2152 ADD. W 3 B 41 POP. W 3 A 41 POP. W 3041 RET } R 10, R 12 #fact R 12, R 11 #0 x 0, R 12 #0 x 4, SP R 11 R 10 CPE 323 Intro 2 Embedded. Systems 48
Factorial: List File 19 0000002 20 21 000004 000006 000008 00000 A 00000 C 000010 000012 000014 000018 00001 C 000020 000022 22 000024 000026 000028 23 sorted 000000 int fact(int n) { fact: 0 A 12 PUSH. W 0 A 4 C MOV. W R 10 R 12, R 10 if(n>1) return n*fact(n-1); 2 A 93 CMP. W #0 x 2, R 10 0 E 38 JL ? ? fact_0 0 C 4 A MOV. W R 10, R 12 3 C 53 ADD. W #0 xffff, R 12 B 012. . CALL #fact 0212 PUSH. W SR 32 C 2 DINT 824 A 3001 MOV. W R 10, &0 x 130 824 C 3801 MOV. W R 12, &0 x 138 1 C 423 A 01 MOV. W &0 x 13 a, R 12 3241 POP. W SR 013 C JMP ? ? fact_1 else return 1; ? ? fact_0: 1 C 43 MOV. W #0 x 1, R 12 ? ? fact_1: 3 A 41 POP. W R 10 3041 RET } In segment DATA 16_C, align 1, align- `? <Constant "n=%d, nf=%d\n">`: 6 E 3 D 25642 C 20 DC 8 "n=%d, nf=%d 12" 6 E 663 D 25640 A 00 CPE 323 Intro 2 Embedded. Systems 49
Functions and Parameters #include "io 430. h" void swapbyv(int a, int b); void swapbyr(int *a, int *b); int main( void ) { // Stop watchdog timer to prevent time out reset WDTCTL = WDTPW + WDTHOLD; int x = 5; int y = 6; // pass parameters by value swapbyv(x, y); // pass parameters by reference swapbyr(&x, &y); } void swapbyv(int a, int b) { int temp; temp = a; a = b; b = temp; } void swapbyr(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } return 0; CPE 323 Intro 2 Embedded. Systems 50
Functions and Parameters 8 int main( void ) main: 9 { 000000 2182 SUB. W #0 x 4, SP 10 // Stop watchdog timer to prevent time out reset 11 WDTCTL = WDTPW + WDTHOLD; 000002 B 240805 A 2001 MOV. W #0 x 5 a 80, &0 x 120 12 13 int x = 5; 000008 B 14005000200 MOV. W #0 x 5, 0 x 2(SP) 14 int y = 6; 00000 E B 14006000000 MOV. W #0 x 6, 0(SP) 19 24 29 30 000014 000016 00001 A swapbyv(x, y); 2 D 41 MOV. W 1 C 410200 MOV. W B 012. . CALL @SP, R 13 0 x 2(SP), R 12 # swapbyv 00001 E 000020 000022 000024 swapbyr(&x, &y); 0 D 41 MOV. W 0 C 41 MOV. W 2 C 53 ADD. W B 012. . CALL SP, R 13 SP, R 12 #0 x 2, R 12 # swapbyr 000028 00002 A 00002 C 00002 E return 0; 0 C 43 MOV. W #0 x 0, R 12 2152 ADD. W #0 x 4, SP 3041 RET REQUIRE _A_WDTCTL } CPE 323 Intro 2 Embedded. Systems 51
Functions and Parameters Maximum stack usage in bytes: align 2 32 33 34 35 000000 36 000002 37 000004 38 000006 39 align 2 40 41 42 43 000000 44 000002 45 000006 46 00000 A In segment CODE, Function CSTACK ------main 6 -> swapbyv 6 -> swapbyr 6 swapbyr 2 swapbyv 2 void swapbyv(int a, int b) { swapbyv: int temp; temp = a; 0 F 4 C a = b; 0 C 4 D b = temp; 0 D 4 F } 3041 MOV. W R 12, R 15 MOV. W R 13, R 12 MOV. W R 15, R 13 Segment part sizes: Function/Label Bytes -------_A_WDTCTL 2 main 46 swapbyv 8 swapbyr 12 RET In segment CODE, void swapbyr(int *a, int *b) { swapbyr: int temp; temp = *a; 2 F 4 C MOV. W *a = *b; AC 4 D 0000 MOV. W *b = temp; 8 D 4 F 0000 MOV. W } 3041 RET @R 12, R 15 66 bytes in segment CODE 2 bytes in segment DATA 16_AN 66 bytes of CODE memory 0 bytes of DATA memory (+ 2 bytes shared) @R 13, 0(R 12) R 15, 0(R 13) CPE 323 Intro 2 Embedded. Systems 52
Pointers and C #include "io 430. h" #include "stdio. h" int main( void ) { // Stop watchdog timer to prevent time out reset WDTCTL = WDTPW + WDTHOLD; int x = 5; // an integer x int *p_x; // a pointer to int y 1; // an integer y 1 (uninitialized) long int y 2, y 3; // long integers y 2, y 3 long int *p_y 2; // a pointer to long integer char mya[20] = "hello world, cpe 323!"; // character array char *p_mya; // pointer to character p_x = &x; // y 1 = 10 + x; // y 2 = -1; p_y 2 = &y 2; // y 3 = 10 + *p_y 2; p_mya = mya; // p_mya = p_mya + 3; } p_x points to x new value to y 1 pointer p_y 2 points to y 2 p_mya points to array mya // display addresses and variables in terminal i/o printf("a. x=%x, x=%xn", &x, x); printf("a. p_x=%x, p_x=%xn", &p_x, p_x); printf("a. y 1=%x, y 1=%xn", &y 1, y 1); printf("a. y 2=%x, y 2=%lxn", &y 2, y 2); printf("a. y 3=%x, y 3=%lxn", &y 3, y 3); printf("a. p_y 2=%x, p_y 2=% xn", &p_y 2, p_y 2); printf("a. mya=%x, mya=%sn", &mya, mya); printf("a. p_mya=%x, p_mya=%xn", &p_mya, p_mya); return 0; CPE 323 Intro 2 Embedded. Systems 53
Pointers and C, cont’d 1 #include "io 430. h" In segment DATA 16_AN, at 0 x 120 union <unnamed> volatile __data 16 _A_WDTCTL: 000000 DS 8 2 2 #include "stdio. h" 3 4 000000 5 6 000004 7 00000 A 8 9 10 11 12 13 14 15 000010 000012 000016 00001 A 00001 E 000022 000024 In segment CODE, align 2 int main(void) { main: 31802600 SUB. W #0 x 26, SP // Stop watchdog timer to prevent time out reset WDTCTL = WDTPW + WDTHOLD; B 240805 A 2001 MOV. W #0 x 5 a 80, &0 x 120 int x = 5; // an integer x B 14005000000 MOV. W #0 x 5, 0(SP) int *p_x; // a pointer to int y 1; // an integer y 1 (uninitialized) long int y 2, y 3; // long integers y 2, y 3 long int *p_y 2; // a pointer to long integer char mya[20] = "hello world, cpe 323!"; // character array 0 C 41 MOV. W SP, R 12 3 C 501200 ADD. W #0 x 12, R 12 3 E 40. . MOV. W #`? <Constant "hello world, cpe 323!">`, R 14 3 D 401400 MOV. W #0 x 14, R 13 B 012. . CALL #? Copy. Memory. Bytes char *p_mya; // pointer to character p_x = &x; 0 F 41 MOV. W 814 F 0800 MOV. W // p_x points to x SP, R 15, 0 x 8(SP) CPE 323 Intro 2 Embedded. Systems 54
Pointers and C, cont’d 16 000028 00002 A 00002 E 17 000032 000036 18 00003 A 00003 C 000040 19 000044 000048 00004 A 00004 E 000052 000054 000058 20 21 00005 C 00005 E 000062 000066 y 1 = 10 + x; // new value to y 1 2 F 41 MOV. W @SP, R 15 3 F 500 A 00 ADD. W #0 xa, R 15 814 F 0600 MOV. W R 15, 0 x 6(SP) y 2 = -1; B 1430 A 00 MOV. W #0 xffff, 0 xa(SP) B 1430 C 00 MOV. W #0 xffff, 0 xc(SP) p_y 2 = &y 2; // pointer p_y 2 points to y 2 0 F 41 MOV. W SP, R 15 3 F 500 A 00 ADD. W #0 xa, R 15 814 F 0400 MOV. W R 15, 0 x 4(SP) y 3 = 10 + *p_y 2; 1 F 410400 MOV. W 0 x 4(SP), R 15 2 E 4 F MOV. W @R 15, R 14 1 F 4 F 0200 MOV. W 0 x 2(R 15), R 15 3 E 500 A 00 ADD. W #0 xa, R 14 0 F 63 ADDC. W #0 x 0, R 15 814 E 0 E 00 MOV. W R 14, 0 xe(SP) 814 F 1000 MOV. W R 15, 0 x 10(SP) p_mya = mya; // p_mya points to array mya 0 F 41 MOV. W SP, R 15 3 F 501200 ADD. W #0 x 12, R 15 814 F 0200 MOV. W R 15, 0 x 2(SP) p_mya = p_mya + 3; B 15003000200 ADD. W #0 x 3, 0 x 2(SP) CPE 323 Intro 2 Embedded. Systems 55
Speed and Performance of Microprocessors n Why is difficult to compare the speed of two microprocessors? n n n Performance Execution time MIPS: Million of Instructions Per Second Carefully interpret benchmarks! Clock Cycles/Bus Cycles CPE 323 Intro 2 Embedded. Systems 56
Speed and Performance of Microprocessors, cont’d #include "msp 430. h" NAME main PUBLIC main init: main: lnext: arr 1 aend ORG DC 16 RSEG MOV NOP MOV. W PUSH MOV MOV SUB DEC MOV. B DEC JNZ JMP $ NOP DC 8 0 FFFEh init CSTACK CODE #SFE(CSTACK), SP #WDTPW+WDTHOLD, &WDTCTL R 14 SP, R 14 #aend, R 6, R 5 #arr 1, R 5, SP R 6 R 14 @R 6, 0(R 14) R 5 lnext ; ; #define controlled include file module name make the main label visible outside this module ; ; ; ; set reset vector to 'init' label pre-declaration of segment place program in 'CODE' segment set up stack main program Stop watchdog timer ; ; how many bytes is in allocate storage for decrement pointer to decrement pointer on R 14 points to the top of the stack the array on the stack arr 1 the stack 1, 2, 3, 4, 5, 6, 7, 8, 9 END CPE 323 Intro 2 Embedded. Systems 57
Speed and Performance of Microprocessors, cont’d #include "msp 430. h" NAME main PUBLIC main init: main: lnext: arr 1 aend ORG DC 16 RSEG MOV NOP MOV. W PUSH MOV MOV SUB DEC MOV. B DEC JNZ JMP $ DC 8 ; ; 0 FFFEh init CSTACK CODE #SFE(CSTACK), SP #WDTPW+WDTHOLD, &WDTCTL R 14 SP, R 14 #aend, R 6, R 5 #arr 1, R 5, SP R 6 R 14 @R 6, 0(R 14) R 5 lnext #define controlled include file module name make the main label vissible outside this module ; set reset vector to 'init' label ; pre-declaration of segment ; place program in 'CODE' segment ; 4 cc ; 1 cc ; 5 cc ; 3 cc (table 3. 15) ; 1 cc ; 2 cc ; 1 cc x 9 ; 4 cc x 9 ; 1 cc x 9 ; 2 cc x 9 1, 2, 3, 4, 5, 6, 7, 8, 9 END TOTAL NUMBER OF CLOCK CYLES: TOTAL NUMBER OF INSTRUCITONS CPI 4+1+5+3+1+2+1+9 x(1+1+4+1+2) = 20+9 x 9 = 101 cc 9+9 x 5 = 54 instructions 101/54 = 1. 87 cc/instruction CPE 323 Intro 2 Embedded. Systems 58
- Slides: 58