26
Help for Help for Assignment 2 Assignment 2

Help for Assignment 2. Topics Tackled Handling some “C++” keywords when programming assembly code –Programming “subroutines called by subroutines” –Programming

Embed Size (px)

Citation preview

Help forHelp forAssignment 2Assignment 2

Topics TackledTopics Tackled

Handling some “C++” keywords when Handling some “C++” keywords when programming assembly codeprogramming assembly code– Programming “subroutines called by subroutines”Programming “subroutines called by subroutines”– Programming “extern” variablesProgramming “extern” variables– Programming “volatile” variablesProgramming “volatile” variables

Understanding what I did when Understanding what I did when – Programming “subroutines called by subroutines”Programming “subroutines called by subroutines”– Programming “extern” variablesProgramming “extern” variables– Programming “volatile” variablesProgramming “volatile” variables

Subroutine calling subroutineSubroutine calling subroutineAll void functionsAll void functions

File “bloodEnglishmanASM.asm”File “bloodEnglishmanASM.asm”

.extern _Fee__Fv;.extern _Fee__Fv;

.extern _Fi__Fv;.extern _Fi__Fv;

.extern _Fo__Fv;.extern _Fo__Fv;

.section program;.section program;

.global _Fum__Fv;.global _Fum__Fv;_Fum__Fv:_Fum__Fv: LINK 16;LINK 16;

CALL _Fee __Fv;CALL _Fee __Fv; CALL _Fi __Fv;CALL _Fi __Fv; CALL _Fo__Fv;CALL _Fo__Fv;

P0 = [FP +4];P0 = [FP +4]; UNLINK;UNLINK;_Fum__Fv.END:_Fum__Fv.END: JUMP (P0);JUMP (P0);

File File “bloodEnglishmanCPP.cpp”“bloodEnglishmanCPP.cpp”

void Fee(void);void Fee(void);void Fi(void);void Fi(void);void Fo(void);void Fo(void);void Fum(void);void Fum(void);

void Fum(void) {void Fum(void) { Fee( );Fee( ); Fi( );Fi( ); Fo( );Fo( );}}

What nursery rhyme? What nursery rhyme?

Fe, Fe,

Fi, Fi,

Fo, Fo,

Fum, Fum,

I smell the blood of an Englishman?I smell the blood of an Englishman?

Subroutine calling subroutineSubroutine calling subroutineAll integer functionsAll integer functions

File “bloodEnglishmanASM.asm”File “bloodEnglishmanASM.asm”

.extern _Fee__Fi;.extern _Fee__Fi;

.extern _Fi__Fi;.extern _Fi__Fi;

.extern _Fo__Fi;.extern _Fo__Fi;

.section program;.section program;

.global _Fum__Fi;.global _Fum__Fi;_Fum__Fi: // value passed in R0_Fum__Fi: // value passed in R0 LINK 16;LINK 16;

R0 += 6;R0 += 6; // pass parameter in R0 // pass parameter in R0 CALL _Fee __Fi;CALL _Fee __Fi;

R0 = 7; // pass parameter in R0R0 = 7; // pass parameter in R0 CALL _Fi __Fi;CALL _Fi __Fi;

R0 = 8; // pass parameter in R0R0 = 8; // pass parameter in R0 CALL _Fo__Fi;CALL _Fo__Fi;

P0 = [FP +4];P0 = [FP +4]; UNLINK;UNLINK;_Fum__Fi.END:_Fum__Fi.END: JUMP (P0);JUMP (P0);

File File “bloodEnglishmanCPP.cpp”“bloodEnglishmanCPP.cpp”

void Fee(int);void Fee(int);void Fi(int);void Fi(int);void Fo(int);void Fo(int);void Fum(int);void Fum(int);

void Fum(int void Fum(int valuevalue) {) { Fee(6 + value);Fee(6 + value); Fi(7);Fi(7); Fo(8);Fo(8);}}

Subroutine calling subroutineSubroutine calling subroutineAll integer functionsAll integer functions

.extern _Fee__Fi;.extern _Fee__Fi;

.extern _Fi__Fi;.extern _Fi__Fi;

.extern _Fo__Fi;.extern _Fo__Fi;

.section program;.section program;

.global _Fum__Fi;.global _Fum__Fi;_Fum__Fi: // value passed in R0_Fum__Fi: // value passed in R0 LINK 16;LINK 16; [--SP] = R0;[--SP] = R0; // Save passed value // Save passed value

