- 1 year ago
Printing hello world in nasm windows
Comment
1 / 1
To print "Hello, World!" using NASM (Netwide Assembler) on Windows, you'll need to create an assembly program that calls the Windows API to display the text. Here's a simple example:
- Create an Assembly File:
Create a file namedhello.asm
and open it with a text editor. Add the following assembly code:
assemblysection .data
hello db "Hello, World!", 0
section .text
global _start
_start:
; Get standard output handle
mov eax, 0x2 ; sysnum: GetStdHandle
mov ebx, -11 ; arg1: STD_OUTPUT_HANDLE
int 0x80 ; Call Windows API
; Write "Hello, World!" to stdout
mov eax, 0x4 ; sysnum: WriteFile
mov ebx, eax ; arg1: handle (stdout)
mov ecx, hello ; arg2: buffer
mov edx, 13 ; arg3: count (length of hello)
int 0x80 ; Call Windows API
; Exit the program
mov eax, 0x1 ; sysnum: ExitProcess
xor ebx, ebx ; arg1: exit code
int 0x80 ; Call Windows API
- Assemble and Link:
Open a Command Prompt and navigate to the directory where yourhello.asm
file is located. Then, assemble and link the code using NASM and a linker likeld
:
bashnasm -f win32 hello.asm
ld -o hello.exe hello.obj
- Run the Program:
Execute the resultinghello.exe
executable to see the "Hello, World!" message:
bashhello.exe
Keep in mind that this example uses 32-bit assembly and the int 0x80
instruction for system calls. This approach is specific to older versions of Windows and x86 architecture. If you're using a more recent version of Windows or a different architecture, you might need to adapt the code accordingly.
Note that this example demonstrates the low-level Windows API calls for educational purposes. In a practical scenario, you'd likely use higher-level programming languages or libraries to interact with the operating system and display messages.