Questions tagged [nasm]

NASM is the Netwide Assembler, an open-source x86/x64 assembler. It aims at being portable, modular and at having a simple syntax.

NASM is the Netwide Assembler, an open-source x86/x64 assembler. It aims at being portable, modular and at having a simpler syntax than the most commonly used open-source assembler gas. It supports a whole range of output formats (including ELF, PE/COFF), plain binary (a.out) and supports Intel 64, 32, 16 and 8 bit syntaxes.

For x86 assembly info in general, see the many links to reference manuals/docs, optimization/performance guides, tools, and debugging tips in the x86 tag wiki

See also:

  • The NASM homepage

  • The NASM manual

  • An older version of the NASM Appendix A that has text descriptions with every instruction entry, along with the CPU they were introduced in (8086, 186, 386, etc.) But it only includes MMX and older; the current version of the appendix stripped the text because SSE2/AVX/etc. have so many instructions.

  • https://yasm.tortall.net/ YASM is a NASM-compatible assembler with some nice features (e.g. long NOPs by default), but development has stalled and it doesn't support AVX512.

  • x264 has a very large set of NASM macros that attempt to abstract the calling conventions of x86_32, win64, linux64, and also do CPU feature-level checking. (e.g. to declare a function/block as SSSE3, and catch accidental usage of an SSE4.1 instruction).

    It is very intrusive and makes your source code look significantly different from normal x86 asm (macros for register names). It's licensed separately (ISC, not GPL) so it can be used in other projects.

    One copy of it can be found in the libvpx (VP8/9 video codec) source tree. x264 itself also has a copy, and see those projects for DSP functions using it.

5046 questions
7
votes
1 answer

Linux NASM detect EOF

I'm trying to learn the basics asm on linux and I can't find a very good reference. The NASM docs seem to assume you already know masm... I found no examples in the documentation of the cmp (outside the Intel instruction reference). I'd written a…
tMC
  • 18,105
  • 14
  • 62
  • 98
7
votes
2 answers

nasm/gcc issue on 64-bit Mac OS X Lion

I was reading this article, and at one point it gives me this nasm program: ; tiny.asm BITS 32 GLOBAL main SECTION .text main: mov eax, 42 ret And tells me to run the following commands: $ nasm -f elf tiny.asm $ gcc…
math4tots
  • 8,540
  • 14
  • 58
  • 95
7
votes
4 answers

Why does the mov instruction have use ax instead of two segment registers directly?

I see code like: mov ax, cs mov ds, ax mov es, ax Why can't I just compress this to: mov ds, cs mov es, cs Is the first way faster since its using the accumulator register? But that wouldn't seem intuitive since cs and ds are segment registers. Or…
samoz
  • 56,849
  • 55
  • 141
  • 195
7
votes
5 answers

Printf without newline in assembly

I've recently read this article on using printf and scanf in assembly: Meaning of intfmt: db "%d", 10, 0 in assembly In particular it says "In printf, the newline prints a newline and then (if the output is in line buffered mode, which it probably…
Sarah Tattersall
  • 1,275
  • 2
  • 21
  • 32
7
votes
1 answer

Why does printf still work with RAX lower than the number of FP args in XMM registers?

I am following the book "Beginning x64 Assembly Programming", in Linux 64 system. I am using NASM and gcc. In the chapter about floating point operations the book specifies the below code for adding 2 float numbers. In the book, and other online…
pesotsan
  • 73
  • 3
7
votes
1 answer

Why would I use NASM over GNU Assembler (GAS)?

Why would I use NASM over GNU Assembler (GAS) when GAS supports multiple architectures whereas NASM supports the x86 architecture exclusively? I know that there are syntactic differences, but that doesn't bother me too much.
Serket
  • 3,785
  • 3
  • 14
  • 45
7
votes
2 answers

Debugging NASM in VS code

I have a simple C++ program that calls some NASM code: main.cpp: #include extern "C" int foo(); int main() { std::cout << "The result is: " << foo() << std::endl; return 0; } foo.asm: bits 64 global foo section .text foo: mov…
Touloudou
  • 2,079
  • 1
  • 17
  • 28
7
votes
1 answer

Solution needed for building a static IDT and GDT at assemble/compile/link time

This question is inspired by a problem many have encountered over the years, especially in x86 operating system development. Recently a related NASM question was bumped up by an edit. In that case the person was using NASM and was getting the…
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
7
votes
3 answers

How can I get nasm to work as a command in terminal in MacOS?

I have been trying to get nasm to work as a command in terminal, but nothing seems to be working. I just keep getting the error: nasm: error: unable to find utility "nasm", not a developer tool or in PATH So, first, I tried updating my PATH…
Dinosaur212
  • 123
  • 2
  • 9
7
votes
2 answers

Minimal opcode size x86-64 strlen implementation

I'm investigating a minimal opcode size x86-64 strlen implementation for my code golfing / binary executable that is not supposed to exceed some size (think of demoscene for simplicity). General idea comes from here, size optimization ideas from…
Kamil.S
  • 5,205
  • 2
  • 22
  • 51
7
votes
1 answer

Mach-O 64-bit format does not support 32-bit absolute addresses. NASM Accessing Array

Running this code off my Mac computer, using command: nasm -f macho64 -o max.a maximum.asm This is the code I am attempting to run on my computer that finds the largest number inside an array. section .data data_items: dd…
Apthos
  • 73
  • 1
  • 6
7
votes
2 answers

Are the prologue and epilogue mandatory when writing assembly functions?

Recently I rewrote some libc functions in assembly and for some of them (the one that didn't need any call or syscall instructions, like strlen), I discarded the prologue and epilogue because my tests didn't failed without it (maybe they were not…
shorty_ponton
  • 454
  • 4
  • 14
7
votes
1 answer

How to create an ELF executable on Mac OS X?

I'm following this tutorial: https://littleosbook.github.io/#linking-the-kernel and I'm on a Mac. I'm at the point where I have an object file created from nasm, and I want to turn it into an ELF executable using ld. The ld on a mac doesn't look…
cstack
  • 2,152
  • 6
  • 28
  • 47
7
votes
1 answer

How can I create a bootable CD image with my kernel?

I have a kernel, to boot I am using command qemu-system-i386 -kernel kernel.bin. Is any way to create bootable disk image to boot with qemu-system-i386 -cdrom CD.iso? The code I am compiling in linux with these commands: nasm -f elf32 kernel.asm -o…
shy_potato
  • 137
  • 1
  • 9
7
votes
2 answers

nasm/ld "relocation truncated to fit: R_386_16"

Assembly: [BITS 16] global _start _start: mov ax, 0x07C0 mov ds, ax mov si, hw call print_string jmp $ print_string: mov ah, 0x0E .char: lodsb cmp al, 0 je .exit int 0x10 jmp .char .exit: ret times…
DutChen18
  • 1,133
  • 1
  • 7
  • 24