generating “rectify( )”

38
Generating “Rectify( )” Assembly code examples Part 1 of 3

Upload: evan-salazar

Post on 01-Jan-2016

36 views

Category:

Documents


1 download

DESCRIPTION

Generating “Rectify( )”. Assembly code examples Part 1 of 3. Concepts. Concepts of C++ “stubs” Forcing the test to fail – test of test Generating valid “C++ code” to satisfy the tests Need for “name mangling” for overloaded functions - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Generating “Rectify( )”

Generating “Rectify( )”

Assembly code examplesPart 1 of 3

Page 2: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

2 / 38

Concepts

Concepts of C++ “stubs” Forcing the test to fail – test of test Generating valid “C++ code” to satisfy the

tests Need for “name mangling” for overloaded

functions How do you find out the name mangled name

so it can be used in assembly code Learning just enough TigerSHARC assembly

code to make things “work”

Page 3: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

3 / 38

Software “AM” radio concept

AntennaPickup

LocalOscillator

Mixer Low passFilter

Rectifier

Audio out

RF STAGE

IF STAGE

AUDIO STAGE

Most stages handled with high speed software

Low passFilter

+ amplifier

Page 4: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

4 / 38

Test Driven Development

DescribeRequirements

Design Solution

Build Solution Test Solution

WriteAcceptance Tests

WriteUnit Tests

CUSTOMER

DEVELOPER

Work with customer to check that the tests properly express what the customer wants done. Iterative process with customer “heavily involved” – “Agile” methodology.

Page 5: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

5 / 38

E-TDD Test.cpp files have four main

components. Many error messages if not present Include Files – cut-and-paste (always the same)

TEST_CONNECT (TestFileInfo)

TEST(testNAME, testTYPE)

NOTE: Tests execute from LAST in file to FIRST. As normally the LAST test is the most recently added test, this is good.

You test if new code works and then check (regression test) that you did not break anything

LINK_TEST(TestFileInfo, testTYPE)

Page 6: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

6 / 38

Now expand the Customer Tests to do what the customer has requested

Problems to be fixed in Assignment 1

Add test for If N <= 0, return NULL otherwise return the start of the output array

Tests are working by mistake asWe are not resetting the output array to 0 between function calls

Page 7: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

7 / 38

Name mangled names can be seen from linker

C++ name as used The name mangled name generated byin C++ code by the C++ compiler in response to function overloading. These are the “assembly code” names

Page 8: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

8 / 38

Next step: Write just enough code to satisfy the linker – C++ stubs

Page 9: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

9 / 38

Write the assembly language stub

We lost control of the processors in the debug environment.

Page 10: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

10 / 38

Build the code incrementally to satisfy tests

See speed change now weAre executing code – but why failures

Note what if N < = 0

Page 11: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

11 / 38

Note

Special marker

Compiler optimization

FLOATS 927 304 -- THREE FOLD

INTS 960 150 – SIX FOLD

Why the difference, and can we do better, and do we want to?

Note the failures – what are they

Page 12: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

12 / 38

Fix Tests to only show “FAILURES

Page 13: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

13 / 38

Generate assembly code

Do the code in steps, attempting to satisfy one test at a time

Learn “the assembler” in steps Get “some idea” of the issues we need to

learn about as we go along Just enough knowledge “to get things to

work” Worry about full details later

Page 14: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

14 / 38

What we need to know based on experiences from other processors Can we return from an assembly language routine

without crashing the processor? Return a parameter from assembly language routine

(Is it same for ints and floats?) Pass parameters into assembly language

(Is it same for ints and floats?) Do IF THEN ELSE statements Read and write values to memory Read and write values in a loop Do some mathematics on the values fetched from

memoryAll this stuff is demonstrated by coding

HalfWaveRectifyASM( )

Page 15: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

15 / 38

Write tests about passing values back from an assembly code routine

Page 16: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

16 / 38

What we have learned We passed the “very general” test

Managed to call and return from an assembly code and did not crash the system

