How to use nasm and ld to write assembly code!
Create a new bash repl. We will be using a bash script to automate assembling, linking, and running, although normally that would be a job for
Step 2: Create a new file called main.asm.
Step 3: In order to test, we'll put a simple hello world program in. If you want to learn about how this works, check the link above. Here's the code:
section .text global _start ;must be declared for linker (ld) _start: mov edx,len mov ecx,msg mov ebx,1 mov eax,4 int 0x80 mov eax,1 int 0x80 section .data msg db 'Hello, world!', 0xa len equ $ - msg
Step 4: Now let's add the assembler to our script.
Add this to the bash script:
nasm -f elf -o main.o main.asm
If you've ever used gcc, the -o option might be familiar to you. It's for determining output. Put the main.asm wherever you like, it's the input. Don't put it after the -f or -o, though.
Step 5: Let's pass our file through the linker and run it!
Add this to your bash script:
ld -m elf_i386 -s -o main main.o
Like nasm, -o means output and you can put the main.o wherever, except after -o and -m. Make sure that the input to ld is the output from nasm. In order to run it, type ./PATH/TO/main, in this case ./main.
If it says Hello World, you're good to go!