Assembly Language Fundamentals

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Which of the following is NOT a primary function of the Instruction Set Architecture (ISA)?

  • Specifying the instructions that the CPU can execute
  • Managing power consumption of the CPU (correct)
  • Defining the system's state, including registers and memory
  • Determining the effect of the instructions on the system's state

In the context of assembly language, what does the term 'opcode' refer to?

  • An assembly language command (correct)
  • A type of register
  • A file extension for assembly source code
  • A memory address

Which of the following actions is directly facilitated by the knowledge of assembly language?

  • Designing computer hardware.
  • Developing web applications.
  • Debugging higher-level language code. (correct)
  • Writing code that is portable across different operating systems.

What is a key difference between Intel and AT&T assembly syntax?

<p>Intel syntax omits the '%' character in front of register names, while AT&amp;T includes it. (A)</p> Signup and view all the answers

Which register is commonly used to store the return value of a function or as an accumulator for arithmetic operations?

<p>EAX (A)</p> Signup and view all the answers

What is the main function of the EFLAGS register in assembly language?

<p>To indicate different values through its different bits, reflecting the status of the processor and results of operations. (A)</p> Signup and view all the answers

Which memory segment in assembly language is used for declaring initialized data or constants?

<p>.data (D)</p> Signup and view all the answers

What is the purpose of the .bss section in assembly language?

<p>To store uninitialized data or variables. (C)</p> Signup and view all the answers

In assembly language, what does the instruction movl $0x4050, %eax do?

<p>It moves the immediate value 0x4050 into the EAX register. (D)</p> Signup and view all the answers

What does the assembly instruction push %ebp typically accomplish in the context of function calls?

<p>It saves the current base pointer, used to reference local variables and arguments. (B)</p> Signup and view all the answers

How does the leal instruction differ from the movl instruction when used with memory operands?

<p><code>leal</code> calculates an address and stores it in the destination, while <code>movl</code> loads the value from that address. (D)</p> Signup and view all the answers

In assembly language, what is the primary function of jump instructions?

<p>To control the flow of execution by changing the instruction sequence. (A)</p> Signup and view all the answers

What is the role of the stack in the context of procedure calls in assembly language?

<p>To pass arguments to the procedure, store return information, save registers, and allocate space for local variables. (C)</p> Signup and view all the answers

In the context of assembly language procedures, what does the term 'caller-saved registers' refer to?

<p>Registers that the calling function must preserve if their values are needed after the function call. (C)</p> Signup and view all the answers

Why is understanding ISA and assembly language valuable for programmers?

<p>It provides insights into how programs execute in memory, enabling more efficient program development. (B)</p> Signup and view all the answers

Which of the following is a characteristic of Complex Instruction Set Computing (CISC) architectures?

<p>Many different instructions with many different formats. (D)</p> Signup and view all the answers

Which of the following is NOT an advantage of using assembly language?

<p>Code portability (A)</p> Signup and view all the answers

Which component is NOT typically part of an assembly programmer's view of the system?

<p>Operating System (D)</p> Signup and view all the answers

In assembly language, what is the purpose of instruction suffixes like 'b', 'w', and 'l'?

<p>To define the size of the operand being manipulated. (C)</p> Signup and view all the answers

What does the instruction sar k,D do in assembly language?

<p>Shift D right by k bits arithmetically (C)</p> Signup and view all the answers

Which factor does NOT directly affect the time required for a program to execute?

<p>The programmer's level of experience (A)</p> Signup and view all the answers

What happens to upper bits of the destination in movsbw instruction?

<p>They are filled with copies of the most significant bit of the source value (B)</p> Signup and view all the answers

When translating a high-level language like C into assembly language, what is the role of the compiler?

<p>To translate the C code into assembly instructions. (B)</p> Signup and view all the answers

What is the term for memory locations nearest to Arithmetic Logic Unit for processing?

<p>Registers (B)</p> Signup and view all the answers

What is the purpose of EQU command?

<p>It sets a variable equal to some memory (A)</p> Signup and view all the answers

What does the HLT command do?

<p>It halts the program (A)</p> Signup and view all the answers

Which action is automatically done within PUSH instruction?

