ASM-Lect03
Assembly Language Fundamentals
Authors and Edition
Assembly Language for x86 Processors, 7th Edition
Prepared by: Kip Irvine, Donia Gamal
Comments in Assembly Language
Single Line Comment
Syntax can be placed at the end or beginning of a line to clarify code, making it easier to understand for human readers:
Example:
inc eax ; single line commentThis indicates the increment operation while explaining its purpose.
Multiline Comment
Defined using the
COMMENTdirective, which helps in documenting larger sections of code:Example:
COMMENT ! This is a comment!Useful for providing extensive descriptions or intentions behind complex code structures.
Symbolic Constants
Equal-Sign Directive
Used to define a symbolic constant for better clarity and maintainability:
Syntax:
name = expressionExample:
COUNT = 500
This approach allows for redefinition of constants throughout the program, enhancing flexibility.
Calculating Sizes of Arrays and Strings
Utilizes the location counter
$to find sizes of dynamically sized data structures. This aids in memory management:For byte arrays:
ListSize = ($ - list)For word arrays:
ListSize = ($ - list) / 2Understanding the sizes is crucial for buffer management and avoiding overflow errors.
EQU and TEXTEQU Directives
EQU Directive
Defines a symbol as either an integer or text and cannot be redefined:
Examples:
PI EQU 3.1416pressKey EQU 0x0A– can represent hardware constants.
TEXTEQU Directive
Defines symbols that can be redefined and may contain more complex expressions:
Example:
continueMsg TEXTEQU "Press any key to continue..."This makes the code more readable and modifiable, especially in message management.
General-Purpose Registers
Registers Overview
16-bit Segment Registers: CS (Code Segment), SS (Stack Segment), DS (Data Segment), ES (Extra Segment)
32-bit General-Purpose Registers: EAX (Accumulator), EBX (Base), ECX (Counter), EDX (Data), FS, GS (Pointer registers), EBP (Base Pointer), ESP (Stack Pointer), ESI (Source Index), EDI (Destination Index)
Data representation in registers varies:
AL: 8 bits, AX: 16 bits, EAX: 32 bits – each increasing in size accommodates larger data operations effectively.
Data Directive Examples
Examples of how to define data types for better data management:
ByteVal0 BYTE ?– uninitialized byte.ByteVal1 BYTE 48– specifies a byte initialized to 48.String1 BYTE 'Hello'– defines a string as a sequence of bytes.Dup(5) DWVar DWORD 234553H– creates multiple DWORD entries initialized to a specific value, promoting efficient storage.
Signed and Unsigned Integers
Signed Integers
The highest bit represents the sign (1 for negative and 0 for positive).
Negative values in hexadecimal format can be identified when the highest digit is greater than 7 (e.g., 8A, C5).
Unsigned Integers
Represent only positive values, which simplifies certain arithmetic operations but limits expressiveness for negative ranges.
Data Transfer Instructions
Basic Characteristics
Facilitates the transfer of data between registers or from registers to memory, essential for data manipulation.
The MOV instruction serves as the primary operation for data transfer:
Syntax:
MOV dest, srcSource can be an immediate value, a register, or a memory reference.
MOV Instruction
Operand Types:
Immediate value (e.g.,
MOV EAX, 5),Register (e.g.,
MOV EAX, EBX),Memory reference (e.g.,
MOV EAX, [var])
It’s essential to note that direct transfers between memory locations are not permitted – data must first be moved to a register.
MOV Instruction Restrictions
Both operands must match in size to maintain data integrity.
Certain registers cannot receive data (e.g., CS, EIP), as these are crucial for system operation and require special handling.
Example MOV Scenarios
Example data initialization:
.data count BYTE 100.data bVal BYTE 20
Several successful and unsuccessful MOV operations are discussed to illustrate the importance of operand size and destination validity.
Same-size Operands Constraint
This fundamental constraint ensures that operands must match in size, which is crucial for interpretation by the processor.
Safety measures for signed and unsigned integers during transfers are critical:
Unsigned values are filled with 0’s to preserve number integrity.
Signed values utilize the sign bit to maintain accurate representation across different data sizes.
MOVZX and MOVSX Instructions
MOVZX: Transfers a small unsigned operand to a larger size, filling the additional bits with 0’s, which is vital in data expansion without sign alteration.
MOVSX: Transfers a signed operand and fills the new space with the sign bit, preserving the sign in conversion.
XCHG Instruction
This instruction allows for the exchange of values between two operands of the same size, which is often used to swap register contents or values.
At least one operand must be a register to ensure speed and efficiency in execution.
Addressing Modes Overview
Various methods for accessing instruction operands include:
Register Addressing: Utilizes register names directly to access their values, enhancing speed.
Immediate Addressing: Where a direct value is embedded in the instruction.
Direct Memory Addressing: Uses variable labels to point to memory locations.
Direct Offset Addressing: Adds a constant offset to produce an effective address, allowing flexible memory access.
Example of Direct Offset Addressing
This addressing mode prompts efficient data access with calculated offsets when managing arrays and strings, boosting performance.
Array Manipulation Example
Demonstrating how to effectively swap the order of elements in an array:
Move elements to EAX, execute the exchange, and subsequently copy values back, showcasing efficient memory use and logic implementation.
Addition and Subtraction Instructions
Basic Types
Key instructions include ADD, SUB, INC (increment), DEC (decrement), NEG (negate).
The common format for these operations is:
ADD/SUB destination, source, essential for performing arithmetic in assembly language.Note that memory-to-memory arithmetic is not permitted, underscoring the need for register usage.
Example Addition Scenario
A scenario illustrates how to add three unsigned bytes stored within an array, combining multiple operands to show cumulative addition.
Flags Impacted by ADD/SUB Instructions
Flags that can be set will include:
Carry Flag (CF), Overflow Flag (OF), Sign Flag (SF), Zero Flag (ZF), Auxiliary Carry Flag (AF), Parity Flag (PF).
Importance of Flags
These flags play a crucial role in detecting calculation errors and enabling conditional branching based on results, which are vital for control flow in assembly programs.
Zero Flag (ZF) and Sign Flag (SF)
Descriptions of how and when these flags are set provide insights into program logic and conditional execution:
The Zero Flag is set when the result of an operation is zero, indicating potential program end conditions.
The Sign Flag reflects the sign of the result, which is crucial in arithmetic operations.
Additional Flags
Auxiliary Flag (AF) and Parity Flag (PF) examples give insight into advanced condition checking and debugging operations, thus enhancing program robustness.
Conclusion
Continuous progress through assembly language instructions and their fundamental concepts fosters a deeper understanding of low-level programming, preparing students for more complex chapters and practical applications in computer science.
Reference
Textbook Chapter: 4.1 – 4.2 (partially)