Questions tagged [assembly]

Assembly language questions. Please tag the processor and/or the instruction set you are using, as well as the assembler, a valid set should be like this: ([assembly] [x86] [gnu-assembler] or [att]). Use the [.net-assembly] tag instead for .NET assemblies, [cil] for .NET assembly language, and for Java bytecode, use the tag java-bytecode-asm instead.

Assembly is a family of very low-level programming languages, just above machine code. In assembly, each statement corresponds to a single machine code instruction. These instructions are represented as mnemonics in the given assembly language and are converted into executable machine code by a utility program referred to as an assembler; the conversion process is referred to as assembly, or assembling the code.

Language design

Basic elements

There is a large degree of diversity in the way that assemblers categorize statements and in the nomenclature that they use. In particular, some describe anything other than a machine mnemonic or extended mnemonic as a pseudo-operation (pseudo-op). A typical assembly language consists of three types of instruction statements that are used to define program operations:

  • Opcode mnemonics
  • Data sections
  • Assembly directives

Opcode mnemonics and extended mnemonics

Instructions (statements) in assembly language are generally very simple, unlike those in high-level languages. Generally, a mnemonic is a symbolic name for a single executable machine language instruction (an opcode), and there is at least one opcode mnemonic defined for each machine language instruction. Each instruction typically consists of an operation or opcode plus zero or more operands. Most instructions refer to a single value, or a pair of values. Operands can be immediate (value coded in the instruction itself), registers specified in the instruction or implied, or the addresses of data located elsewhere in storage. This is determined by the underlying processor architecture: the assembler merely reflects how this architecture works. Extended mnemonics are often used to specify a combination of an opcode with a specific operand. For example, the System/360 assemblers use B as an extended mnemonic for BC with a mask of 15 and NOP for BC with a mask of 0.

Extended mnemonics are often used to support specialized uses of instructions, often for purposes not obvious from the instruction name. For example, many CPU's do not have an explicit NOP instruction, but do have instructions that can be used for the purpose. In 8086 CPUs the instruction xchg ax,ax is used for nop, with nop being a pseudo-opcode to encode the instruction xchg ax,ax. Some disassemblers recognize this and will decode the xchg ax,ax instruction as nop. Similarly, IBM assemblers for System/360 and System/370 use the extended mnemonics NOP and NOPR for BC and BCR with zero masks. For the SPARC architecture, these are known as synthetic instructions

Some assemblers also support simple built-in macro-instructions that generate two or more machine instructions. For instance, with some Z80 assemblers the instruction ld hl,bc is recognized to generate ld l,c followed by ld h,b. These are sometimes known as pseudo-opcodes.

Tag use

Use the tag for assembly language programming questions, on any processor. You should also use a tag for your processor or instruction set architecture (, , , , , etc). Consider a tag for your assembler as well (, , , et cetera).

If your question is about inline assembly in C or other programming languages, see . For questions about .NET assemblies, use instead and for .NET's Common Intermediate Language, use . For Java ASM, use the tag .

Resources

Beginner's resources

Assembly language tutorials, guides, and reference material

43242 questions
10
votes
3 answers

SIGILL in Android NDK code

I have an NDK app out on market and got a native crash report about a SIGILL signal. (I use google breakpad to generate native crash reports.) Here are the details: My app is compiled for armeabi-v7a, with NEON support. It crashed on a NVIDIA Tegra…
tmandry
  • 1,295
  • 16
  • 33
10
votes
2 answers

Clang generates strange output when dividing two integers

I have written the following very simple code which I am experimenting with in godbolt's compiler explorer: #include uint64_t func(uint64_t num, uint64_t den) { return num / den; } GCC produces the following output, which I would…
Gary Allen
  • 1,218
  • 1
  • 13
  • 28
10
votes
4 answers

Measure CPU speed by counting assembly instructions

Edit: My original example had a silly mistake. After fixing it I still get weird results, though. In my naive attempt to measure my CPU speed the "brute-force" way, I made the program below: #include #include #include…
user541686
  • 205,094
  • 128
  • 528
  • 886
10
votes
2 answers

How CPUs implement Instructions like MUL/MULT?