We passed some specific tests in the test file “by accident”

CJMP – is the “way to return” from an assembly code function to “C++” Instruction format is interesting

nop; nop; nop;; ; separate instructions executed togetherCJMP (ABS);; ;; indicates the end of an “grouped”

instructionCJMP must be like RTS – meaning there is a CJMP register (or

memory location) storing the address to return to after thisCOMPARE TO Blackfin

P0 = [FP + 4]; Place storing return address UNLINK; JUMP (P0);

Page 17: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

17 / 38

More detailed look at the code

Single semi-colonsDouble semi-colons

Start function labelEnd function label

Used for“profiling code”

Label format similar to 68KNeeds leading underscore and final colon

As with 68K and Blackfin needs a .sectionBut name and format different

As with 68K need .align statementIs the “4” in bytes (8 bits)

or words (32 bits)

As with 68K need .globalto tell other code that this function

exists

Page 18: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

18 / 38

Need to know

How do we return “an integer pointer” Need to look at “C++” manual for coding

conventions As with 68K, MIPS and Blackfin expect to

have Volatile registers – function variate registers,

that DON’T need to be conserved when developing a function

Non-volatile, preserved registers – function invariate registers, that DO need to be conserved when developing a function

Page 19: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

19 / 38

Return registers There are many, depending on what you need to return Here we need to use J8

Many registers available – need ability to control usage J0 to J31 – registers (integers and pointers) (SISD mode) XR0 to XR31 – registers (integers) (SISD mode) XFR0 to XFR31 – registers (floats) (SISD mode)

Did I also mention I0 to I31 – registers (integers and pointers) (SISD mode) YR0 to YR31 , YFR0 to YFR31 (SIMD mode) XYR, YXR and R registers (SIMD mode) And also the MIMD modes And the double registers and the quad registers …….

#define return_pt_J8 J8 // J8 is a VOLATILE, NON-PRESERVED register

Page 20: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

20 / 38

Using J8 for returned int * value

Now passing this test “by accidentShould be conditionally passing back NULL

Page 21: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

21 / 38

Conditional tests

Need to code – returning a NULL or the starting address of the final array

int *HalfWaveRectifyRelease(int initial_array[ ], int final_array[ ], int N)if ( N < 1) return_pt = NULL;else /* after some calculations */ return_pt = &final[ 0];

Questions to ask the instruction manual How are parameters passed to us? On the stack (as with 68K) or in registers / stack (as with

MIPS and Blackfin)? – answer turns out to be more like MIPS and Blackfin

How do you do an IF? How do you do conditional jumps?

Page 22: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

22 / 38

Parameter passing

Spaces for first four parameters ARE ALWAYS present on the stack (as with 68K)

But the first four parameters are passed in registers (J4, J5, J6 and J7 most of the time) (as with MIPS)

The parameters passed in registers are often then stored into the spaces on the stack (like the MIPS) for “safe keeping” when assembly code functions call assembly code functions

J4, J5, J6 and J7 are volatile, non-preserved registers

Page 23: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

23 / 38

Coding convention

// int *HalfWaveRectifyRelease(int initial_array[ ],

// int final_array[ ], int N)

#define initial_pt_inpar1 J4 incoming parameters

#define final_pt_inpar2 J5

#define N_J6_inpar3 J6

#define return_pt_J8 J8 return value

Page 24: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

24 / 38

Can we pass back the start of the final array

Still passing tests byaccident and thestart of the array needs to be conditional returnvalue

Page 25: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

25 / 38

What we need to know based on experiences from other processors Can we return from an assembly language routine

without crashing the processor? Return a parameter from assembly language routine

(Is it same for ints and floats?) Pass parameters into assembly language

(Is it same for ints and floats?) Do IF THEN ELSE statements Read and write values to memory Read and write values in a loop Do some mathematics on the values fetched from

memoryAll this stuff is demonstrated by coding

HalfWaveRectifyASM( )

Page 26: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

26 / 38

Doing an IF (N < 1) JUMP type of instruction

68K versionCMP.L #1, D1 ; Performs subtraction (D1 – 1) and sets

; condition code flagBLT ELSE ; Branch if result of (D1 – 1) < 0

; BLE is a branch if less than ; zero instruction NOT on whether D1 < 1

TigerSHARC version COMP(N_inpar3, 1);; // Perform N < 1 test IF JLT, JUMP ELSE;; // NOTE: Use of comma , and semi-colons ;;

Same possible error on BOTH processors 68K -- which test BLE, BLT or BGT should be used? TigerSHARC – which test JLE, JLT or NJLE should be used?

Page 27: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

27 / 38

ELSE is a TigerSHARC keyword Should have guessed as editor turned in blue

ELSE is a KEYWORD

Fix that error first

Page 28: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

28 / 38

Why is ELSE a keyword

FOUR PART ELSE INSTRUCTION IS LEGAL

IF JLT; ELSE, J1 = J2 + J3; // Conditional execution – if true ELSE, XR1 = XR2 + XR3; // Conditional – if true YFR1 = YFR2 + YFR3;; // Unconditional -- always

IF JLT; DO, J1 = J2 + J3; // Conditional execution -- if true DO, XR1 = XR2 + XR3; // Conditional -- if true YFR1 = YFR2 + YFR3;; // Unconditional -- always

Having this sort of format means that the instruction pipeline is not disrupted by jumps when we do IF statements

Page 29: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

29 / 38

Fix ELSE keyword error

GREATER a keyword?Not blue

Just change it to somethingelse rather than wasting time

Page 30: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

30 / 38

Label name is not the problem

NOTE:This is “C-like” syntax,But it is not “C”

Statement must end in ;;Not ;

Page 31: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

31 / 38

Should learn to read – looking at wrong error. Click on error line

Missing ;;

Page 32: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

32 / 38

Still not got the correct syntaxBecause of missing ;; (dual semicolons)

Processor thinks we want

return_pt = 0; JUMP END_IF; return_pt = INPAR3 ;;

Apparently such a complicated instruction IS LEGAL provided the jump is at the start of the multiple issue instruction

Page 33: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

33 / 38

Add dual-semicolons everywhereWorry about “multiple issues” later

This dual semi-colonIs so important that youMUST code review for it allthe time or else you wasteso much time in theLab. Key in exams / quizzes

At last an error I know how to fix

Page 34: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

34 / 38

Well I thought I understood it !!!

Speed issue – JUMPS can’t be too close together.

Not normally a problem when “if” is larger

Page 35: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

35 / 38

Add a single instruction of 4 NOPsnop; nop; nop; nop;; Fix the last error as part of Assignment 1Fix the remaining error

in handling the IF THEN ELSEas part of assignment 1

Worry about code efficiency later(refactor) when all code working

Page 36: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

36 / 38

What we need to know based on experiences from other processors Can we return from an assembly language routine

without crashing the processor? Return a parameter from assembly language routine

(Is it same for ints and floats?) Pass parameters into assembly language

(Is it same for ints and floats?) Do IF THEN ELSE statements Read and write values to memory Read and write values in a loop Do some mathematics on the values fetched from

memoryAll this stuff is demonstrated by coding

HalfWaveRectifyASM( )

Page 37: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

37 / 38

Assignment 1 – code the following as a software loop – follow 68K approachextern “C” int CalculateSum(void) { int sum = 0; for (int count = 0; count < 6; count++) { sum = sum + count; } return sum;}

extern “C” – means that this function is “C” compatible rather than “C++”. No overloading (requiring name-mangling) permitted

Page 38: Generating “Rectify( )”

04/19/23 TigerSHARC assemble code 1, M. Smith, ECE, University of Calgary, Canada

38 / 38

Reminder – software for-loopbecomes “while loop” with initial test

extern “C” int CalculateSum(void) {

int sum = 0;

int count = 0;

while (count < 6) {

sum = sum + count;

count++;

}

return sum;

}

Do line by line translation