<p>Value of ESP is decremented (C)</p> Signup and view all the answers

What does the direction flag determine?

<p>Left or right direction for moving and comparing string data (D)</p> Signup and view all the answers

What is the meaning of instruction int 0x80?

<p>It calls a interrupt with address 0x80 (D)</p> Signup and view all the answers

Using the values in the image, what is the value of 9(%eax,%edx)?

<p>11 (C)</p> Signup and view all the answers

Using the values in the image, what is the value of 4(%eax)?

<p>AB (C)</p> Signup and view all the answers

What would the following code do?

movl $len, %edx
movl $msg, %ecx
movl $1, %ebx
movl $4, %eax
int $0x80

<p>Print 'hello world\n' (C)</p> Signup and view all the answers

Which statement best describes the relationship between assembly language and machine code?

<p>Assembly language is a symbolic representation of machine code, providing a more human-readable form. (C)</p> Signup and view all the answers

In the context of assembly language, what is the purpose of a label?

<p>To mark a specific location in the code for use as a jump target or reference point. (C)</p> Signup and view all the answers

Which x86 processor introduced both a new logical memory organization and 32-bit processing to the personal computing world?

<p>80386 (D)</p> Signup and view all the answers

Which statement best describes the role of registers in assembly language programming?

<p>Registers are small, fast storage locations within the CPU used for data manipulation and program execution. (D)</p> Signup and view all the answers

Which registers are Callee-Saved?

<p>EBP, EBX, ESI, EDI (B)</p> Signup and view all the answers

Flashcards

Assembly Language

An intermediate language between Machine Code and High Level Language.

Mnemonics

Human-readable abbreviations for machine instructions.

MASM

Microsoft Assembler. Outputs OMF files.

GAS

The GNU assembler. Uses ATT-style syntax.

Signup and view all the flashcards

NASM

Netwide Assembler. Free, small, outputs many object file types.

Signup and view all the flashcards

Registers

Fastest memory allocations, nearest to ALU for processing.

Signup and view all the flashcards

Memory

Primary memory area where data and program code are stored.

Signup and view all the flashcards

Opcodes

Assembly language commands that process data.

Signup and view all the flashcards

Instruction Pointer

Stores the address of the next instruction to execute.

Signup and view all the flashcards

EAX Register

Used to store the value returned from a function or as an accumulator.

Signup and view all the flashcards

EBX Register

Base pointer to the data section.

Signup and view all the flashcards

ECX Register

Counter register for loops and strings.

Signup and view all the flashcards

EDX Register

Data register that holds the size of the data.

Signup and view all the flashcards

EFLAGS Register

A single register that may indicate different values through its different bits.

Signup and view all the flashcards

Zero Flag (ZF)

Sets if the result of the instruction is zero; cleared otherwise.

Signup and view all the flashcards

Sign Flag (SF)

Sets equal to the most significant bit of the result.

Signup and view all the flashcards

Overflow Flag (OF)

Indicates overflow of a high-order bit.

Signup and view all the flashcards

Direction Flag (DF)

Determines left or right direction for string data.

Signup and view all the flashcards

Interrupt Flag (IF)

Determines whether external interrupts are ignored or processed.

Signup and view all the flashcards

Trap Flag (TF)

Allows setting processor operation in single-step mode.

Signup and view all the flashcards

Data Segment

Declares memory region where data elements are stored.

Signup and view all the flashcards

Code Segment

Area in memory that stores instruction codes.

Signup and view all the flashcards

Stack Segment

Contains data values passed to functions and procedures.

Signup and view all the flashcards

.ascii directive

Expects zero or more string literals separated by commas.

Signup and view all the flashcards

.asciz directive

Like .ascii, but each string is followed by a zero byte.

Signup and view all the flashcards

.lcomm directive

Reserve length bytes for a local common.

Signup and view all the flashcards

.global directive

Used to make the text symbol visible.

Signup and view all the flashcards

Operation Suffixes

Instructions with letters like 'b', 's', 'w', 'l', 'q', 't'.

Signup and view all the flashcards

Data Transfer Instructions

Copy data from one location to another.

Signup and view all the flashcards

CF: Carry Flag

