การดำเนินการทางคณิตศาสตร์และตรรกะ
เรียนรู้การดำเนินการทางคณิตศาสตร์พื้นฐาน (ADD, SUB, MUL, DIV) และการดำเนินการทางตรรกะ (AND, OR, XOR, NOT) กับข้อมูลในรีจิสเตอร์และหน่วยความจำ
การดำเนินการทางคณิตศาสตร์และตรรกะ เป็นบทเรียน Assembly Language & x86 Low-Level Systems Programming ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Assembly Language & x86 Low-Level Systems Programming และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Assembly Language & x86 Low-Level Systems Programming มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Intro to Assembly Ops
Welcome to the world of x86 Assembly's core operations! Today, we'll learn how CPUs perform basic math and logic.
These instructions are fundamental. They allow your programs to calculate values, make decisions, and manipulate data at the lowest level.
Addition: The ADD Instruction
The ADD instruction is used to add two numbers. It takes two operands: a destination and a source.
- Syntax:
ADD destination, source - The
sourcevalue is added to thedestinationvalue. - The result is stored back in the
destinationoperand. - Operands can be registers, memory locations, or immediate values (constants).
For example, ADD EAX, EBX adds the value in EBX to EAX, storing the sum in EAX.
ADD in Action
Let's see ADD in a simple program. We'll add 3 to 5, storing the result in the EAX register.
Try running this example:
section .text
global _start
_start:
mov eax, 5 ; Load 5 into EAX
add eax, 3 ; Add 3 to EAX (EAX becomes 8)
; Exit the program (Linux 32-bit syscall)
mov eax, 1 ; sys_exit syscall number
xor ebx, ebx ; Exit code 0
int 0x80 ; Call kernelSubtraction: The SUB Instruction
The SUB instruction is the opposite of ADD. It subtracts the source operand from the destination operand.
- Syntax:
SUB destination, source - The
sourcevalue is subtracted from thedestinationvalue. - The result is stored back in the
destinationoperand.
Like ADD, SUB can use registers, memory, or immediate values as operands. It's essential for basic arithmetic and comparisons.
SUB in Action
Here's SUB at work. We'll load 10 into EAX and then subtract 4 from it.
Run this code to see the subtraction:
section .text
global _start
_start:
mov eax, 10 ; Load 10 into EAX
sub eax, 4 ; Subtract 4 from EAX (EAX becomes 6)
; Exit the program (Linux 32-bit syscall)
mov eax, 1 ; sys_exit syscall number
xor ebx, ebx ; Exit code 0
int 0x80 ; Call kernelMultiplication: MUL & IMUL
Multiplication in x86 Assembly uses MUL for unsigned numbers and IMUL for signed numbers.
MULandIMULoften use theAX,EAX, orRAXregister implicitly as one operand.- For 32-bit multiplication, if you multiply a value in
EAXby another register (e.g.,EBX), the 64-bit result is stored across two registers: the lower 32 bits inEAXand the higher 32 bits inEDX.
Always remember to consider the size of your operands and the potential size of the result!
Multiplication Example
Let's multiply 5 by 6 using MUL. The result will be 30.
After execution, EAX will hold the lower part of the result, and EDX will hold the higher part (which will be 0 in this case).
section .text
global _start
_start:
mov eax, 5 ; Load 5 into EAX
mov ebx, 6 ; Load 6 into EBX
mul ebx ; Multiply EAX by EBX. Result in EDX:EAX
; EAX will be 30, EDX will be 0
; Exit the program (Linux 32-bit syscall)
mov eax, 1 ; sys_exit syscall number
xor ebx, ebx ; Exit code 0
int 0x80 ; Call kernelDivision: DIV & IDIV
Division is handled by DIV (unsigned) and IDIV (signed). These instructions are a bit more complex.
- For 32-bit division, the dividend (number to be divided) is expected to be in
EDX:EAX(EDXholds the high 32 bits,EAXholds the low 32 bits). - The divisor is specified as an operand (e.g., a register or memory location).
- After division, the quotient (result) is stored in
EAX, and the remainder is stored inEDX.
Always ensure EDX is correctly set (often cleared to 0) before an unsigned division if your dividend fits in EAX.
Division Example
Let's divide 10 by 3. We expect a quotient of 3 and a remainder of 1.
Observe how EDX is cleared before the division, as our dividend (10) fits entirely within EAX.
section .text
global _start
_start:
mov eax, 10 ; Load 10 into EAX (low part of dividend)
mov edx, 0 ; Clear EDX (high part of dividend for 32-bit)
mov ebx, 3 ; Load 3 into EBX (divisor)
div ebx ; Divide EDX:EAX by EBX.
; Quotient in EAX (3), Remainder in EDX (1)
; Exit the program (Linux 32-bit syscall)
mov eax, 1 ; sys_exit syscall number
xor ebx, ebx ; Exit code 0
int 0x80 ; Call kernelBitwise Logic: AND, OR, XOR, NOT
Beyond arithmetic, assembly excels at bitwise operations. These instructions manipulate individual bits within a number.
AND: Sets a bit if both corresponding bits are 1. Useful for masking bits.OR: Sets a bit if at least one corresponding bit is 1. Useful for setting specific bits.XOR: Sets a bit if corresponding bits are different. Useful for toggling bits or quickly clearing a register (e.g.,XOR EAX, EAX).NOT: Inverts all bits (flips 0s to 1s, and 1s to 0s). This is a unary operation (takes one operand).
These are crucial for low-level control, flags, and data manipulation.
Quick Check: Bitwise Ops
Consider the following assembly snippet:
mov al, 0b11001010
and al, 0b00001111What will be the final value stored in the AL register after these instructions execute?
Recap: Fundamental Operations
Great job! You've learned the essential arithmetic and bitwise logic operations in x86 Assembly.
- Arithmetic:
ADD,SUB,MUL/IMUL,DIV/IDIVperform calculations. - Logic:
AND,OR,XOR,NOTmanipulate individual bits for masking, setting, toggling, and inverting.
These instructions are the building blocks for complex programs, enabling your CPU to process data and make decisions effectively. Keep practicing!
คำถามที่พบบ่อย
บทเรียน “การดำเนินการทางคณิตศาสตร์และตรรกะ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การดำเนินการทางคณิตศาสตร์และตรรกะ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Assembly Language & x86 Low-Level Systems Programming ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Assembly Language & x86 Low-Level Systems Programming มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การดำเนินการทางคณิตศาสตร์และตรรกะ”
เรียนรู้การดำเนินการทางคณิตศาสตร์พื้นฐาน (ADD, SUB, MUL, DIV) และการดำเนินการทางตรรกะ (AND, OR, XOR, NOT) กับข้อมูลในรีจิสเตอร์และหน่วยความจำ คุณปฏิบัติ Assembly Language & x86 Low-Level Systems Programming ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Assembly Language & x86 Low-Level Systems Programming หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Assembly Language & x86 Low-Level Systems Programming บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การดำเนินการทางคณิตศาสตร์และตรรกะ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Assembly Language & x86 Low-Level Systems Programming นี้ได้ไหม
ได้ บทเรียน Assembly Language & x86 Low-Level Systems Programming ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- คำสั่งย้ายข้อมูล (MOV, PUSH, POP)
- การดำเนินการทางคณิตศาสตร์และตรรกะ
- การกระโดดตามเงื่อนไขและลูป
- คำสั่งดำเนินการระดับบิตและเลื่อนบิต