Skip to content
Search

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.
asm:cpuid
Checks CPU information. See CPUID.
.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.
cmp, jmp
Programing/Assembley 16bit - Assembly 03. Conditions, control statements
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.

xor eax, eax

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

__asm int 3;

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

Favorite site

Online tools

Tutorials

Guide

Example

Reverse engineering

Debugging

System call

Intel

References


  1. Conversion_c_to_assembler.pdf ↩

  2. Maybe-SP-Assembly_Language_Beginning.pdf ↩

  3. Host: http://www.cs.utah.edu/ ↩

  4. Linux_assembly_lecture_01.pdf ↩

  5. Linux_assembly_lecture_02.pdf ↩

  6. Linux_assembly_lecture_03.pdf ↩

  7. Linux_assembly_lecture_04.pdf ↩

  8. Based_assembly_language_for_beginners.pdf ↩

  9. Korea.gnu.org-manual-as.zip ↩

  10. Assembly_Programming_-_Optimization_Course_3.pdf ↩

  11. P3ace_-_asm_01.pdf ↩

  12. P3ace_-_asm_02.pdf ↩

  13. P3ace_-_asm_03.pdf ↩

  14. P3ace_-_asm_04.pdf ↩

  15. P3ace_-_asm_05.pdf ↩

  16. Win32_MASM_Programming_Intro_-_Visual_Cpp.pdf ↩

  17. Register_and_assembly_and_stack_-_Asecurity.pdf ↩

  18. Winasmtut.pdf ↩

  19. Netstat.tistory.com_-_assembly_status_register.pdf ↩

  20. Blog.daum.net_-sysnet924-assembly-_status_flag.pdf ↩

  21. C_programmer_assembly.pdf ↩

  22. Asm64-handout.pdf ↩

  23. X64_cheatsheet.pdf ↩

  24. Linkc.tistory.com_-_list_of_assembly_instructions.pdf ↩

author Zer0 sitemap