The most recent operation generated a carry out of most significant bit.

Signup and view all the flashcards

Jump instruction

Switch execution to new position in program.

Signup and view all the flashcards

Procedure Call

Passing data and control from one part of a program to another.

Signup and view all the flashcards

Stack pointer register

Register that contains top of the stack

Signup and view all the flashcards

Reentrant Code

A language where the code can execute at any point and time.

Signup and view all the flashcards

Study Notes

  • This module introduces assembly language fundamentals.
  • This module explains registers in A32 assembly, different operations in assembly, and the need for a stack to pass parameters.

Introduction to Assembly

  • The module covers ISA, Intel processors/architectures history, the relationship between 'C' code and assembly, and x86 registers.

Improving Program Performance

  • High-Level Language (HLL) code, like 'C', translates to machine code for execution.
  • Compiling source code to assembly language then to machine language is required for execution.
  • Program execution time depends on program structure, the compiler, ISA, and hardware implementation.
  • Optimization techniques improve program performance.
  • Language and compiler choices rely on the application.
  • Hardware executes the application.
  • Understanding ISA and assembly language helps programmers develop more efficient programs.

Instruction Set Architectures (ISA)

  • ISA defines a system's state via registers, memory, and program counter.
  • ISA defines instructions the CPU can execute and the effect of these instructions on the system's state.
  • ISA defines how a computer is used by machine language programmers or compilers.
  • It covers memory model (access and referencing), instruction format/types/modes, and operand registers with data addressing.
  • Machine-level instruction types consist of arithmetic, logical, data transfer, and flow control; instruction modes include kernel and user.
  • Assembly programmers use the ISA to write programs using machine-level instructions.

Hardware Design: x86

  • Processors implementing the x86 ISA dominate server, desktop, and laptop markets.
  • x86 is backwards compatible up to the 8086 (introduced in 1978) with added features.
  • The complex instruction set computer (CISC) includes many instructions/formats, but only a subset is commonly used in Linux.

Intel x86 Evolution Milestones

  • 8087 coprocessor: Introduced interesting issues for Intel floating-point functions.
  • 80286 (1982): Added memory functionality and expanded the external data bus to 16 bits.
  • 80386: Introduced logical memory organization and 32-bit processing to maintain compatibility with the 286 and 8088.

Intel x86 processors - machine evolution

  • 486 (1989): 1.9M transistors
  • Pentium (1993): 3.1M transistors
  • Pentium/MMX (1997): 4.5M transistors
  • Pentium Pro (1995): 6.5M transistors
  • Pentium III (1999): 8.2M transistors
  • Pentium 4 (2001): 42M transistors
  • Core 2 Duo (2006): 291M transistors
  • Core i7 (2008): 731M transistors
  • Advancements included instructions for multimedia, parallel operations on data, efficient conditional operations, and more cores.

Other Processors

  • AMD has historically been a bit slower and less expensive than Intel.
  • Top circuit designers were recruited to build Opteron as a competitor to Pentium 4, and to develop x86-64 (an extension of x86 to 64 bits).

Introduction to Assembly Langauge

  • Assembly Language serves as an intermediate between machine code and high-level languages, following the "one language instruction for one machine instruction" norm.
  • Assembly code has better human understanding, writes more efficient/optimized programs using mnemonics and reserving memory locations.
  • Reading assembly code is essential for programmers, as it makes them understand compiler optimizations, underlying inefficiencies, function invocation, computer systems, and operating systems.
  • Assembly routines are useful where execution speed is critical.
  • Assembly knowledge allows debugging of higher-level language code and code generation for compiler developers.
  • Intel and AT&T are the two assembly instruction syntax and representation conventions.

Assembly Format Comparison

  • Intel code omits size designation suffixes and '%' before registers.
  • Intel describes memory locations differently with reverse listed multiple operands; avoid confusion.
  • Intel format can be found on IA32 documentation from Intel and Windows documentation from Microsoft.

Assembler Types

  • Main assembler types are MASM, GAS, and NASM.
  • MASM: Microsoft Assembler: Outputs OMF files convertible to win32 format.
  • GAS: GNU Assembler: Uses ATT-style syntax, designed as part of the GNU compiler collection (gcc) and uses Intel-style configuration.
  • NASM: Netwide Assembler: Is free and outputs various object file types with language more sensible than MASM.

