Assembly
lastmod 2025-01-15created 2014-02-1960 edits
Assembly language (North Korean standard language: "assembler language") is a low-level computer programming language that corresponds one-to-one with machine language.
The machine language used differs depending on the computer architecture, so the assembly languages built to correspond to those machine languages differ as well. Each CPU supports different types and numbers of operations, and the size and number of registers and the representation of stored data types also differ. All general-purpose computers basically perform the same functions, but the process by which they carry out those functions can differ, and these differences are reflected in assembly language.
Moreover, multiple mnemonics and syntaxes can correspond to a single instruction set. In such cases, the one used in the manufacturer's documentation is usually the most commonly used.
Memory model
- TINY
- SMALL
- COMPACT
Category
Basics you need to know
Programmers writing assembly code and programmers writing C code look at the system from different perspectives when they write code. In general, the following are things you must understand to become an assembly programmer.
- Among the 8 registers, the program counter (%eip) holds the memory address of the next instruction to be executed (next instruction).
- The integer register file has 8 storage locations (storage) that hold 32-bit values. These storage locations are called registers (register) and can hold addresses and integer values. Some registers are used to manage program execution, and some are used as temporary storage.
- The 1-bit condition code registers hold information about the most recently executed arithmetic instruction. Combinations of multiple condition codes control the flow of the program.
- The floating-point register file has 8 storage locations for real numbers (float).
- Program memory holds the object code for program execution, procedure calls (procedure call), return values from functions, and memory blocks allocated by the user, and uses virtual addresses (virtual address).
- A single assembly instruction performs only a very simple task. For example, it adds two numbers, transfers data from memory to a register, or branches on a condition.
- C generally provides various data types, but assembly sees data simply as a contiguous array (array) of bytes. For example, arrays and structs in C are represented as contiguous collections of bytes, signed and unsigned are not distinguished, and neither are pointers and integers.
Syntax
-
MOV [목적지], [값] - Stores the value in the destination.
-
MOV AX,56h: The value of AX becomes 56h. -
MOV AX,BX: The value of AX is replaced with the value of BX.
- asm:int
- Software interrupt.
-
.global.globl - 8.41. .global symbol, .globl symbol
- Creates a symbol visible to ld.
- lea
- KLDP - What is the lea instruction in assembly..?
- Stands for Load Effective Address; it treats the following argument as an address value.
- Do not get this confused: lea does not add to or subtract from the value at the location the source register points to, but adds to or subtracts from the register value itself.
-
pushw,popw - pushing and popping of 16-bit values.
-
pushl,popl - pushing and popping of 32-bit values.
-
pushq,popq - The new stack instructions pushq and popq allow pushing and popping of 64-bit values.
-
rep - Repeats the following instruction until ECX becomes 0.
-
jmp,ja,jb,je - Jumps to the desired label. (ja: Jump if Above, jb: Jump if Below, je: Jump if Equal)
Sample
An instruction that sets a register to 0.
mov eax,0 and the like are rarely used because the instruction is larger.
How to set a break point at the code level in Visual Studio
In this case, if you put __asm int 3; in the code where you want the break point, execution will break at that point when it runs.
mov eax,0CCCCCCCCh
00401023 sub esp,40h ;지역변수를 위한 공간확보.
00401026 push ebx
00401027 push esi
00401028 push edi
00401029 lea edi,[ebp-40h]
0040102C mov ecx,10h
00401031 mov eax,0CCCCCCCCh
00401036 rep stos dword ptr [edi]
The source above is the assembly that allocates local variables right after the prelude, the function start code. The second line from the bottom moves 0xcccccccch into eax. When variables are created on the stack, they are first filled with this value. 0xCC is the assembly code int 3, the break instruction.
cmp vs test
First, both instructions seem to be used to take two operands and compare their values. Comparing the differences:
- CMP: Subtracts the second operand from the first operand. The result of this operation only affects ZF, the Zero Flag, does not affect the operands, and is discarded.
- TEST: ANDs the first operand with the second operand. The result of this operation only affects ZF, does not affect the operands, and is discarded.
In the end, one subtracts and the other ANDs, and because of this
- CMP can determine whether the two operands are exactly equal, and
- TEST can determine whether the two operands are both 0 or not.
With TEST, you cannot pin down the values except when the two operands are not 0 (even if the two values are different nonzero values, the result can be 0), so it is used in the form TEST eax, eax to check whether the target value is 0 or not.
See also
- CPU
- Netwide Assembler
- GNU Assembler
- Inline assembler
- Executable and Linkable Format (ELF)
- x86 calling conventions
- Disassembler
- Customasm - An assembler that can assemble source files using a user-defined instruction set it provides.
Favorite site
- MASM32 web site
- Wikipedia (en) entry on assembly language
- Let's analyze inline assembly!!!!
- The GNU Assembler
- Basics of Linux assembly analysis
- Linux Assembly HOWTO
- Linuxdoc Sgml/Assembly-HOWTO: Assembly HOWTO V0.3C
- Linux Assembly HOWTO : Linux Assembly HOWTO
- C to assembler 1
- SP Assembly Language Beginning 2
- Introduction to x86 Assembly Language – Part II
- x86 Assembly Guide
- [Recommended] Assembly, the primordial programming language: ways to express memory addresses
- Assembly, a language still used today
- Getting started with assembly language (1) - What is assembly language?? (converting C code to assembly code and analyzing it)
- PYRASIS.COM: Reverse engineering basics for beginners - Assembly instructions
- PYRASIS.COM: Reverse engineering basics for beginners - Interpreting machine code as assembly
- Using inline assembly in C
- asm simple function call
Online tools
- [Recommended] Compiler Explorer - Matches each line of code to its assembly and links the two
- Quick C++ Benchmark - Instantly compare C++ benchmarks online
- C++ Insights - See how C++ gets compiled.
Tutorials
- [Recommended] Linux assembly lectures 1-4 3
Guide
- Introduction to Win32 MASM programming (using Visual C++) 16
- Registers, assembly, and the stack 17
- Windows Assembly Programming Tutorial - Version 1.02 18
- ESP and EBP
- <Looking at disassembly code> 2. Function calling conventions (__cdecl, __stdcall)
- IA-32 assembly on Mac OS X
- Difference between the mov and lea instructions
- Assembly status register 19
Example
Reverse engineering
- Reverse engineering study part 2. Stack Frame
- Reverse engineering basics for beginners - Assembly instructions
- [Recommended] Hack My Mind :: Summary of assembly language instructions 24
Debugging
- Tracing x64 call stack arguments and checking windbg's Child-SP, RetAddr, Args to Child values
- Crash Dump Analysis Patterns (Part 14)
- WinDBG - Matching symbol files
- Assembly - Analyzing C code as assembly
- Assembly - Analyzing C if and switch statements as assembly
- Assembly - Swapping the CPU's state with previously saved state (context switch)
System call
Intel
References
-
Conversion_c_to_assembler.pdf ↩
-
Maybe-SP-Assembly_Language_Beginning.pdf ↩
-
Host: http://www.cs.utah.edu/ ↩
-
Linux_assembly_lecture_01.pdf ↩
-
Linux_assembly_lecture_02.pdf ↩
-
Linux_assembly_lecture_03.pdf ↩
-
Linux_assembly_lecture_04.pdf ↩
-
Based_assembly_language_for_beginners.pdf ↩
-
Korea.gnu.org-manual-as.zip ↩
-
Assembly_Programming_-_Optimization_Course_3.pdf ↩
-
P3ace_-_asm_01.pdf ↩
-
P3ace_-_asm_02.pdf ↩
-
P3ace_-_asm_03.pdf ↩
-
P3ace_-_asm_04.pdf ↩
-
P3ace_-_asm_05.pdf ↩
-
Win32_MASM_Programming_Intro_-_Visual_Cpp.pdf ↩
-
Register_and_assembly_and_stack_-_Asecurity.pdf ↩
-
Winasmtut.pdf ↩
-
Netstat.tistory.com_-_assembly_status_register.pdf ↩
-
Blog.daum.net_-sysnet924-assembly-_status_flag.pdf ↩
-
C_programmer_assembly.pdf ↩
-
Asm64-handout.pdf ↩
-
X64_cheatsheet.pdf ↩
-
Linkc.tistory.com_-_list_of_assembly_instructions.pdf ↩