In the terminal, navigate to the directory containing hello.asm and run the following command to assemble the code into an object file:
nasm -f elf32 -o hello.o hello.asm
Use the linker to create an executable from the object file:
ld -m elf_i386 -o hello hello.o
Finally, execute the program:
./hello
- You installed NASM and GCC.
- Wrote an assembly program in VS Code.
- Assembled it using nasm.
- Linked it using ld.
- Ran it to print "Hello, World!" in the terminal.
Open a terminal on Ubuntu and run:
nasm -f bin filename.asm -o filename.com
This command converts your assembly code to a .com
file.
After assembling the .com
file, you can run it within DOSBox:
mount c /path/to/your/directory c:
filename.com
nasm file.asm -o file.com
nasm file.asm -o file.com -l file.lst
file.com
afd file.com
If your file uses modern NASM syntax and is correctly formatted, assemble it like this:
nasm -f elf64 yourfile.asm -o yourfile.o
For first one
ld -o yourfile yourfile.o
./yourprogram
If you encounter issues, consider the following:
Ensure that the file paths used in the mov rsi, msg instruction correctly point to the message in your .asm file.
Verify that the syscall numbers used (1 for sys_write and 60 for sys_exit) are correct for your system.
Ensure the executable has execute permissions. If not, set the correct permissions using:
chmod +x yourprogram
If you encounter issues or need to debug your assembly program, follow these steps:
Generate debugging information during the assembly process:
nasm -f elf64 -g yourfile.asm -o yourfile.o
ld -g -o yourprogram yourfile.o 3.
Use gdb (GNU Debugger) to debug your program:
gdb ./yourprogram
Inside gdb, you can set breakpoints and step through the code to identify where it might be failing. For example:
Set a Breakpoint: break \_start
Run the Program: run
Step Through Code: step
Continue Execution: continue
Print Registers or Variables: print rax
(or other registers)