User Tools

Site Tools


No ACL setup yet! Denying access to everyone.
en:edu:zmitac:s1:apl:lab:lab2

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:edu:zmitac:s1:apl:lab:lab2 [2024/11/25 11:34] – [Instalacja] ktokarzen:edu:zmitac:s1:apl:lab:lab2 [2024/11/26 13:26] (current) – [Laboratory2] ktokarz
Line 1: Line 1:
-====== Lab 2: Installation and configuration of the Visual Studio Community 2019 for dll libraries written in MASM. ======+====== Programming in Assembler Laboratory ====== 
 +===== Laboratory 2 ===== 
 +Configuration of the Visual Studio Community 2019 for dll libraries written in MASM.
  
-===== Utworzenie projektu z asemblerową biblioteką DLL =====+===== Creating the project with dll library written in assembler =====
  
-Po zainstalowaniu możemy utworzyć projekt, w którym główny program będzie napisany w języku wysokiego poziomu (np. C#, C++), a biblioteka dołączana dynamicznie (dll) będzie napisana w asemblerze.  +In the beginning, we can create a project in which the main program will be written in HLL (High-Level Language) like C++, and the library written in assembler will be linked dynamically (dll).  
-<note tip>Tworzenie bibliotek w asemblerze, wywoływanych z programów stworzonych w wybranym języku wysokiego poziomu (HLL) jest obecnie najczęstszym wykorzystaniem języka asemblerowego.</note> +<note tip>Creation of libraries written in assembly language, with functions called from the application written in a chosen high-level language is currently the most common assembler use.</note> 
-<note>Przedstawiony w dalszej części proces konfiguracji przebiega identycznie dla programu głównego napisanego w C++ jak i C#. Wykorzystane zrzuty ekranu pochodzą z konfiguracji bibliotek DLL dla obu języków programowania, jednak zwykle różnice są drugoplanowe i nie mają wpływu na proces konfiguracji środowiska.</note> +<note>The configuration process presented in the following slides can slightly differ in other versions of Visual Studio and is identical to the main program written in C++ and C#. The screenshots used are from the DLL configuration for both programming languages, but the differences are usually secondary and do not affect the environment configuration process.</note> 
-==== Utworzenie nowego projektu ==== +==== Creating of the new project ==== 
-W tym ćwiczeniu zajmiemy się dołączeniem biblioteki dll do programu napisanego w C++. +During this laboratory, we'll show how to create and use dll (dynamically linked library) written in assembler in that application written in C++ 
-Konfigurację rozpoczynamy od utworzenia nowego projektu w języku C++. +We start by creating a new C++ project. 
  
 {{ :pl:edu:zmitac:n1z:ja:create_step1.jpg?800 |}} {{ :pl:edu:zmitac:n1z:ja:create_step1.jpg?800 |}}
  
-Utworzymy aplikację konsolową, napisaną w języku C++, przeznaczoną do uruchomienia pod kontrolą systemu operacyjnego Windows.+We'll create the console application, written in C++, for execution in the Windows operating system.
  
 {{ :pl:edu:zmitac:n1z:ja:create_1.jpg?800 |}} {{ :pl:edu:zmitac:n1z:ja:create_1.jpg?800 |}}
  
-Aby utworzyć projekt należy go nazwać. Dla tego projektu proponujemy nazwę "Lab2_dll_cpp". Ścieżki pozostawiamy domyślne.+We need to define that name for the project. We suggest the name "Lab2_dll_cpp". All paths can be left with default values.
  
 {{ :pl:edu:zmitac:n1z:ja:create_2.jpg?800 |}} {{ :pl:edu:zmitac:n1z:ja:create_2.jpg?800 |}}
  
-<note tip>Na tym etapie zostanie utworzone rozwiązanie zawierające projekt z szablonem programu C++. Rozwiązanie jest często zwane solucją (z ang. solution).</note>+<note tip>At this stage the solution will be created including the project with the C++ program template.</note>
  
-==== Dodanie do rozwiązania części asemblerowej ====+==== Adding the assembly part to the solution ====
  
-Aby w naszym rozwiązaniu dodać bibliotekę napisaną w asemblerze, dodajemy do solucji nowy projekt. Klikając prawym klawiszem myszy na rozwiązaniu wybieramy dodaj (add) nowy projekt (new project).+To add a library written in assembler to our solution, we add a new project to the solution. Right-click on the solution and select add new project.
  
 {{ :pl:edu:zmitac:n1z:ja:add_asm_1.jpg?800 |}} {{ :pl:edu:zmitac:n1z:ja:add_asm_1.jpg?800 |}}
Line 30: Line 32:
 {{ :pl:edu:zmitac:n1z:ja:add_asm_2.jpg?800 |}} {{ :pl:edu:zmitac:n1z:ja:add_asm_2.jpg?800 |}}
  
-Nasz nowy projekt asemblerowy tworzymy jako projekt biblioteki (library) zgodny z C++, pracujący pod kontrolą systemu operacyjnego Windows. Z listy dostępnych opcji wybieramy bibliotekę dołączaną dynamicznie Dynamic-Link Library (DLL).+We create our new assembler project as a C++ compatible library project, running under the Windows operating system. From the list of available options, we select the Dynamic-Link Library (DLL).
  
 {{ :pl:edu:zmitac:n1z:ja:add_step3.jpg?800 |}} {{ :pl:edu:zmitac:n1z:ja:add_step3.jpg?800 |}}
  
-Nazywamy pojekt np. "JaAsm" i tworzymy go pozostawiając domyślną ścieżkę.+We name the project e.g. "JaAsm" and create it leaving the default path.
  
 {{ :pl:edu:zmitac:n1z:ja:add_step4.jpg?800 |}} {{ :pl:edu:zmitac:n1z:ja:add_step4.jpg?800 |}}
  
-<note tip>Obie części naszego rozwiązania będą umieszczone jako podfoldery w jednym folderze o nazwie zgodniej z nazwą rozwiązania, w naszym przypadku "Lab2_dll_cpp". Każdy z tych podfolderów przyjmie nazwę odpowiednich projektów, czyli "Lab2_dll_cpp" dla części napisanej w C++ i "JaAsm" dla części napisanej w asemblerze.</note>+<note tip>Both parts of our solution will be placed as subfolders in one folder named according to the name of the solution, in our case "Lab2_dll_cpp". Each of these subfolders will take the name of the respective projects, i.e. "Lab2_dll_cpp" for the part written in C++ and "JaAsm" for the part written in assembler.</note>
  
-==== Ustawienie zależności w rozwiązaniu ==== +==== Setting dependencies in the solution ==== 
-Następnym krokiem jest ustawienie zależności projektu w taki sposób, aby kompilacja aplikacji konsolowej w C++ wymuszała automatycznie kompilację biblioteki asemblerowej. W tym celu należy kliknąć prawym klawiszem myszy na projekt konsolowy C++, wybrać menu „Zależności kompilacji” (Build dependencies), a następnie „Zależności projektu” (Project dependencies).+The next step is to set up the project dependencies so that compiling the C++ console application automatically forces the assembly library to be compiled. To do this, right-click on the C++ console project, select the "Build dependencies" menu, and then select "Project dependencies".
  
 {{ :pl:edu:zmitac:n1z:ja:lab2_dependencies_1.jpg?800 |}} {{ :pl:edu:zmitac:n1z:ja:lab2_dependencies_1.jpg?800 |}}
Line 47: Line 49:
 {{ :pl:edu:zmitac:n1z:ja:lab2_dependencies_2.jpg?800 |}} {{ :pl:edu:zmitac:n1z:ja:lab2_dependencies_2.jpg?800 |}}
  
-W nowo otwartym oknie zaznaczamy naszą bibliotekę "JaAsm" i klikamy „OK”+In the newly opened window, select our "JaAsm" library and click "OK".
  
 {{ :pl:edu:zmitac:n1z:ja:lab2_dependencies_3.jpg?800 |}} {{ :pl:edu:zmitac:n1z:ja:lab2_dependencies_3.jpg?800 |}}
  
-<note tip>Po ustawieniu tej zależności, przy kompialcji projektu lub programu napisanego w C++ automatycznie zostanie także przekompilowany plik asemblerowy.</note>+<note tip>Once this dependency is set, when you compile a project or program written in C++, the assembler file will also be recompiled automatically.</note>
  
-==== Zamiana biblioteki z C++ na asemblerową ==== +==== Changing the type from a C++ library to an assembler one ==== 
-Najpierw należy usunąć automatycznie utworzone pliki projektu zgodnego z C++. W tym celu wystarczy zaznaczyć wskazane pliki nagłówkowe i źródłowe, a następnie prawy klawisz myszy i wybrać „Usuń” bądź też kliknąć Delete.+First, you need to remove the automatically created C++ project files. To do this, simply select the header and source files, then right-click and choose "Remove" or click Delete key.
  
 {{ :pl:edu:zmitac:n1z:ja:prepare_step1.jpg?800 |}} {{ :pl:edu:zmitac:n1z:ja:prepare_step1.jpg?800 |}}
  
-Dla biblioteki konieczne jest dostosowanie kompilacji jako plik asemblerowy MASM. W tym celu klikamy projekt biblioteki prawym klawiszem myszy i z listy wybieramy „Zależności kompilacji” (Build dependencies), a następnie „Dostosowanie kompilacji” (Build customization).+For the library, it is necessary to customize the compilation as an MASM assembler file. To do this, right-click the library project and select "Build dependencies" from the list, then "Build customization".
  
 {{ :pl:edu:zmitac:n1z:ja:prepare_step2.jpg?800 |}} {{ :pl:edu:zmitac:n1z:ja:prepare_step2.jpg?800 |}}
Line 64: Line 66:
 {{ :pl:edu:zmitac:n1z:ja:prepare_step3.jpg?800 |}} {{ :pl:edu:zmitac:n1z:ja:prepare_step3.jpg?800 |}}
  
-W nowo otwartym oknie zaznaczamy „masm” i potwierdzamy nasz wybór „OK”.+In the newly opened window, select "masm" and confirm our selection with "OK".
  
 {{ :pl:edu:zmitac:n1z:ja:prepare_step4.jpg?800 |}} {{ :pl:edu:zmitac:n1z:ja:prepare_step4.jpg?800 |}}
  
-Kolejnym krokiem jest dodanie do biblioteki plików źródłowego w asemblerze oraz pliku definicji. Aby to zrobić ponownie klikamy prawym przyciskiem myszy na bibliotekę "JaAsm", a następnie wybieramy „Dodaj” (Add) oraz „Nowy element” (New item).+The next step is to add the source files in the assembler and the definition file to the library. To do this, right-click on the "JaAsm" library again, then select "Add" and "New item".
  
 {{ :pl:edu:zmitac:n1z:ja:prepare_step5.jpg?800 |}} {{ :pl:edu:zmitac:n1z:ja:prepare_step5.jpg?800 |}}
  
-Czynność tę powtarzamy dwukrotnie wybierając za każdym razem "Narzędzie" (Utility) i typ pliku tekstowy, podając nazwy: "JaDll.asm" oraz "JaDll.def".+We repeat this operation twice, each time selecting "Utility" and the text file type, giving the names: "JaDll.asm" and "JaDll.def".
  
 {{ :pl:edu:zmitac:n1z:ja:prepare_step6.jpg?800 |}} {{ :pl:edu:zmitac:n1z:ja:prepare_step6.jpg?800 |}}
  
-<note tip>Przy wykonaniu czynności zgodnie z podaną uprzednio kolejnością, z dużym prawdopodobieństwem krok przedstawiony na kolejnym rysunku nie będzie konieczny.</note>+<note tip>If you perform the activities in the order given above, it is highly likely that the step shown in the next figure will not be necessary.</note>
  
-Aby kompilator wiedział, że stworzony plik asemblerowy jest skojarzony z MASM, należy wskazać jego typ. Krok ten jest niezbędny gdy wykorzystujemy plik źródłowy C++ i zmieniamy jedynie jego rozszerzenie na *.asm. Klikając pracym klawiszem na pliku *.asm wybieramy properties i ustawiamy typ pliku na Microsoft Macro Assembler.+In order for the compiler to know that the created assembler file is associated with MASM, its type must be indicated. This step is necessary when we use a C++ source file and only change its extension to *.asm. By clicking the work key on the *.asm file, we select properties and set the file type to Microsoft Macro Assembler.
  
 {{ :pl:edu:zmitac:n1z:ja:file_type1.jpg?800 |}} {{ :pl:edu:zmitac:n1z:ja:file_type1.jpg?800 |}}
  
  
-==== Ustawienie trybu kompilacji ====+==== Setting the compilation mode ====
  
-Kompilacja powinna przebiegać w trybie 64-bitowym (X64). W tym celu należy ustawić taki tryb w opcjach rozwiązania. +Compilation should be done in 64-bit mode (x64). To do this, set this mode in the solution options. Right-click on our solution and select "Properties".
-Klikamy prawym przyciskiem myszy na nasze rozwiązanie i wybieramy „Właściwości” (Properties).+
  
 {{ :pl:edu:zmitac:n1z:ja:x64_step1.jpg?800 |}} {{ :pl:edu:zmitac:n1z:ja:x64_step1.jpg?800 |}}
Line 92: Line 93:
 {{ :pl:edu:zmitac:n1z:ja:x64_step2.jpg?800 |}} {{ :pl:edu:zmitac:n1z:ja:x64_step2.jpg?800 |}}
  
-W nowo otwartym oknie przechodzimy na zakładkę „Właściwości konfiguracji” -> „Konfiguracja” (Configuration properties -> Configuration) na następnie klikamy w „Menedżer konfiguracji” (Configuration manager).+In the newly opened window, go to the "Configuration properties" -> "Configuration" tab and then click "Configuration manager".
  
 {{ :pl:edu:zmitac:n1z:ja:x64_step3.jpg?800 |}} {{ :pl:edu:zmitac:n1z:ja:x64_step3.jpg?800 |}}
  
-W nowym oknie wskazujemy wszędzie platformę x64 (gdyby dana opcja była niedostępna z dostępnej listy wybieramy pozycję „Nowy” (New), a następnie tworzymy platformę x64 poprzez kopiowanie ustawień z Any CPU). Należy także przy obu częściach solucji włączyć kompilację (Build).+In the new window, we indicate the x64 platform everywhere (if a given option is unavailable, we select "New" from the available list, and then create the x64 platform by copying the settings from Any CPU). You should also enable compilation (Build) for both parts of the solution.
  
 {{ :pl:edu:zmitac:n1z:ja:x64_step4.jpg?800 |}} {{ :pl:edu:zmitac:n1z:ja:x64_step4.jpg?800 |}}
Line 104: Line 105:
 {{ :pl:edu:zmitac:n1z:ja:x64_step6.jpg?800 |}} {{ :pl:edu:zmitac:n1z:ja:x64_step6.jpg?800 |}}
  
-Po wszystkim upewniamy się raz jeszcze, że wszędzie wskazana platforma to x64 i zamykamy okno. +After that, we make sure once again that the platform indicated everywhere is x64 and close the window. The "x64" option should automatically be set in the main menu bar of Visual Studio, but it is worth checking and modifying it if necessary.
-W głównym pasku menu Visual Studio powinna automatycznie ustawić się opcja "x64", niemniej warto to sprawdzić i w razie potrzeby zmodyfikować.+
  
 {{ :pl:edu:zmitac:n1z:ja:x64_step7.jpg?800 |}} {{ :pl:edu:zmitac:n1z:ja:x64_step7.jpg?800 |}}
  
-==== Końcowa konfiguracja biblioteki asemblerowej ====+==== Final setup of the assembly library ====
  
-Przystępujemy do konfiguracji biblioteki. W tym celu klikamy prawym przycskiem myszy na bibliotece i wybieramy „Właściwości” (Properties).+We proceed to configure the library. To do this, right-click on the library and select "Properties".
  
 {{ :pl:edu:zmitac:n1z:ja:config_dll_step1.jpg?800 |}} {{ :pl:edu:zmitac:n1z:ja:config_dll_step1.jpg?800 |}}
Line 117: Line 117:
 {{ :pl:edu:zmitac:n1z:ja:config_dll_step2.jpg?800 |}} {{ :pl:edu:zmitac:n1z:ja:config_dll_step2.jpg?800 |}}
  
-W otwartym nowo oknie przechodzimy do „Konsolidator” -> „Dane wejściowe” (Linker -> Input) i w polu „Plik definicji modułu” (Module definition file) wpisujemy nazwę naszego pliku „*.def”. W naszym przypadku "JaDll.def". Wpis potwierdzamy.+In the newly opened window, go to "Linker" -> "Input" and in the "Module definition file" field, enter the name of our file "*.def". In our case, "JaDll.def". Confirm the entry.
  
 {{ :pl:edu:zmitac:n1z:ja:config_dll_step3.jpg?800 |}} {{ :pl:edu:zmitac:n1z:ja:config_dll_step3.jpg?800 |}}
  
-Końcowym krokiem konfiguracji jest wyłączenie punktu wejścia do biblioteki. W tym celu w zakładce "Konsolidator" (Linker) przechodzimy do opcji "Zaawansowane" (Advanced) i w polu „Brak punktu wejścia” (No entry point) wybieramy opcję „Tak (/NOENTRY)”. Zapisujemy wprowadzone ustawienia i zamykamy okno.+The final step in the configuration is to disable the library entry point. To do this, go to the "Linker" tab and select "Advanced" and select "Yes (/NOENTRY)" in the "No entry point" field. Save the settings and close the window.
  
 {{ :pl:edu:zmitac:n1z:ja:config_dll_step4.jpg?800 |}} {{ :pl:edu:zmitac:n1z:ja:config_dll_step4.jpg?800 |}}
  
-<note tip>Biblioteka dll, w przeciwieństwie do programu głównego, nie ma punktu (adresu) startowego, który wykorzystuje system operacyjny do uruchomienia programu. Udostępnia jedynie zestaw funkcji dla innych programów.</note>+<note tip>A dll library, unlike a main program, does not have a starting point (address) that the operating system uses to start the program. It only provides a set of functions for other programs.</note>
  
-===== Wykorzystanie biblioteki dll napisanej w asemblerze ===== +===== Using a dll library written in assembler ===== 
-==== Biblioteka asemblerowa dll ==== +==== dll library written in assembler ==== 
-Biblioteka dll zawiera funkcje, które można użyć w innych programach. Pierwsza wersja naszej biblioteki będzie udostępniała jedną funkcję, zwracającą stałą wartość. Wartość tę następnie odczytamy w pliku C++. Przykładowa zawartość pliku asemblerowego (JaDll.asm) może wyglądać następująco.+ 
 +The dll library contains functions that can be used in other programs. The first version of our library will provide one function that returns a constant value. We will then read this value in a C++ file. An example of the assembly file (JaDll.asm) may look like this.
  
 <code> <code>
Line 140: Line 141:
 </code> </code>
  
-Z plikiem kodu programu nierozerwalnie związany jest plik definicji (JaDll.def). Plik taki musi zawierać nazwę biblioteki DLL jako agrument dyrektywy LIBRARY, oraz listę nazw funkcji dostępnych dla innych modułów poprzedzonych dyrektywą EXPORTS. Aby nasza funkcja MyProc była widziana przez program C++ musimy ją zadeklarować jako eksportowaną. Plik JaDll.def w naszym projekcie może wyglądać następująco.+The program code file is inextricably linked to the definition file (JaDll.def). Such a file must contain the name of the DLL library as an argument of the LIBRARY directive, and a list of function names available to other modules preceded by the EXPORTS directive. In order for our MyProc function to be seen by the C++ program, we must declare it as exported. The JaDll.def file in our project may look like this.
  
 <code> <code>
Line 147: Line 148:
 </code> </code>
  
-Często chcemy wykorzystać przekazane do funkcji parametry. Przypomnijmy, że w 64-bitowej konwencji wywołań pierwsze cztery parametry są umieszczane bezpośrednio w rejestrach procesora. Szczegóły są opisane w  [[pl:edu:zmitac:n1z:ja:lab1#powtorka_z_teoriifunkcje_i_ich_wywolywanie_z_poziomu_assemblera|instrukcji do pierwszego ćwiczenia]]. I tak, dwa parametry typu całkowitego są umieszczone w rejestrach RCX i RDX. Funkcja, któa zwraca sumę dwóch argmentów całkowitych wyglądałaby w następujący sposób.+We often want to use the parameters passed to the function. Recall that in the 64-bit calling convention, the first four parameters are placed directly in the processor registers. The details were provided during lectures and are described in the instructions for the first lab exercise. Thus, two integer-type parameters are placed in the RCX and RDX registers. A function that returns the sum of two integer arguments would look like this.
  
 <code> <code>
Line 159: Line 160:
 </code> </code>
  
-Argumenty typu zmiennoprzecinkowego należy przekazać przez rejestry XMM0 i XMM1, a wynik zwrócić przez XMM0. Oczywiście należy użyć instrukcji rozszerzeń MMX/SSE.+Floating point arguments should be passed through registers XMM0 and XMM1, and the result should be returned through XMM0. Of course, the MMX/SSE extension instructions should be used.
  
 <code> <code>
Line 169: Line 170:
 ... ...
 </code> </code>
-==== Odwołanie do biblioteki dll z programu C++ ====+==== Calling functions in dll from C++ program ====
  
-Aby program napisany w C++ mógł wykorzystać funkcję z biblioteki należy wykonać kilka kroków. +In order for a program written in C++ to use a function from a library, several steps must be performed. 
-  - Przygotować typ funkcji zgodny z jej późniejszym wywołaniem, +- Prepare the type of the function that is compatible with its later call, 
-  - Załadować bibliotekę funkcją LoadLibrary uzyskując jednocześnie jej uchwyt, +- Load the library using the LoadLibrary function while obtaining its handle, 
-  - Odczytać aktualny adres interesującej nas funkcji wywołując GetProcAddress, +- Read the current address of the function of interest to us by calling GetProcAddress, 
-  - Użyć funkcji odwołując się do jej adresu. +- Use the function by referring to its address. 
-Przykładowy kod mógłby wyglądać tak:+The sample code could look like this:
  
 <code> <code>
Line 192: Line 193:
 </code> </code>
  
-Użycie funkcji z parametrami wymaga nieco zmodyfikowanych definicji typu. Należy także zwrócić uwagę na typ wartości zwracanej przez funkcję. Pamiętać należy, że w przypadku liczb zmiennoprzecinkowych, rozmiar rejestrów XMM0, XMM1 to 64 bity. Odpowiada to wartości zmiennoprzecinkowej podwójnej precyzji, czyli w języku C++ typowi "double".+Using a function with parameters requires slightly modified type definitions. You should also pay attention to the type of the value returned by the function. Remember that in the case of floating-point numbers, the size of the XMM0, XMM1 registers is 64 bits. This corresponds to a double-precision floating-point value, or in C++ to the type "double".
  
 <code> <code>
Line 201: Line 202:
 </code> </code>
  
-==== Zadanie do zrealizowania w ramach laboratorium ==== +==== Task to be completed in the laboratory ==== 
-Należy napisać bibliotekę DLL w asemblerze i program w C++ demonstrujący działanie tej biblioteki.\\ +You should write a DLL in assembler and a C++ program that demonstrates how the DLL works.\\ 
-**Biblioteka DLL** ma udostępniać funkcje wykonujące i zwracające wynik podstawowych obliczeń arytmetycznych (dodawanie, odejmowanie, mnożenie i dzielenie) na parach liczb przekazanych jako parametry ich wywołania. Funkcje powinny mieć wersję stało- i zmiennoprzecinkową (w sumie osiem funkcji).\\ +**The DLL** should provide functions that perform and return the result of basic arithmetic calculations (addition, subtraction, multiplication, and division) on pairs of numbers passed as parameters to their call. The functions should have fixed- and floating-point versions (eight functions in total).\\ 
-**Program w C++** ma wywoływać wszystkie funkcje przekazując przykładowe wartości parametrów i wyświetlić wyniki na ekranie. Nie jest wymagany interfejs użytkownika - wartości do obliczeń mogą być umieszczone w kodzie programu.+**The C++ program** should call all functions, pass sample parameter values, and display the results on the screen. No input user interface is required - the values ​​for calculations can be placed in the program code. 
 + 
 +==== For the report ==== 
 +Include the code of your C++ file and assembler source file WITH COMMENTS. Also, add a screenshot of the window with the results displayed.
  
en/edu/zmitac/s1/apl/lab/lab2.1732530880.txt.gz · Last modified: by ktokarz