Questions tagged [x86]

x86 is an architecture derived from the Intel 8086 CPU. The x86 family includes the 32-bit IA-32 and 64-bit x86-64 architectures, as well as legacy 16-bit architectures. Questions about the latter should be tagged [x86-16] and/or [emu8086]. Use the [x86-64] tag if your question is specific to 64-bit x86-64. For the x86 FPU, use the tag [x87]. For SSE1/2/3/4 / AVX* also use [sse], and any of [avx] / [avx2] / [avx512] that apply

The x86 family of CPUs contains 16-, 32-, and 64-bit processors from several manufacturers, with backward-compatible instruction sets, going back to the Intel 8086 introduced in 1978.

There is an tag for things specific to that architecture, but most of the info here applies to both. It makes more sense to collect everything here. Questions can be tagged with either or both. Questions specific to features only found in the x86-64 architecture, like RIP-relative addressing, clearly belong in x86-64. Questions like "how to speed up this code with vectors or any other tricks" are fine for x86, even if the intention is to compile for 64bit.

Related tag with tag-wikis:

  • wiki (some good SIMD guides), and (not much there)
  • wiki for guides specific to interfacing with a compiler that way.
  • wiki and wiki have more details about the differences between the two major x86 assembly syntaxes. And for Intel, how to spot which flavour of Intel syntax it is, like NASM vs. MASM/TASM.

Learning resources


Guides for performance tuning / optimisation:

Instruction set / asm syntax references:

OS-specific stuff: ABIs and system-call tables:






  • 16bit interrupt list: PC BIOS system calls (int 10h / int 16h / etc, AH=callnumber), DOS system calls (int 21h/AH=callnumber), and more.

memory ordering:

Specific behaviour of specific implementations

Q&As with good links, or directly useful answers:


FAQs / canonical answers:

If you have a problem involving one of these issues, don't ask a new question until you've read and understood the relevant Q&A.

(TODO: find better question links for these. Ideally questions that make a good duplicate target for new dups. Also, expand this.)


How to get started / Debugging tools + guides

Find a debugger that will let you single-step through your code, and display registers while that happens. This is essential. We get many questions on here that are something like "why doesn't this code work" that could have been solved with a debugger.

On Windows, Visual Studio has a built-in debugger. See Debugging ASM with Visual Studio - Register content will not display. And see Assembly programming - WinAsm vs Visual Studio 2017 for a walk-through of setting up a Visual Studio project for a MASM 32-bit or 64-bit Hello World console application.

On Linux: A widely-available debugger is gdb. See Debugging assembly for some basic stuff about using it on Linux. Also How can one see content of stack with GDB?

There are various GDB front-ends, including GDBgui. Also guides for vanilla GDB:

With layout asm and layout reg enabled, GDB will highlight which registers changes since the last stop. Use stepi to single-step by instructions. Use x to examine memory at a given address (useful when trying to figure out why your code crashed while trying to read or write at a given address). In a binary without symbols (or even sections), you can use starti instead of run to stop before the first instruction. (On older GDB without starti, you can use b *0 as a hack to get gdb to stop on an error.) Use help x or whatever for help on any command.

GNU tools have an Intel-syntax mode that's similar to MASM, which is nice to read but is rarely used for hand-written source (NASM/YASM is nice for that if you want to stick with open-source tools but avoid AT&T syntax):

Another key tool for debugging is tracing system calls. e.g. on a Unix system, strace ./a.out will show you the args and return values of all the system calls your code makes. It knows how to decode the args into symbolic values like O_RDWR, so it's much more convenient (and likely to catch brain-farts or wrong values for constants) than using a debugger to look at registers before/after an int or syscall instruction. Note that it doesn't work correctly on Linux int 0x80 32-bit ABI system calls in 64-bit processes: What happens if you use the 32-bit int 0x80 Linux ABI in 64-bit code?.

To debug boot or kernel code, boot it in Bochs, qemu, or maybe even DOSBox, or any other virtual machine / simulator / emulator. Use the debugging facilities of the VM to get way better information than the usual "it locks up" you will experience with buggy privileged code.

Bochs is generally recommended for debugging real-mode bootloaders, especially ones that switch to protected mode; Bochs's built-in debugger understands segmentation (unlike GDB), and can parse a GDT, IDT, and page tables to make sure you got the fields right.

For DOS programs, see the x86-16 tag wiki for debuggers that run inside the guest, and thus can debug a specific DOS program maybe more easily than Bochs for the whole system.


REPL (Read Eval Print Loop) environments for typing an instruction and seeing what it does to register values. Maybe only useful for user-space, perhaps not osdev stuff.

16952 questions
8
votes
4 answers

What is this x86 inline assembly doing?