R0 = 6; // pass parameter in R0R0 = 6; // pass parameter in R0 CALL _Fee __Fi;CALL _Fee __Fi;

R0 = 7; // pass parameter in R0R0 = 7; // pass parameter in R0 CALL _Fi __Fi;CALL _Fi __Fi;

R0 = [SP++];R0 = [SP++]; // Recover passed value // Recover passed value R0 += 8;R0 += 8; // pass parameter in R0 // pass parameter in R0 CALL _Fo__Fi;CALL _Fo__Fi;

P0 = [FP +4];P0 = [FP +4]; UNLINK;UNLINK;_Fum__Fi.END:_Fum__Fi.END: JUMP (P0);JUMP (P0);

File File “bloodEnglishmanCPP.cpp”“bloodEnglishmanCPP.cpp”

void Fee(int);void Fee(int);void Fi(int);void Fi(int);void Fo(int);void Fo(int);void Fum(int);void Fum(int);

void Fum(int void Fum(int valuevalue) {) { Fee(6);Fee(6); Fi(7);Fi(7); Fo(8 + value);Fo(8 + value);}}

Subroutine calling subroutineSubroutine calling subroutineAll integer functions -- BetterAll integer functions -- Better

.extern _Fee__Fi;.extern _Fee__Fi;

.extern _Fi__Fi;.extern _Fi__Fi;

.extern _Fo__Fi;.extern _Fo__Fi;

.section program;.section program;

.global _Fum__Fi;.global _Fum__Fi;_Fum__Fi: // value passed in R0_Fum__Fi: // value passed in R0 LINK 16;LINK 16; [FP + 8] = R0;[FP + 8] = R0; // Save passed value // Save passed value

R0 = 6; // pass parameter in R0R0 = 6; // pass parameter in R0 CALL _Fee __Fi;CALL _Fee __Fi;

R0 = 7; // pass parameter in R0R0 = 7; // pass parameter in R0 CALL _Fi __Fi;CALL _Fi __Fi;

R0 = [FP + 8];R0 = [FP + 8]; // Recover passed value // Recover passed value R0 += 8;R0 += 8; // pass parameter in R0 // pass parameter in R0 CALL _Fo__Fi;CALL _Fo__Fi;

P0 = [FP +4];P0 = [FP +4]; UNLINK;UNLINK;_Fum__Fi.END:_Fum__Fi.END: JUMP (P0);JUMP (P0);

File “bloodEnglishmanCPP.cpp”File “bloodEnglishmanCPP.cpp”

void Fee(int);void Fee(int);void Fi(int);void Fi(int);void Fo(int);void Fo(int);void Fum(int);void Fum(int);

void Fum(int void Fum(int valuevalue) {) { Fee(6);Fee(6); Fi(7);Fi(7); Fo(8 + value);Fo(8 + value);}}

extern long int extern long int ..extern _thumb; extern _thumb;

.section L1_cache;.section L1_cache;

.global _plumb[5] = {1, 2, 3, 4, 5};.global _plumb[5] = {1, 2, 3, 4, 5};

.section program;.section program;

.global _Jack__FPl; // Pointer long.global _Jack__FPl; // Pointer long_Jack__FPl: // pointer passed in R0_Jack__FPl: // pointer passed in R0 LINK 16;LINK 16; P0 = R0; // Use passed value as a pointerP0 = R0; // Use passed value as a pointer

P1.L = _thumb;P1.L = _thumb; P1.H = _thumb;P1.H = _thumb; R0 = [P1 + (2 * 4)]; // LOADR0 = [P1 + (2 * 4)]; // LOAD

P1.L = _plumb;P1.L = _plumb; P1.H = _plumb;P1.H = _plumb; R1= [P1 + (1 * 4)]; // LOADR1= [P1 + (1 * 4)]; // LOAD R0 = R0 + R1;R0 = R0 + R1; [P0 + (1 * 4)] = R0; // STORE[P0 + (1 * 4)] = R0; // STORE

P0 = [FP +4];P0 = [FP +4]; UNLINK;UNLINK;_ Jack__FPl .END::_ Jack__FPl .END:: JUMP (P0);JUMP (P0);

File “PutInHisThumb.cpp”File “PutInHisThumb.cpp”

