No ACL setup yet! Denying access to everyone.
en:edu:zmitac:s1:apl:lab:lab1
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:edu:zmitac:s1:apl:lab:lab1 [2024/11/18 21:44] – [Practice: Creating a C++ project and conversion to ASM] ktokarz | en:edu:zmitac:s1:apl:lab:lab1 [2026/09/16 11:58] (current) – Accessibility: add WCAG-compliant alt texts to all nine figures mcp_agent | ||
|---|---|---|---|
| Line 2: | Line 2: | ||
| ===== Laboratory 1 ===== | ===== Laboratory 1 ===== | ||
| - | Programming | + | Configuration of the Visual Studio for simple assembler application written |
| - | ==== Instroduction | + | ===== Introduction ===== |
| In this exercise, you will create a console application in pure assembler for x64 (Intel/AMD processors, 64 bits) that will interact with the Windows operating system (Windows 7, 8, 8.1, 10, 11) by calling its functions via the Windows API. | In this exercise, you will create a console application in pure assembler for x64 (Intel/AMD processors, 64 bits) that will interact with the Windows operating system (Windows 7, 8, 8.1, 10, 11) by calling its functions via the Windows API. | ||
| Line 84: | Line 84: | ||
| Create a new project: a command-line (console) application for C++, native (__not C++ .NET__) figure {{ref> | Create a new project: a command-line (console) application for C++, native (__not C++ .NET__) figure {{ref> | ||
| <figure jalabstep1> | <figure jalabstep1> | ||
| - | {{: | + | {{: |
| < | < | ||
| </ | </ | ||
| Line 91: | Line 91: | ||
| Delete the generated .cpp file (from VS) and then add a new .asm file (this can be done by clicking on the project (not the solution!) and selecting to add a new element figure {{ref> | Delete the generated .cpp file (from VS) and then add a new .asm file (this can be done by clicking on the project (not the solution!) and selecting to add a new element figure {{ref> | ||
| <figure jalabstep21> | <figure jalabstep21> | ||
| - | {{: | + | {{: |
| < | < | ||
| </ | </ | ||
| <figure jalabstep22> | <figure jalabstep22> | ||
| - | {{: | + | {{: |
| < | < | ||
| </ | </ | ||
| Line 103: | Line 103: | ||
| <figure jalabstep31> | <figure jalabstep31> | ||
| - | {{: | + | {{: |
| < | < | ||
| Line 109: | Line 109: | ||
| <figure jalabstep32> | <figure jalabstep32> | ||
| - | {{: | + | {{: |
| < | < | ||
| Line 116: | Line 116: | ||
| Next, change the type of application being built from '' | Next, change the type of application being built from '' | ||
| <figure jalabstep4> | <figure jalabstep4> | ||
| - | {{: | + | {{: |
| < | < | ||
| </ | </ | ||
| Line 129: | Line 129: | ||
| <figure jalabstep51> | <figure jalabstep51> | ||
| - | {{:pl: | + | {{:en: |
| < | < | ||
| </ | </ | ||
| Line 140: | Line 140: | ||
| Details in figure {{ref> | Details in figure {{ref> | ||
| <figure jalabstep52> | <figure jalabstep52> | ||
| - | {{: | + | {{: |
| < | < | ||
| </ | </ | ||
| Line 147: | Line 147: | ||
| In some cases, the '' | In some cases, the '' | ||
| <figure jalabstep6> | <figure jalabstep6> | ||
| - | {{: | + | {{: |
| < | < | ||
| </ | </ | ||
| Line 247: | Line 247: | ||
| In the program, we use interaction with graphic elements such as the main window (Foreground Window) and the data stream (STD_OUT). When calling the function, it is necessary to clearly indicate which graphic object we want to interact with or create a child object (e.g. MessageBox).\\ | In the program, we use interaction with graphic elements such as the main window (Foreground Window) and the data stream (STD_OUT). When calling the function, it is necessary to clearly indicate which graphic object we want to interact with or create a child object (e.g. MessageBox).\\ | ||
| To do this, we need to get two handles: | To do this, we need to get two handles: | ||
| - | * a handle to the current main application window at the operating system level (so-called Foreground Window) | + | |
| - | * a handle to a terminal-type data stream, into which information can be entered. | + | * a handle to a terminal-type data stream, into which information can be entered. |
| - | <note important> | + | <note important> |
| To read the handles, two functions from the Windows API will be useful, which will return pointers (handles) to the appropriate objects. These values should be saved and can be used multiple times. | To read the handles, two functions from the Windows API will be useful, which will return pointers (handles) to the appropriate objects. These values should be saved and can be used multiple times. | ||
| - | Reading the handle to the main window, this handle should be used when invoking a dialog | + | Reading the handle to the main window, this handle should be used when invoking a dialogue |
| < | < | ||
| ... | ... | ||
| Line 260: | Line 260: | ||
| ... | ... | ||
| </ | </ | ||
| + | |||
| + | In the above example, the value returned by the function (via the RAX register) is saved in the variable '' | ||
| + | |||
| + | To read the handle to the inside of the terminal, you need to use another function: | ||
| + | < | ||
| + | ... | ||
| + | mov rcx, STD_OUTPUT_HANDLE | ||
| + | call GetStdHandle | ||
| + | mov stdHandle, rax | ||
| + | ... | ||
| + | </ | ||
| + | However, this function requires one argument, which data stream we want. There are three possibilities: | ||
| + | * STD_INPUT_HANDLE = -10 - standard input - it can be used to read data from the console | ||
| + | * STD_OUTPUT_HANDLE = -11 - standard output - it is used to write something in the terminal; remember the ending ENTER, i.e. '' | ||
| + | * STD_ERROR_HANDLE = -12 - stream for errors; | ||
| + | |||
| + | An example code that displays the text in the variable (at the address) '' | ||
| + | < | ||
| + | ... | ||
| + | ; WINAPI WriteConsoleA( | ||
| + | ; RCX => _In_ HANDLE hConsoleOutput, | ||
| + | ; RDX => _In_ const VOID *lpBuffer, | ||
| + | ; R8 => _In_ DWORD nNumberOfCharsToWrite, | ||
| + | ; R9 => _Out_opt_ LPDWORD lpNumberOfCharsWritten, | ||
| + | mov rcx, stdHandle | ||
| + | lea rdx, text_msg | ||
| + | mov r8, sizeof text_msg | ||
| + | mov r9, dummy | ||
| + | call WriteConsoleA | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | To display a dialogue box, use a pointer to the main application window. The parameters passed in this order are a handle, two pointers, to texts (they must end with a zero, RDX is a pointer to the content that the dialogue box will display, and R8 is a pointer to the window header that will be given to the window) that are to appear in header and dialogue body and finally, a binary mask specifying which buttons and icons to use in the dialogue box ((https:// | ||
| + | < | ||
| + | ... | ||
| + | ; WINUSERAPI int WINAPI MessageBoxA( | ||
| + | ; RCX => _In_opt_ HWND hWnd, | ||
| + | ; RDX => _In_opt_ LPCSTR lpText, | ||
| + | ; R8 => _In_opt_ LPCSTR lpCaption, | ||
| + | ; R9 => _In_ UINT uType); | ||
| + | mov rcx, handler | ||
| + | mov rdx, offset hello_msg | ||
| + | mov r8, offset info_msg | ||
| + | mov r9, 0 ; MB_OK | ||
| + | and rsp, not 8 | ||
| + | call MessageBoxA | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | <note important> | ||
| + | |||
| + | ==== Tasks to be done for the report ==== | ||
| + | Let's start wth debugging. If the program runs correctly please set the breakpoint at the beginning of assembly code. Open register window and observe the content of registers after instruction execution. Please do not step into Windows functions including WriteConsoleA() and MessageBoxA(), | ||
| + | * RIP - note how its content differs with every instruction in the series. Do changes depend on the type of instruction executed? Compare changes with the disassembly window and the number of bytes instructions occupy. | ||
| + | * While calling WriteConsoleA look at the memory dump window at the address stored in RDX. What is there in the memory? You can try different view modes. | ||
| + | * While calling MessageBoxA function look at the memory dump window at the address stored in RDX and in R8. What is there in the memory? You can try different view modes. | ||
| + | * Include in the program the instruction which sets the RAX to some chosen value e.g. 00FFh before calling previously mentioned functions. Does the RAX register content change after returning from these functions? | ||
| + | * Try to replace the instruction "mov rdx, offset hello_msg" | ||
| + | |||
| + | |||
| + | ==== Method of results verification ==== | ||
| + | The texts and windows that appear on the screen should correspond to the algorithm. Pay special attention to whether random strings of characters are generated, which may suggest that you forgot about the ending 0. | ||
en/edu/zmitac/s1/apl/lab/lab1.1731962698.txt.gz · Last modified: by ktokarz
