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:52] – [Creating the project with dll library written in assembler] ktokarzen:edu:zmitac:s1:apl:lab:lab2 [2024/11/26 13:26] (current) – [Laboratory2] ktokarz
Line 1: Line 1:
-====== Lab 2: 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.
  
 ===== Creating the project with dll library written in assembler ===== ===== Creating the project with dll library written in assembler =====
Line 53: Line 55:
 <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> <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.1732531939.txt.gz · Last modified: by ktokarz