Assembly Programmer's Views

  • Assembly code involves Registers (fastest memory for ALU processing), Memory (primary memory for data/code), and Opcodes (commands processing register data).

Registers

  • Registers are volatile small memory areas for memory manipulation.
  • They include eight "general purpose" registers and one "instruction pointer."
  • Six registers are commonly used and two are rarely used.
  • EAX stores function return values or as an accumulator
  • EBX acts as a base pointer to the data section.
  • ECX functions as a counter register for loops and strings.
  • EDX is an I/O Pointer.
  • ESI is a Source Indicator.
  • EDI is a Destination Indicator.
  • ESP is a Stack Pointer.
  • EBP is a Stack Frame base pointer, indicating where the stack starts for a specific function.
  • EIP is a Pointer to the next instruction to execute.

EAX Register

  • EAX is the main and most commonly used register, functioning as an accumulator where all calculations and system calls occur.

EBX Registers

  • EBX is a general-purpose register used as a base pointer for memory access or to store an extra pointer or calculation step.

EDX Register

  • EDX is the data register and holds the size of the data.

ECX Register

  • ECX is used as a count register for loops; counting instructions use this register, going downwards instead of upwards.

EFLAGS Register

  • A single register indicates values through its different bits.
  • ZERO FLAG (ZF): sets if the instruction result is zero; otherwise, cleared.
  • SIGN FLAG (SF): sets equal to the result's most significant bit.
  • OVERFLOW FLAG (OF): indicates an overflow of a high-order bit after a signed arithmetic operation.
  • DIRECTION FLAG (DF): determines left/right direction when moving/comparing string data; value determines direction.
  • INTERRUPT FLAG (IF): disables/enables external interrupts like keyboard entry depending on a value.
  • TRAP FLAG (TF): enables setting processor operations in single-step mode.

Memory

  • Memory is a byte addressable array in RAM that stores Code, user data, and some OS data, including a stack to support procedures.

Memory Segments

  • Assembly follows the segmented memory model, dividing system memory into independent segments referenced by segment registers.
  • The data segment (.data and .bss sections) declares memory regions for program data storage, is static, and zero-filled. The DS register stores the data segment's starting address.
  • The code segment (.text section) stores instruction codes and is a fixed area; the CS register stores its start address.
  • The stack segment contains data passed to functions and procedures and is stored in SSR.An extra segment (pointed to by ES) stores extra data.

Opcodes

  • Opcodes, or mnemonic codes, are assembly language commands with mainly three categories (data transfer, arithmetic, and logical/program control).
  • Examples of opcodes: Add, Sub, Imul, Xor, Or, and, Shl, Shr, Sar, Sal.
  • Example: movw $ff00,%ax (ax=1111.1111.0000.0000), shrw $3,%ax (ax=0001.1111.1110.0000)

Assembly Program Structure

  • An assembly program consists of the .data section (variable declarations), the .bss section (also variable declarations), and the .text section (program codes).
  • The global _start is the first statement in .text, defining the program's starting point, and _start is where program statements are written.
  • Assembly comments begin with semicolons (;), and can include printable characters.

Declaring Variables

  • Four variables (msg, msg2m len & len1) are defined.
  • Msg and msg2 have constant values, while len and len1 store sizes, for example msg: .asciz "Hello, world!\n".
  • The .ascii expects zero or more string literals separated by commas, assembling each string (without trailing zero byte) into consecutive addresses.
  • .asciz function acts like .ascii, but each string is trailed by a zero byte; the z in .asciz means zero.

Bss Section

  • Variables change during program runtime.
  • This may include variable user input, processing, and output.
  • The .lcomm reserves space in memory.
  • Example: The lcomm function reserves 1 byte for input1, input2, ans.

Text Section

  • .global indicates that the text symbol applies to ld
  • Both spellings (.globl and .global) can be used.
  • xdef is an acceptable synonym for .global.
  • To print “Hello, World! This is the first program” on screen, example program code is available.

Register Size in Different Machine Types