I came across this code and need to understand what it is doing. It just seems to be declaring two bytes and then doing nothing... uint64_t x; __asm__ __volatile__ (".byte 0x0f, 0x31" : "=A" (x)); Thanks!
MK.
  • 3,907
  • 5
  • 34
  • 46
8
votes
4 answers

Why is the dividend 64 bits in x86 assembly?

Why does the idiv x86 assembly instruction divide EDX:EAX (64 bits) by a given register whereas other mathematical operations, including multiplication, simply operate on single input and output registers? Multiplication: mov eax, 3 imul eax,…
Overv
  • 8,433
  • 2
  • 40
  • 70
8
votes
2 answers

Why can't I change the value of a segment register? (MASM)

I decided to teach myself assembly language. I have realized that my program will not compile if I attempt to change the value of any segment register. Every article that I have found says that I can indeed change the value of at least 4 segment…
Ed S.
  • 122,712
  • 22
  • 185
  • 265
8
votes
4 answers

how do procedure calls work in assembler?

I just started tinkering with ASM and I'm not sure if my understanding of procedure calls is correct. say at some point in the code there is a procedure call call dword ptr[123] and the procedure consists of only one command, ret: ret 0004 what…
int3
  • 12,861
  • 8
  • 51
  • 80
8
votes
2 answers

Why can't I save the value of rip?

#include uint64_t rip; int main() { asm( "movq %%rip, %0\n" : "=m" (rip) ); sleep(10); } When I compile I get cc -m64 rip.c -o rip /tmp/ccwNbZi1.s: Assembler messages: /tmp/ccwNbZi1.s:12: Error: suffix or…
dbbd
  • 864
  • 1
  • 8
  • 23
8
votes
3 answers

Is it possible for evolutionary algorithms to create machine code?

This is a question of general interest, as I am not trying to solve a specific problem. I have looked around to try to find some articles that cover this area, but am struggling to even put together some good search terms. Let's start with what I…
8
votes
2 answers

How to detect that a given PE file (exe or dll) is 64 bit or 32 bit

I need to detect whether a given .dll or .exe file is 32 bit or 64 bit At the moment I have only one solution: read the PE Header from the specified file and take the 'Machine' field from there. ( Specification: Microsoft Portable Executable and…
inazaruk
  • 74,247
  • 24
  • 188
  • 156
8
votes
4 answers

How to calculate MIPS of my processor?

I have an old PC. I want to calculate MIPS(Million Instruction Per Second) and DMIPS of its processor exactly. What can I do for this?
Soroush
  • 989
  • 2
  • 10
  • 16
8
votes
1 answer

gdb find memory address of line number

Say I have attached gdb to a process and within the its memory layout there is a file and line number which I would like the memory address of. How can I get the memory address of line n in file x? This is on Linux x86.
Bhubhu Hbuhdbus
  • 1,489
  • 6
  • 24
  • 31
8
votes
5 answers

How to make a C program that can run x86 hex codes

I have an array of hex codes that translate into assembly instructions and I want to create program in C that can execute these. unsigned char rawData[5356] = { 0x4C, 0x01, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x0C, 0x00, 0x00, 0x3D,…
Iowa15
  • 3,027
  • 6
  • 28
  • 35
8
votes
1 answer

Simulating packusdw functionality with SSE2

I'm implementing a fast x888 -> 565 pixel conversion function in pixman according to the algorithm described by Intel [pdf]. Their code converts x888 -> 555 while I want to convert to 565. Unfortunately, converting to 565 means that the high bit is…
mattst88
  • 1,462
  • 13
  • 21
8
votes
2 answers

How does the system choose the right Page Table?

Let's focus on uniprocessor computer systems. When a process gets created, as far as I know, the page table gets set up which maps the virtual addresses to the physical memory address space. Each process gets its own page table, stored in the kernel…
saimn
  • 195
  • 2
  • 8
8
votes
3 answers

How to tell gcc to stop using built-in functions?

I am using my own modified glibc. I saw in the compiled code that compiler was not using many standard library functions from my glibc when I linked with it. Then I put -fno-builtin flag. Things got better and I could see that many functions which…
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
8
votes
3 answers

How to check keys status in x86 assembly?

I took x86 assembly as a hobby this past january so I could make games that would work on old 8086-powered computers like the PCj and Tandy 1000, but the books I found don't exactly teach much on that specific topic. While some dos and bios…
DieJay
  • 95
  • 1
  • 1
  • 6
8
votes
1 answer

Identifying faulting address on General Protection Fault (x86)

I am trying to write a ISR for the General Protection Fault (GP#13) on x86. I am unable to figure out from the INTEL docs as to how I can find out the faulting address causing the exception. I know that for Page fault exceptions (GP#14) the cr2…
rss
  • 83
  • 1
  • 3
1 2 3
99
100