diff --git a/Assignment 4, CS 223 b/Assignment 4, CS 223 index 91ff3fcdbb2af35cba69cbe6e51da5cfa1c4e4c5..fb5f749926574e142ec347a5f09de2fc92da15e0 100644 --- a/Assignment 4, CS 223 +++ b/Assignment 4, CS 223 @@ -3,96 +3,40 @@ Fall 2016 You are provided two files: A main and a function in separate source files. The function is a simple character swap, using the necessary pointers to accomplish 'pass by reference'. It also returns an integer just to prove it can. - This assignment will have you use gdb on the msp430 to answer questions about the structure and progress of the program. You will fill in answers to questions about the program. Please create a pdf file with those answers filled in. This can be done within a text editor that has a 'save as' option to pdf or one can use some pdf creator such as 'enscript'. It is this final pdf which I wish you to push back to me. Please remove the original source files, and any concomitant files (Words of Power !) which might have been created. The purpose of this assignment is twofold: - Cure You of Insanity by 'encouraging' (forcing) you to use gdb - - Look at the MSP430 architecture and how a working program is organized on the processor. - -The main program is -#include <msp430.h> - -// a quick demo of pass by ref, pointers. -void Swap(int *, int *); // POINTERS !!! -int main(int argc, char * argv[]) - -{int A,B; - int *P1,P2;// two variables only one is a pointer - int *P3,*P4; // two variables both pointers -// Got to do this first - WDTCTL = WDTHOLD | WDTPW; - BCSCTL1 = CALBC1_1MHZ; - DCOCTL = CALDCO_1MHZ; -// Let's turn on an LED - P1DIR = 0b00000001; - P1OUT = 0b00000001; -// -A=42; -B= -454; -Z=swap(&A,&B;); -char * Str = "A String"; -char StrArray={'A,' ','S','t','r','i','n','g',(char) 0}; -Z=swap(&StrArray[2],&StrArray5]); - -} - -You are to answer the following questions, providing evidence via screen captures or the script program. You will be wise to have some documentation with you as you work through these questions. The header file <msp430.h> or the particular <msp430g2553.h> - -I ) First -- find the addresses of the following variables - -P1DIR - - -P1OUT - - -Str - - -"A String" - + 1. Cure You of Insanity by 'encouraging' (forcing) you to use gdb -StrArray - - -A - - -B + 2. Look at the MSP430 architecture and how a working program is organized on the processor. +You are to answer the following questions, providing evidence via screen captures or the script program. You will be wise to have some documentation with you as you work through these questions. The header file <msp430.h> or the particular <msp430g2553.h> -Temp +I) Find the addresses of the following variables: + 1. P1DIR + 2. P1OUT + 3. str + 4. "A String" + 5. str_array + 6. a + 7. b + 8. tmp + 9. ptr1 (inside swap.c) -P1 -II) -Find the address of the statement A = 42; +II) 1. Find the address of the statement `a = (char) 65`: (Do this by setting a breakpoint and looking at the messages or literally look at R0) +2. Find the address of the statement `tmp = *ptr1` + +III) Parameters are SENT in registers R15, R14, R13, R12 (in order of argument list) and function values are returned in R15. Prove that the 'parameter passing protocol' above is honored in statements -Find the address of the statement - -Temp = *P1; - -III) Parameters are SENT in registers R15, R14, R13, R12 (in order of argument list) and function values are returned in R15. - - -Prove that the 'parameter passing protocol' above is honored in statements - - -A) -A=42; -B= -454; -Z=swap(&A,&B;); - +1. i = swap(&a, &b); +2. i = swap(&str_array[2], &str_array[5]); -B) -Z=swap(&StrArray[2],&StrArray5]); \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..c4f275fc30a5a99ce481177b9657459247419a68 --- /dev/null +++ b/Makefile @@ -0,0 +1,41 @@ +# Project name +SOURCE = main.c +ADDITIONAL = swap.c +# Get base name so we can create .elf file +NAME = $(basename $(SOURCE)) +# MSP430 MCU to compile for +CPU = msp430g2553 +# Optimisation level +OPTIMIZATION = -O0 +# Extra variables +CFLAGS = -mmcu=$(CPU) $(OPTIMIZATION) -std=c99 -g -fomit-frame-pointer +# Libemb library link flags +LIBEMB = -lshell -lconio -lserial + +# Build and link executable +$(NAME).elf: $(SOURCE) $(ADDITIONAL) +ifeq (,$(findstring libemb,$(shell cat $(SOURCE)))) + msp430-gcc $(CFLAGS) -o $@ $(SOURCE) $(ADDITIONAL) +else + msp430-gcc $(CFLAGS) -o $@ $(SOURCE) $(ADDITIONAL) $(LIBEMB) +endif + msp430-objdump -D $(NAME).elf > $(NAME).hex + +# Flash to board with mspdebug +flash: $(NAME).elf + mspdebug rf2500 "prog $(NAME).elf" + +# Erase board +erase: + mspdebug rf2500 erase + +stuff: hworld.c + echo "I found hworld.c!" && cat hworld.c + +# Clean up temporary files +clean: + rm -f *.elf *.hex + +# Remote debug board +debug: $(NAME).elf + ( mspdebug rf2500 "gdb" 1>/dev/null & ); msp430-gdb $(NAME).elf -ex "target remote :2000" diff --git a/SwapMethod.c b/SwapMethod.c deleted file mode 100644 index 7da9ea89025b7ebdffb9bd291575a072662e44b0..0000000000000000000000000000000000000000 --- a/SwapMethod.c +++ /dev/null @@ -1,8 +0,0 @@ -int Swap( char * P1, char * P2) -{char Temp; - Temp = *P1; - *P1 = *P2; // dereference on left and right , tra la ! - *P2 = Temp; - Z=-42; - return(Z); -} diff --git a/main.c b/main.c new file mode 100644 index 0000000000000000000000000000000000000000..bb11005bfa2c35b14256a6a6ff114974933b75c4 --- /dev/null +++ b/main.c @@ -0,0 +1,30 @@ +#include <msp430.h> + +int swap(char *, char *); + +int main(void) { + + WDTCTL = WDTHOLD | WDTPW; + BCSCTL1 = CALBC1_1MHZ; + DCOCTL = CALDCO_1MHZ; + + P1DIR = 0b00000001; + P1OUT = 0b00000001; + + char a, b; + int i; + int *ptr1, not_ptr; + int *ptr3, *ptr4; + + a = (char) 65; + b = (char) 126; + i = swap(&a, &b); + + char *str = "A String"; + char str_array[9] = { 'A', ' ', 'S', 't', 'r', 'i', 'n', 'g', (char) 0 }; + + i = swap(&str_array[2], &str_array[5]); + + return 0; +} + diff --git a/swap.c b/swap.c index f20914f018c23c33f6f14dcfe31dec477c42449e..f2abeb0bf9bdfbe3b38c09bec6bafc39c369ba13 100644 --- a/swap.c +++ b/swap.c @@ -1,27 +1,12 @@ -#include <msp430.h> +int swap(char *ptr1, char *ptr2) { + int i; + char tmp; -// a quick demo of pass by ref, pointers. -int Swap(char *, char *); // POINTERS !!! -main() + tmp = *ptr1; + *ptr1 = *ptr2; + *ptr2 = tmp; -{char A,B;int Z; - int *P1,P2;// two variables only one is a pointer - int *P3,*P4; // two variables both pointers -// Got to do this first - WDTCTL = WDTHOLD | WDTPW; - BCSCTL1 = CALBC1_1MHZ; - DCOCTL = CALDCO_1MHZ; -// Let's turn on an LED - P1DIR = 0b00000001; - P1OUT = 0b00000001; -// -A=(char) 65; -B= (char) 126; - -Z =swap(&A,&B;); -char * Str = "A String"; -char StrArray={'A', ' ' ,'S','t','r','i','n','g',(char) 0}; -Z=swap(&StrArray[2],&StrArray5]); + i = -42; + return i; } -