extern long int thumb[10];extern long int thumb[10];long int plumb[5] = {1, 2, 3, 4, 5}long int plumb[5] = {1, 2, 3, 4, 5}long int Jack(long int *goodboy)long int Jack(long int *goodboy)

long int Jack(long int *goodboy{long int Jack(long int *goodboy{ long int sum;long int sum;

goodboy[3] = goodboy[3] = thumb[2]thumb[2] + plumb[1] + plumb[1]}}

extern volatile long int extern volatile long int ..extern _thumb; extern _thumb;

.section L1_cache;.section L1_cache;

.global _plumb[5] = {1, 2, 3, 4, 5};.global _plumb[5] = {1, 2, 3, 4, 5};

.section program;.section program;

.global _Jack__FPl; // Pointer long.global _Jack__FPl; // Pointer long_Jack__FPl: // pointer passed in R0_Jack__FPl: // pointer passed in R0 LINK 16;LINK 16; P0 = R0; // Use passed value as a pointerP0 = R0; // Use passed value as a pointer

P1.L = _thumb;P1.L = _thumb; P1.H = _thumb;P1.H = _thumb; R0 = [P1 + (2 * 4)]; // LOADR0 = [P1 + (2 * 4)]; // LOAD

P1.L = _plumb;P1.L = _plumb; P1.H = _plumb;P1.H = _plumb; R1= [P1 + (1 * 4)]; // LOADR1= [P1 + (1 * 4)]; // LOAD R0 = R0 + R1;R0 = R0 + R1; [P0 + (1 * 4)] = R0; // STORE[P0 + (1 * 4)] = R0; // STORE

P0 = [FP +4];P0 = [FP +4]; UNLINK;UNLINK;_Jack__FPl.END:_Jack__FPl.END: JUMP (P0);JUMP (P0);

File “PutInHisThumb.cpp”File “PutInHisThumb.cpp”

extern volatile long int thumb[10];extern volatile long int thumb[10];long int plumb[5] = {1, 2, 3, 4, 5}long int plumb[5] = {1, 2, 3, 4, 5}long int Jack(long int *goodboy)long int Jack(long int *goodboy)

long int Jack(long int *goodboy{long int Jack(long int *goodboy{ long int sum;long int sum;

goodboy[3] = goodboy[3] = thumb[2]thumb[2] + plumb[1] + plumb[1]}}

extern long int – optimized codeextern long int – optimized code..extern _star; extern _star; .extern _BlinkLight__Fv;.extern _BlinkLight__Fv;

.section program;.section program;

.global _Wonderwhat__Fv; .global _Wonderwhat__Fv; _Wonderwhat__Fv: _Wonderwhat__Fv:

LINK 16;LINK 16; P0 = R0; // Use passed value as a pointerP0 = R0; // Use passed value as a pointer

P1.L = _thumb;P1.L = _thumb; P1.H = _thumb;P1.H = _thumb; R0 = [P1 + (2 * 4)]; // LOADR0 = [P1 + (2 * 4)]; // LOAD

CC = R0 == 2; CC = R0 == 2; IF !CC JUMP DONE;IF !CC JUMP DONE;

LOOP: CALL _BlinkLight__Fv;LOOP: CALL _BlinkLight__Fv;JUMP LOOP;JUMP LOOP;

DONE:DONE:P0 = [FP +4];P0 = [FP +4];

UNLINK;UNLINK;_Wonderwhat__Fv .END:_Wonderwhat__Fv .END: JUMP (P0);JUMP (P0);

File “TwinkleTwinkle.cpp”File “TwinkleTwinkle.cpp”

extern long int star[10];extern long int star[10];void WonderWhat(void);void WonderWhat(void);void BlinkLight(void);void BlinkLight(void);

void WonderWhat(void) {void WonderWhat(void) { while (while (star[2]star[2] == 2) { == 2) { BlinkLight( );BlinkLight( ); }}}}

Either star[2] == 2, or it does notEither star[2] == 2, or it does not

If star[2] == 2 then get an infinite loopIf star[2] == 2 then get an infinite loop

extern volatile long int extern volatile long int optimized codeoptimized code

..extern _star; extern _star;

.extern _BlinkLight__Fv;.extern _BlinkLight__Fv;

.section program;.section program;

.global _Wonderwhat__Fv; .global _Wonderwhat__Fv; _Wonderwhat__Fv: _Wonderwhat__Fv:

LINK 16;LINK 16; P0 = R0; // Use passed value as a pointerP0 = R0; // Use passed value as a pointer

P1.L = _thumb;P1.L = _thumb; P1.H = _thumb;P1.H = _thumb;

LOOP:LOOP: R0 = [P1 + (2 * 4)]; // KEEP LOADINGR0 = [P1 + (2 * 4)]; // KEEP LOADING

CC = R0 == 2; CC = R0 == 2; // LOOP NEEDED// LOOP NEEDED IF !CC JUMP DONE;IF !CC JUMP DONE;

CALL _BlinkLight__Fv;CALL _BlinkLight__Fv;JUMP LOOP;JUMP LOOP;

DONE:DONE:P0 = [FP +4];P0 = [FP +4];

UNLINK;UNLINK;_Wonderwhat__Fv .END:_Wonderwhat__Fv .END: JUMP (P0);JUMP (P0);

File “TwinkleTwinkle.cpp”File “TwinkleTwinkle.cpp”

extern volatile long int star[10];extern volatile long int star[10];void WonderWhat(void);void WonderWhat(void);void BlinkLight(void);void BlinkLight(void);

void WonderWhat(void) {void WonderWhat(void) { while (while (star[2]star[2] == 2) { == 2) { BlinkLight( );BlinkLight( ); }}}}

star[2] == 2 MAY START OFF BEING 2star[2] == 2 MAY START OFF BEING 2

But since star is “volatile” then some external But since star is “volatile” then some external action may change it.action may change it.

Loop needed in optimized code if “volatile” Loop needed in optimized code if “volatile” memory valuememory value

Understanding what I didUnderstanding what I did

Will look at things in more detail later in Will look at things in more detail later in classclass

But here are some ideas of why we did But here are some ideas of why we did what we didwhat we did

Subroutine calling subroutineSubroutine calling subroutineAll void functionsAll void functions

File “bloodEnglishmanASM.asm”File “bloodEnglishmanASM.asm”

.extern _Fee__Fv;.extern _Fee__Fv;

.extern _Fi__Fv;.extern _Fi__Fv;

.extern _Fo__Fv;.extern _Fo__Fv;

.section program;.section program;

.global _Fum__Fv;.global _Fum__Fv;_Fum__Fv:_Fum__Fv:

Since the other functions Since the other functions are coded “outside” or are coded “outside” or “external” to this file then “external” to this file then we indicate that with the we indicate that with the keyword keyword .extern.extern

File File “bloodEnglishmanCPP.cpp”“bloodEnglishmanCPP.cpp”

void Fee(void);void Fee(void);void Fi(void);void Fi(void);void Fo(void);void Fo(void);void Fum(void);void Fum(void);

void Fum(void) {void Fum(void) {

}}

Subroutine calling subroutineSubroutine calling subroutineAll void functionsAll void functions

File “bloodEnglishmanASM.asm”File “bloodEnglishmanASM.asm”

.extern _Fee__Fv;.extern _Fee__Fv;

.extern _Fi__Fv;.extern _Fi__Fv;

.extern _Fo__Fv;.extern _Fo__Fv;

.section program;.section program;

.global _Fum__Fv;.global _Fum__Fv;_Fum__Fv:_Fum__Fv:

Since we want other functions Since we want other functions (coded “outside” or “external” (coded “outside” or “external” to this file) to be able to use to this file) to be able to use Fum( ) coded in this file we Fum( ) coded in this file we must “globalize” (tell must “globalize” (tell everybody) this functions everybody) this functions name with the name with the keyword keyword ..globalglobal

File File “bloodEnglishmanCPP.cpp”“bloodEnglishmanCPP.cpp”

void Fee(void);void Fee(void);void Fi(void);void Fi(void);void Fo(void);void Fo(void);void Fum(void);void Fum(void);

void Fum(void) {void Fum(void) {

}}

Subroutine calling subroutineSubroutine calling subroutineAll void functionsAll void functions

File “bloodEnglishmanASM.asm”File “bloodEnglishmanASM.asm”

.extern _Fee__Fv;.extern _Fee__Fv;

.extern _Fi__Fv;.extern _Fi__Fv;

.extern _Fo__Fv;.extern _Fo__Fv;

.section program;.section program;

.global _Fum__Fv;.global _Fum__Fv;_Fum__Fv:_Fum__Fv: LINK 16;LINK 16;

CALL _Fee __Fv;CALL _Fee __Fv; CALL _Fi __Fv;CALL _Fi __Fv; CALL _Fo__Fv;CALL _Fo__Fv;

P0 = [FP +4];P0 = [FP +4]; UNLINK;UNLINK;_Fum__Fv.END:_Fum__Fv.END: JUMP (P0);JUMP (P0);

The LINK 16; operation saves The LINK 16; operation saves this subroutine’s return this subroutine’s return address (stored in the “RETurn address (stored in the “RETurn from Subroutine register” from Subroutine register” RETS) onto the stack.RETS) onto the stack.

This allows the processor to use This allows the processor to use register RETS when it calls register RETS when it calls other subroutines – same was other subroutines – same was as the MIPS – different as the MIPS – different register nameregister name

UNLINK restores (recovers, UNLINK restores (recovers, reads) RETS from the stack so reads) RETS from the stack so we can exit this subroutine.we can exit this subroutine.

Subroutine calling subroutineSubroutine calling subroutineAll integer functionsAll integer functions

.extern _Fee__Fi;.extern _Fee__Fi;

.extern _Fi__Fi;.extern _Fi__Fi;

.extern _Fo__Fi;.extern _Fo__Fi;

.section program;.section program;

.global _Fum__Fi;.global _Fum__Fi;_Fum__Fi: // value passed in R0_Fum__Fi: // value passed in R0 LINK 16;LINK 16; [--SP] = R0;[--SP] = R0; // Save passed value // Save passed value

R0 = 6; // pass parameter in R0R0 = 6; // pass parameter in R0 CALL _Fee __Fi;CALL _Fee __Fi;

R0 = 7; // pass parameter in R0R0 = 7; // pass parameter in R0 CALL _Fi __Fi;CALL _Fi __Fi;

R0 = [SP++];R0 = [SP++]; // Recover passed value // Recover passed value R0 += 8;R0 += 8; // pass parameter in R0 // pass parameter in R0 CALL _Fo__Fi;CALL _Fo__Fi;

P0 = [FP +4];P0 = [FP +4]; UNLINK;UNLINK;_Fum__Fi.END:_Fum__Fi.END: JUMP (P0);JUMP (P0);

Name mangle changesName mangle changes(Do not need to remember (Do not need to remember

details in quizzes and exams details in quizzes and exams – critical to get correct in labs)– critical to get correct in labs)

void Foo(void) void Foo(void) _Foo__Fv _Foo__Fvint Foo(void) int Foo(void) _Foo__Fv _Foo__Fvint Foo(int) int Foo(int) _Foo__Fi _Foo__Fiint Foo(long int) int Foo(long int) _Foo__Fl _Foo__Flint Foo(int *) int Foo(int *) _Foo__FPi _Foo__FPiInt Foo(long int *) Int Foo(long int *) _Foo_FPl _Foo_FPl

Can’t overload on the basis of Can’t overload on the basis of return valuereturn value

Subroutine calling subroutineSubroutine calling subroutineAll integer functionsAll integer functions

.extern _Fee__Fi;.extern _Fee__Fi;

.extern _Fi__Fi;.extern _Fi__Fi;

.extern _Fo__Fi;.extern _Fo__Fi;

.section program;.section program;

.global _Fum__Fi;.global _Fum__Fi;_Fum__Fi: _Fum__Fi: // value passed in R0// value passed in R0 LINK 16;LINK 16; [--SP] = R0;[--SP] = R0; // Save passed value // Save passed value

R0 = 6;R0 = 6; // pass parameter in R0 // pass parameter in R0 CALL _Fee __Fi;CALL _Fee __Fi;

R0 = 7;R0 = 7; // pass parameter in R0 // pass parameter in R0 CALL _Fi __Fi;CALL _Fi __Fi;

R0 = [SP++];R0 = [SP++]; // Recover passed value // Recover passed value R0 += 8;R0 += 8; // pass parameter in R0 // pass parameter in R0 CALL _Fo__Fi;CALL _Fo__Fi;

P0 = [FP +4];P0 = [FP +4]; UNLINK;UNLINK;_Fum__Fi.END:_Fum__Fi.END: JUMP (P0);JUMP (P0);

void Fum(int void Fum(int valuevalue) {) { Fee(6);Fee(6); Fi(7);Fi(7); Fo(8 + value);Fo(8 + value);}}

ALWAYS pass the parameter ALWAYS pass the parameter to the subroutine in R0 – to the subroutine in R0 – very similar to MIPSvery similar to MIPS

Subroutine calling subroutineSubroutine calling subroutineAll integer functionsAll integer functions

.extern _Fee__Fi;.extern _Fee__Fi;

.extern _Fi__Fi;.extern _Fi__Fi;

.extern _Fo__Fi;.extern _Fo__Fi;

.section program;.section program;

.global _Fum__Fi;.global _Fum__Fi;_Fum__Fi: // value passed in R0_Fum__Fi: // value passed in R0 LINK 16;LINK 16; [--SP] = R0;[--SP] = R0; // Save passed value // Save passed value

R0 = 6;R0 = 6; // pass parameter in R0 // pass parameter in R0 CALL _Fee __Fi;CALL _Fee __Fi;

R0 = 7;R0 = 7; // pass parameter in R0 // pass parameter in R0 CALL _Fi __Fi;CALL _Fi __Fi;

R0 = [SP++];R0 = [SP++]; // Recover passed value // Recover passed value R0 += 8;R0 += 8; // pass parameter in R0 // pass parameter in R0 CALL _Fo__Fi;CALL _Fo__Fi;

P0 = [FP +4];P0 = [FP +4]; UNLINK;UNLINK;_Fum__Fi.END:_Fum__Fi.END: JUMP (P0);JUMP (P0);

void Fum(int void Fum(int valuevalue) {) { Fee(6);Fee(6); Fi(7);Fi(7); Fo(8 + value);Fo(8 + value);}}

Must save the passed value Must save the passed value (in R0) onto the stack, (in R0) onto the stack, otherwise it will be otherwise it will be destroyed when we call the destroyed when we call the other subroutines while we other subroutines while we use R0 to pass the use R0 to pass the parameterparameter

Note – We can store on the stack Note – We can store on the stack using SP or FP (same as MIPS)using SP or FP (same as MIPS)

.extern _Fee__Fi;.extern _Fee__Fi;

.extern _Fi__Fi;.extern _Fi__Fi;

.extern _Fo__Fi;.extern _Fo__Fi;

.section program;.section program;

.global _Fum__Fi;.global _Fum__Fi;_Fum__Fi: // value passed in R0_Fum__Fi: // value passed in R0 LINK 16;LINK 16; [FP + 8] = R0;[FP + 8] = R0; // Save passed value // Save passed value

R0 = 6; // pass parameter in R0R0 = 6; // pass parameter in R0 CALL _Fee __Fi;CALL _Fee __Fi;

R0 = 7; // pass parameter in R0R0 = 7; // pass parameter in R0 CALL _Fi __Fi;CALL _Fi __Fi;

R0 = [FP + 8];R0 = [FP + 8]; // Recover passed value// Recover passed value R0 += 8;R0 += 8; // pass parameter in R0 // pass parameter in R0 CALL _Fo__Fi;CALL _Fo__Fi;

P0 = [FP +4];P0 = [FP +4]; UNLINK;UNLINK;_Fum__Fi.END:_Fum__Fi.END: JUMP (P0);JUMP (P0);

.extern _Fee__Fi;.extern _Fee__Fi;

.extern _Fi__Fi;.extern _Fi__Fi;

.extern _Fo__Fi;.extern _Fo__Fi;

.section program;.section program;

.global _Fum__Fi;.global _Fum__Fi;_Fum__Fi: // value passed in R0_Fum__Fi: // value passed in R0 LINK LINK 16;16; [SP + 16 + 8] = R0;[SP + 16 + 8] = R0; // Save passed value // Save passed value

R0 = 6; // pass parameter in R0R0 = 6; // pass parameter in R0 CALL _Fee __Fi;CALL _Fee __Fi;

R0 = 7; // pass parameter in R0R0 = 7; // pass parameter in R0 CALL _Fi __Fi;CALL _Fi __Fi;

R0 = [SP + 16 + 8] // Recover passed valueR0 = [SP + 16 + 8] // Recover passed value R0 += 8;R0 += 8; // pass parameter in R0 // pass parameter in R0 CALL _Fo__Fi;CALL _Fo__Fi;

P0 = [FP +4];P0 = [FP +4]; UNLINK;UNLINK;_Fum__Fi.END:_Fum__Fi.END: JUMP (P0);JUMP (P0);

This does not link and runThis does not link and runWhy not?Why not?

A project containing A project containing only the file only the file “PutInHisThumb.cpp” “PutInHisThumb.cpp” will not link and run will not link and run becausebecause

All projects must have a All projects must have a main( )main( ) in them in them

File “PutInHisThumb.cpp”File “PutInHisThumb.cpp”

extern long int thumb[10];extern long int thumb[10];long int plumb[5] = {1, 2, 3, 4, 5}long int plumb[5] = {1, 2, 3, 4, 5}long int Jack(long int *goodboy)long int Jack(long int *goodboy)

long int Jack(long int *goodboy{long int Jack(long int *goodboy{ long int sum;long int sum;

goodboy[3] = goodboy[3] = thumb[2]thumb[2] + + plumb[1]plumb[1]

}}

This does not link and runThis does not link and runWhy not?Why not?

File “main.cpp”File “main.cpp”

extern long int thumb[10];extern long int thumb[10];extern extern long int plumb[5] ;long int plumb[5] ;long int goodboy[10];long int goodboy[10];

long int Jack(long int *goodboy)long int Jack(long int *goodboy)

int main(void) {int main(void) {for (int count = 0; for (int count = 0;

count < 10; count++)count < 10; count++) goodboy[count] = 0;goodboy[count] = 0; Jack(goodboy);Jack(goodboy); printf(“%d\n”, goodboy[3]);printf(“%d\n”, goodboy[3]);}}

File “PutInHisThumb.cpp”File “PutInHisThumb.cpp”

extern long int thumb[10];extern long int thumb[10];long int plumb[5] = {1, 2, 3, 4, 5}long int plumb[5] = {1, 2, 3, 4, 5}long int Jack(long int *goodboy)long int Jack(long int *goodboy)

long int Jack(long int *goodboy{long int Jack(long int *goodboy{ long int sum;long int sum;

goodboy[3] = goodboy[3] = thumb[2]thumb[2] + plumb[1] + plumb[1]}}

This does not link and runThis does not link and runWhy not?Why not?

DECLARED IN ANOTHER FILEDECLARED IN ANOTHER FILEDECLARED IN THIS FILEDECLARED IN THIS FILE

File “main.cpp”File “main.cpp”

extern long int thumb[10];extern long int thumb[10];extern long int plumb[5] ;extern long int plumb[5] ;long int goodboy[10];long int goodboy[10];

File “PutInHisThumb.cpp”File “PutInHisThumb.cpp”

extern long int thumb[10];extern long int thumb[10];long int plumb[5] = {1, 2, 3, 4, 5}long int plumb[5] = {1, 2, 3, 4, 5}

DECLARED IN ANOTHER FILEDECLARED IN ANOTHER FILEDECLARED IN ANOTHER FILEDECLARED IN ANOTHER FILEDECLARED IN THIS FILEDECLARED IN THIS FILE

IN NO FILE HAS MEMORY SPACE BEEN SET ASIDE FOR THE “THUMB” ARRAY

This does link and run -- because This does link and run -- because all arrays have been given “space”all arrays have been given “space”

File “main.cpp”File “main.cpp”

long int thumb[10] = {2, 4, 6, 8, 10};long int thumb[10] = {2, 4, 6, 8, 10};extern long int plumb[5] ;extern long int plumb[5] ;long int goodboy[10];long int goodboy[10];

long int Jack(long int *goodboy)long int Jack(long int *goodboy)

int main(void) {int main(void) {for (int count = 0; for (int count = 0;

count < 10; count++)count < 10; count++) goodboy[count] = 0;goodboy[count] = 0; Jack(Jack(goodboygoodboy);); printf(“%d\n”, goodboy[3]);printf(“%d\n”, goodboy[3]);}}

File “PutInHisThumb.cpp”File “PutInHisThumb.cpp”

extern long int thumb[10];extern long int thumb[10];long int plumb[5] = {1, 2, 3, 4, 5}long int plumb[5] = {1, 2, 3, 4, 5}long int Jack(long int *goodboy)long int Jack(long int *goodboy)

long int Jack(long int *goodboy{long int Jack(long int *goodboy{ long int sum;long int sum;

goodboy[3] = thumb[2] + plumb[1]goodboy[3] = thumb[2] + plumb[1]}}

How can something in memory How can something in memory change like thischange like this

..extern _star; extern _star;

.extern _BlinkLight__Fv;.extern _BlinkLight__Fv;

.section program;.section program;

.global _Wonderwhat__Fv; .global _Wonderwhat__Fv; _Wonderwhat__Fv: _Wonderwhat__Fv:

LINK 16;LINK 16; P0 = R0; // Use passed value as a pointerP0 = R0; // Use passed value as a pointer

P1.L = _thumb;P1.L = _thumb; P1.H = _thumb;P1.H = _thumb;

LOOP:LOOP: R0 = [P1 + (2 * 4)]; // KEEP LOADINGR0 = [P1 + (2 * 4)]; // KEEP LOADING

CC = R0 == 2; CC = R0 == 2; // LOOP NEEDED// LOOP NEEDED IF !CC JUMP DONE;IF !CC JUMP DONE;

CALL _BlinkLight__Fv;CALL _BlinkLight__Fv;JUMP LOOP;JUMP LOOP;

DONE:DONE:P0 = [FP +4];P0 = [FP +4];

UNLINK;UNLINK;_Wonderwhat__Fv .END:_Wonderwhat__Fv .END: JUMP (P0);JUMP (P0);

File “TwinkleTwinkle.cpp”File “TwinkleTwinkle.cpp”

extern volatile long int star[10];extern volatile long int star[10];void WonderWhat(void);void WonderWhat(void);void BlinkLight(void);void BlinkLight(void);

void WonderWhat(void) {void WonderWhat(void) { while (while (star[2]star[2] == 2) { == 2) { BlinkLight( );BlinkLight( ); }}}}

star[2] == 2 MAY START OFF BEING 2star[2] == 2 MAY START OFF BEING 2

But since star is “volatile” then some external But since star is “volatile” then some external action may change it.action may change it.

Loop with repeated read needed in Loop with repeated read needed in optimized code if “volatile” memory optimized code if “volatile” memory valuevalue

Example from Lab. 1 Task 4Example from Lab. 1 Task 4

Lab. 1 Task 4Lab. 1 Task 4

There is a main routine There is a main routine There is an “interrupt audio” routine. Every There is an “interrupt audio” routine. Every 1 / 40000 s the interrupt routine “interrupts” (temporarily 1 / 40000 s the interrupt routine “interrupts” (temporarily halts) halts) main( )main( ),,then runs itself, and then returns control to mainthen runs itself, and then returns control to mainInterrupts run under “external control” when “something Interrupts run under “external control” when “something happens” happens” – switch changes or voltage changes -- unexpectedswitch changes or voltage changes -- unexpected

Subroutines run under “internal control”Subroutines run under “internal control”– ONLY WHEN CALLED – NEVER UNEXPECTEDONLY WHEN CALLED – NEVER UNEXPECTED

Example from Lab. 1 Task 4Example from Lab. 1 Task 4File “main.cpp”File “main.cpp”

volatile boolean mute_on = FALSE;volatile boolean mute_on = FALSE;

long int ReadSwitches(void); long int ReadSwitches(void);

int main( )int main( ) InitializeSwitches( );InitializeSwitches( ); InitializeAudio( );InitializeAudio( );

StartInterrupts( );StartInterrupts( ); while (1) {while (1) { int value = ReadSwitches( );int value = ReadSwitches( );// If switch pressed // If switch pressed // – turn off sound;// – turn off sound; if (value == 0x100) if (value == 0x100) mute_on = TRUE;mute_on = TRUE;

else else mute_on = FALSE;mute_on = FALSE; }}}}

File “interruptservice.cpp”File “interruptservice.cpp”

extern volatile boolean mute_on;extern volatile boolean mute_on;void Process_DataASM(void);void Process_DataASM(void);

EX_INTERRUPT_HANDLER(Sport0_RX_ISR)EX_INTERRUPT_HANDLER(Sport0_RX_ISR){{

………….. /// Lots of good stuff.. /// Lots of good stuff

Process_DataASM( );Process_DataASM( ); // Make the sound occur// Make the sound occur

………….. // Lots of more good stuff;.. // Lots of more good stuff;

}}

void Process_DataASM(void) {void Process_DataASM(void) { if (mute_on = = FALSE)if (mute_on = = FALSE)

MakeTheSound( ); MakeTheSound( );}}

WORRY ABOUT WHAT EX_INTERRUPT_HANDLER( ) MEANS IN LAB. 2