This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:multiasm:papc:chapter_6_8 [2025/11/25 11:47] – [Callig Linux system functions] ktokarz | en:multiasm:papc:chapter_6_8 [2025/11/25 12:46] (current) – [Calling the system functions] ktokarz | ||
|---|---|---|---|
| Line 75: | Line 75: | ||
| ===== Calling the system functions ===== | ===== Calling the system functions ===== | ||
| - | The operating systems offer a set of functions which help write an application. These functions include reading characters and text from standard input, usually the keyboard, displaying characters or text on standard output, usually the monitor, handling files, data streams and many others. In previous generations of operating systems, the software interrupt mechanism was used. In Microsoft DOS, it was **int 21h** while in 32-bit versions of Linux it was **int 80h**. Calling the system function required preparing the arguments in scratch registers and signalling the software interrupt. | + | The operating systems offer a set of functions which help write an application. These functions include reading characters and text from standard input, usually the keyboard, displaying characters or text on standard output, usually the monitor, handling files, data streams and many others. In previous generations of operating systems, the software interrupt mechanism was used. In Microsoft DOS, it was **int 21h** while in 32-bit versions of Linux it was **int 80h** (or in the C-style hex notation int 0x80). Calling the system function required preparing the arguments in scratch registers and signalling the software interrupt. |
| < | < | ||
| You can still find many examples using the software interrupt system call on the Internet. In Linux, they should work properly, although they are slower than the new method. In 64-bit Windows, the **int 21** method is no longer supported. | You can still find many examples using the software interrupt system call on the Internet. In Linux, they should work properly, although they are slower than the new method. In 64-bit Windows, the **int 21** method is no longer supported. | ||
| Line 107: | Line 107: | ||
| ; after the function call is aligned to mod(8) | ; after the function call is aligned to mod(8) | ||
| ; the Windows requires the shadow space on the stack | ; the Windows requires the shadow space on the stack | ||
| - | push rbp ; push rpb to the stack | + | |
| - | mov rbp, rsp ; store rsp to rbp | + | mov |
| - | sub rsp, 48 ; shadow space (32 bytes) and stack alignment (additional 8 bytes) | + | sub |
| ; we need the handle of the console window | ; we need the handle of the console window | ||
| - | mov rcx, STD_OUTPUT_HANDLE | + | |
| - | call GetStdHandle | + | call GetStdHandle |
| - | mov stdout_handle, | + | mov |
| ; display the text in the console window | ; display the text in the console window | ||
| - | mov rcx, stdout_handle | + | |
| - | mov rdx, offset hello_msg | + | mov |
| - | mov r8, sizeof hello_msg | + | mov |
| - | mov r9, dummy | + | mov |
| - | call | + | call WriteConsoleA |
| ; restore the stack pointer and rbp | ; restore the stack pointer and rbp | ||
| - | mov rsp, rbp | + | |
| - | pop rbp | + | pop |
| ; return from the function | ; return from the function | ||
| - | ret | + | |
| MyAssemblerFunction ENDP | MyAssemblerFunction ENDP | ||
| END | END | ||
| Line 134: | Line 134: | ||
| ===== Callig Linux system functions ===== | ===== Callig Linux system functions ===== | ||
| - | The Linux operating system still supports the traditional calling of system functions using software interrupts. It is based on the **int 80h** interrupt, which recognises the number of the function in the EAX register and up to six arguments in EBX, ECX, EDX, ESI, EDI, and EBP. | + | The Linux operating system still supports the traditional calling of system functions using software interrupts. It is based on the **int 0x80** interrupt, which recognises the number of the function in the EAX register and up to six arguments in EBX, ECX, EDX, ESI, EDI, and EBP. |
| The example of the Hello World program in Linux interrupt-based system call is shown in the following code. | The example of the Hello World program in Linux interrupt-based system call is shown in the following code. | ||
| Line 153: | Line 153: | ||
| section | section | ||
| - | msg db "Hello World!" | + | msg db "Hello World!" |
| len equ $ - msg | len equ $ - msg | ||
| </ | </ | ||
| Line 178: | Line 178: | ||
| msg: db "Hello World!", | msg: db "Hello World!", | ||
| - | len equ $ - hello_world | + | len equ $ - msg |
| </ | </ | ||