32 bit assembly

  • All registers can be accessed in 16-bit and 32-bit modes.
  • The register is identified by its two-letter abbreviation (I.e. AX) in 16-bit mode.
  • In 32-bit mode, this two-letter abbreviation is prefixed with an 'E' (extended).

32 to 64 bit assembly

  • 'EAX' is the accumulator register as a 32-bit value.
  • In the 64-bit version, the ‘E' is replaced with an ‘R'; the 64-bit version of ‘EAX' is 'RAX'.

Assembly Language Statements

  • Assembly language comprise of one statement per line.
  • Each statement is arranged in the format of [label] mnemonic [operands] [;comment].
  • NOP does nothing

Instructions

  • NOP does nothing and is used for delay, exchange function to one register.
  • The XCHG EAX, EAX performs an exchange function to one register.
  • PUSH pushes word, double-word, or Quad-word onto the stack, automatically decrementing the stack pointer esp by 4.
  • POP pops data from the stack and automatically sets the esp and also increments it.
  • The EQU command sets a variable equal to some memory.
  • The HLT is used to halt the program.

Operation Suffixes

  • GAS assembly instructions are generally suffixed with "b", "s", "w", "l", "q" or "t" to specify the operand's size.
    • b = byte (8 bit)
    • s = short (16-bit integer) or single (32-bit floating point)
    • w = word (16 bit)
    • l = long (32-bit or 64-bit floating point)
    • q = quad (64 bit)
    • t = ten bytes (80-bit floating point)

Moving Data: IA32

  • Data Transfer operations: movl Source, Dest
  • Operation Types: Immediate, Register, and Memory
  • Constant integer data: prefixed with ‘$’ and encoded with 1, 2, or 4 bytes (Example: $0x400, $-533).
  • Eight (8) integer registers, i.e. %eax, %edx. but %esp and %ebp are reserved.
  • Register memory provides 4 consecutive memory bytes at register address (Example: (%eax)).

Addressing Modes

  • Includes Inc, mov, add, and, mov instructions.
  • Increment example: - inc count ; Increment the memory variable count.
  • Transfer example: - mov total, $48; Transfer the value 48 to total variable.
  • Addition example: - add ah, bh; Add the contents of bh to ah register.
  • Bitwise operator example: - and mask1, 128; preform the and operation
  • Transfer example: - mov al, $10; Transfer the value 10 to a1 reigster.
  • Direct Memory Addressing uses a register, the eex has the value 0x100.
  • Indirect Memory Addressing requires the registers to contains the value.
  • Offset Addressing is where The the offsets of registers may calculate the memory reference for final data.

Memory Addressing

  • Memory can be addressed through direct memory (register), indirect memory, or offset addressing.
  • Use of the %eax in code.
  • Memory referencing and using address to point to data
  • Refers to value in register.
  • %eax refers to value in register, (%eax) uses value in register as an address to point to data as that address, and 9(%eax, %edx) calculates the final refer data register address ( %eax+%edx*9).

How to Get Assembly Language Code?

  • Assembly language can be written from the start or generated from 'C'.
  • Understanding the registers and commands is useful

Compiling into Assembly

  • '$ gcc –O1 -S code.c' command to produce a file code.s using ATT version
  • 'gcc -S -masm=intel code.c' command to reproduce a file code.s using Intel. for Mac OSX
  • Using clang++

Instrucions

  • Basic kinds of instruction include Arithmetic functions ( register ore memory), transferring data (memory and resister), Transfer control (conditional branches.)

Assembly Characteristics in relation to data Types

  • "Integer" data is found as a type such as 1, 2, 4 (IA32), or 8 (just in x86-64) bytes Data types incluse, values and addreses
  • Floating point data can by the lengths (4, 8, or 10 bytes), just like "Integer" data
  • Types of data is stored in continues block/array of bytes

Data Movement instructions

  • First Opcode (Assembly statements) is: "mov"
  • Mov? command, can source from and destination. can implement in varous versions of command
  • The following section describes the example usage of the "mov' command and suffixes
  • Example: mov1 $0x4050,%eax; Immediate Register (4 bytes)
  • The functions of "MOVS" can be described, this function copies date with signed extentions.
  • The functions of "MOVZ" can be described, this function copies date with zero extentions.

