No ACL setup yet! Denying access to everyone.
en:edu:zmitac:s1:apl:lab:lab4
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| en:edu:zmitac:s1:apl:lab:lab4 [2024/11/27 14:52] – created ktokarz | en:edu:zmitac:s1:apl:lab:lab4 [2024/12/05 12:39] (current) – [Tasks to be done during laboratory] ktokarz | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Programming in Assembler Laboratory ====== | ====== Programming in Assembler Laboratory ====== | ||
| ===== Laboratory 4 ===== | ===== Laboratory 4 ===== | ||
| - | DLL functions call and parameters passing. | + | Use of vector instructions. |
| ===== The aim of the exercise ===== | ===== The aim of the exercise ===== | ||
| - | The aim of the exercise is to learn about calling library | + | The aim of the exercise is to learn about vector instructions called SIMD (Single Instruction Multiple Data) and passing parameters to functions |
| - | < | + | As in previous |
| + | Functions will calculate the sum, difference, multiplication, and division | ||
| < | < | ||
| - | ===== The example code ===== | + | ===== First example code ===== |
| - | The following code is divided into two parts. The C++ part calls the functions | + | In the first example, we'll write the function in assembler with an internal definition of variables to show how to define floating point vectors in the assembly program. |
| + | The following code is divided into three parts. The C++ part calls the function | ||
| C++ file | C++ file | ||
| Line 22: | Line 24: | ||
| std::cout << "Hello World!\n"; | std::cout << "Hello World!\n"; | ||
| dllHandle = LoadLibrary(TEXT(" | dllHandle = LoadLibrary(TEXT(" | ||
| - | | + | MyProc |
| - | + | ||
| - | | + | |
| - | | + | |
| - | + | ||
| - | | + | |
| - | printf(" | + | |
| - | printf(" | + | |
| } | } | ||
| </ | </ | ||
| Line 36: | Line 33: | ||
| < | < | ||
| ; | ; | ||
| - | ; String | + | ; Vectors |
| ; | ; | ||
| .data | .data | ||
| - | DataString DB ' | + | first_vector REAL4 2.2, 4.4, 6.6, 8.8 |
| + | second_vector REAL4 1.1, 1.1, 1.1, 1.1 | ||
| + | resulting_vector REAL4 0.0, 0.0, 0.0, 0.0 | ||
| .code | .code | ||
| ; | ; | ||
| - | ; Procedure | + | ; Function |
| - | ; No input parameters, result | + | ; No input parameters, |
| ; | ; | ||
| - | FindChar_1 | + | |
| - | PUSH RSI | + | Sum_of_vectors |
| - | LEA RSI, DataString | + | PUSH RSI ; RSI is non-volatile register |
| - | | + | LEA RSI, first_vector |
| - | Check_End: | + | |
| - | CMP BYTE PTR [RSI], 0FFH ; is it the end of a string (special character FF)? | + | |
| - | | + | |
| - | CMP AL, [RSI] | + | |
| - | | + | |
| - | ADD RSI, 1 ; increment an offset | + | |
| - | | + | |
| - | Got_Equal: | + | |
| - | MOV DL, [RSI] ; load found char into DL | + | |
| - | JMP Done | + | |
| - | Not_Find: | + | |
| - | MOV RAX,0 ; character not found | + | |
| POP RSI | POP RSI | ||
| - | RET ; return from procedure | + | RET |
| - | Done: | + | Sum_of_vectors |
| - | MOV RAX,1 ; character found | + | |
| - | POP RSI | + | |
| - | RET | + | |
| - | FindChar_1 | + | |
| - | ; | + | END |
| - | ; | + | |
| - | ; No input parameters, result is the time (number of processor ticks) | + | |
| ; | ; | ||
| - | ReadTime_1 PROC | + | End of the file |
| - | | + | ;**************************************************************************** |
| - | MOV RCX,RAX | + | </ |
| - | CALL FindChar_1 | + | Def file. |
| - | RDTSC | + | < |
| - | SUB RAX,RCX | + | LIBRARY JaAsm |
| + | EXPORTS Sum_of_vectors | ||
| + | </ | ||
| - | RET ; return form procedure | + | ===== Second example code ===== |
| - | ReadTime_1 ENDP | + | In the second example, we'll write the function in assembler which accepts arguments as three addresses (pointers) to tables of floats defined in a C++ program. The first and second vectors are source while the third argument is a placeholder for the result of calculations. |
| + | The following code is divided into three parts. The C++ part defines the vectors and calls the function in a dynamically linked library passing the addresses of variables as arguments. The function, called Sum_of_vectors(), | ||
| - | END | + | C++ file |
| + | < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | typedef float FloatTable[4]; | ||
| + | typedef FloatTable* FloatPtr; | ||
| + | |||
| + | typedef int(_stdcall* MyProc)(FloatPtr, | ||
| + | HINSTANCE dllHandle = NULL; | ||
| + | |||
| + | FloatTable first_vector { 2.2f, 4.4f, 6.6f, 8.8f }; | ||
| + | FloatTable second_vector{ 1.1f, 1.1f, 1.1f, 1.1f }; | ||
| + | FloatTable result_vector{ 0.0, 0.0, 0.0, 0.0 }; | ||
| + | |||
| + | |||
| + | int main() | ||
| + | { | ||
| + | std::cout << "Hello World!\n"; | ||
| + | dllHandle = LoadLibrary(TEXT(" | ||
| + | |||
| + | MyProc Sum_of_vectors = (MyProc)GetProcAddress(dllHandle, | ||
| + | |||
| + | Sum_of_vectors(& | ||
| + | std::cout << " | ||
| + | std::cout << " | ||
| + | std::cout << " | ||
| + | std::cout << " | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Assembler file | ||
| + | < | ||
| + | .code | ||
| + | |||
| + | ; | ||
| + | ; | ||
| + | ; | ||
| ; | ; | ||
| - | End of the file | + | Sum_of_vectors PROC |
| + | MOVUPS XMM0, OWORD PTR [RCX] ; load content | ||
| + | MOVUPS XMM1, OWORD PTR [RDX] ; load content of memory pointed by the second parameter | ||
| + | ADDPS XMM0, XMM1 | ||
| + | MOVUPS OWORD PTR [R8], XMM0 ; store the result into memory pointed by third parameter | ||
| + | |||
| + | RET | ||
| + | Sum_of_vectors ENDP | ||
| ; | ; | ||
| + | |||
| + | END | ||
| </ | </ | ||
| + | |||
| + | Def file is the same as in a previous example. | ||
| ===== Tasks to be done during laboratory ===== | ===== Tasks to be done during laboratory ===== | ||
| - | - Compile the example code and observe | + | - Compile the examples |
| - | - Make the function more universal: define | + | - Write additional functions to calculate subtraction, |
| - | | + | - Write the program which calculates |
| - | - Compare execution time for scalar | + | * Table contains 64 float values and is defined |
| + | * And address | ||
| + | * A size of the table is passed | ||
| + | | ||
| + | * Function returns the value as the scalar, traditionally by XMM0. | ||
| + | |||
| + | =====For | ||
| + | - Include the code of additional functions. | ||
| + | - Include the code of the average program | ||
| + | - The above programs must contain comments. | ||
en/edu/zmitac/s1/apl/lab/lab4.1732715568.txt.gz · Last modified: by ktokarz