In different assembly languages MUL (x86)/MULT (mips) refer to multiplication. It is a black box for the programmer. I am interested in how actually a CPU accomplishes a multiplication regardless of the architecture. Lets say I have two 16-bit…
George
  • 15,241
  • 22
  • 66
  • 83
10
votes
1 answer

Why is different go-assembler code generated in this case?

Noticed strange things when generating assembly code func foo(v uint64) (b [8]byte) { b[0] = byte(v) b[1] = byte(v >> 8) b[2] = byte(v >> 16) b[3] = byte(v >> 24) b[4] = byte(v >> 32) b[5] = byte(v >> 40) b[6] = byte(v >>…
Pavel Burak
  • 101
  • 1
  • 3
10
votes
0 answers

Why does does a NOP (as a 5th uop) speed up a 4 uop loop on Ice Lake?

All benchmarks are done on: Icelake: Intel(R) Core(TM) i7-1065G7 CPU @ 1.30GHz (ark) Edit: I was not able to reproduce this on broadwell and @PeterCordes was unable to reproduce it on skylake I was trying to benchmark different methods of doing…
Noah
  • 1,647
  • 1
  • 9
  • 18
10
votes
5 answers

How do I specify immediate floating point numbers with inline assembly?

When I try to compile this code: #include main(int argc, char *argv[]) { double y = 0; __asm__ ("fldl $150;" "fsqrt;" "fstl %0;" : : "g" (y) ); printf("%f\n", y); return 0; } I get this…
poundifdef
  • 18,726
  • 23
  • 95
  • 134
10
votes
1 answer

X86 Assembly Instruction Pointer Addressing

I normally don't spend much time reading assembly, so the following compiler output confused me a little. Say I compile this piece of C code on my Intel Core 2 Duo running OSX 10.6: while (var != 69) // var is a global variable { …
lhumongous
  • 1,064
  • 12
  • 27
10
votes
2 answers

Linux default behavior of executable .data section changed between 5.4 and 5.9?

Story Case 1 I accidentally wrote my Assembly code in the .data section. I compiled it and executed it. The program ran normally under Linux 5.4.0-53-generic even though I didn't specify a flag like execstack. Case 2: After that, I executed the…
Ammar Faizi
  • 1,393
  • 2
  • 11
  • 26
10
votes
4 answers

How do I compile DOS programs on Debian?

For my assembly language class, we're writing DOS programs using DPMI. Unfortunately, I don't have access to a 32-bit windows machine all the time. I do have a Debian virtual machine installed on just about every computer I do use. I've got both…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
10
votes
4 answers

Unresolved external symbol printf in Windows x64 Assembly Programming with NASM

I've been trying to learn assembly lately, and came across this post. The author used NASM and Microsoft linker to set up the assembly working environment. I followed the same steps and installed NASM. Then I started to compile the hello world…
jtxkopt
  • 916
  • 1
  • 8
  • 21
10
votes
3 answers

Can compilers generate self modifying code?

It is commonly said that a static variable initialization is wrapped in an if to prevent it from being initialized multiple times. For this, and other one-off conditions, it would be more efficient to have the code remove the conditional after the…
janekb04
  • 4,304
  • 2
  • 20
  • 51
10
votes
1 answer

Any reason to use BX R over MOV pc, R except thumb interwork pre ARMv7?

Linux defines an assembler macro to use BX on CPUs that support it, which makes me suspect there is some performance reason. This answer and the Cortex-A7 MPCore Technical Reference Manual also states that it helps with branch prediction. However my…
10
votes
2 answers

How Does BIOS initialize DRAM?

I've been searching all over for an explanation of how exactly BIOS works now for quite some time. I have designed a bootloader and have jumped to 32-bit mode with it while successfully initializing the IDT as well as the GDT, but in doing so, I…
GodDamn
  • 161
  • 1
  • 8
10
votes
3 answers

How can I use Bochs to run Assembly code?

I want to use Bochs as an 8086 emulator. Is there an easy way to do this? What I want is something like emu8086 (http://www.emu8086.com/).
assemblylearner
  • 101
  • 1
  • 3
1 2 3
99
100