Comparing Byte instructions that are movs vs movz

  • Signed Expansion, the most significate bit of the source value is added.
  • Zero Expansion, the non zero bits of the upper are filed with zeros.

Stack Instructions

  • Used to put data in stack and get it from stack: Function discussed in next session.
  • PUSHL - pushes word, double-word, or Quad-word on the stack; then it decrements the stack pointer esp, by 4.
  • POPL - pops the data from the stack and sets/increments the esp,

Arithmetic and Logical Operations

  • The Load effective address instruction can have one form which reads from a location in code, it does note store the read value to the register (it stores the address location.
  • The register can be set in in specific location. can can be set with 7(%edx,%edx,4), to %eax set register %eax to 5x + 7.

Special Arithmetic Operations and instructions

  • There are some specific arithmetic inplementations taht include all 64 bits in production
  • The pairs of registers %edx and %eax are viewed as forming a single 64 bit unit

Instruction code and usage

  • The the function which is run, and its decription: "imull" is "Signed full multiply" command/function
  • "mull" is "Unsigned full multiply" command/function
  • "cltd" function is to "Convert to quad word" command/function
  • "idivil" is "Signed divide" comands/functions
  • "divl" is the "Unsigned divide" command/function.

Additional Command flags

  • The CPU uses sinle bits known as "condition code registers" Which discribe the attribures and of the operations.
  • CF: the flag indicates the most significate bit. and is used to detect Unsiged overflow
  • ZF - Flag indicates the most recent operation. and overflow if the value has a negative value.

Comparison Test instuction functions/code

Theses instructions set teh condition codes without any updaing of code:

  • "CMP" Command/function (and description)
  • "CMPB" Command/function (and description)
  • "CMPW" Command/function (and description)
  • "CMPL" Command/function (and description)
  • "TEST" Command/function (and description)
  • "TESTB" Command/function (and description)
  • "TESTL" Command/function (and description)

Jump Instructions

  • Under normal circumstances exection fallows in the listed order. a jump command is needed it swich postions.
  • The assembler and The linker, generate the proper encodings of the jump targets.
  • Can use pc relative and abulute address encoding

Machine Instuctions, Example

  • Int variable (int t = x+y;) is a simple calculation.
  • Operandes are: registers, menorys

Disassembling obect code

  • the "-dp command " can be utalised.
  • Can analyse serries of instructions, and analyse bitwise patterns
  • is an aproximation of the output
  • Run on P. code. or other code.

Procedures in Assembly

  • A procedure call involves passing both data and control from one place to another.
  • Machines like IA32, are designed so that they only provide simple transfer methods of the call
  • the program may will done any number of times if defined for its proedure.
  • Each procedure will have a set name.
  • If call, run the code, If not, then move on.

More On Procedures

  • Stack implementations have special features.
  • Stacks can handle doublewords or normal words.
  • Stacks can grow in reverse.

Key Pointer Values for Code

  • The stack pointer is the register containing the top of the stack and base pointer is the register at the bottom of the stack
  • key points are labelled in stacj frame structures image

Example code run

  • Procedures calls code
  • assembly is utalised in conjunction with code.
  • The functions or "Calley" is responsible for managing storing some of the data.
  • The "Caller" is where code can be stored to manage dates

Pointer Register

  • There are pointier registers related to stack memory. "stack pointer ESP" This stores the top. of the stack Stack base pointer (EPB) This indicated the base start of the stack

Stack Implentation

  • Push 1% can be made use push and take back
  • Pushing and pushing code.
  • The esp code will return the value.

Instructions

  • To use instruction control it requires the stack to be used, and the code can run though the memory via calls.
  • Can implement calls by Call/push commands Instructions to return the value requires a pop commnad

More Codes

  • Code must be "Reentant"
  • States are for prreocdires
  • call code to get return value.

Examplle of Code from chapter

  • It can perform A<B where variables can be defined.
  • Example of a call command for %ebp

Practice problems

  • read Textbook, and attempt practice questions
  • The problems require reading the textbook and understanding the youtube videos

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser