Docsity

Assembly Language - Introduction to Computer Systems - Lecture Slides, Slides of Computer System Design and Architecture

Partial preview of the text

Download Assembly Language - Introduction to Computer Systems - Lecture Slides and more Slides Computer System Design and Architecture in PDF only on Docsity!

Assembly Language

Objectives

Be able to read simple x86-64 assembly

language programs

Assembly Language

One assembly instruction

 Straightforward translation to a group of machine

language bits that describe one instruction

What do these instructions do?

 Same kinds of things as high-level languages!

  • Arithmetic & logic
    • Core computation
  • Data transfer
    • Copying data between memory locations and/or

registers

  • Control transfer
    • Changing which instruction is next

Assembly Language (cont.)

But, assembly language has additional

features:

 Distinguishes instructions & data

 Labels = names for program control points

 Pseudo-instructions = special directives to the

assembler

 Macros = user-definable abbreviations for code &

constants

C Compiler’s Output

.file "main.c" .section .rodata .LC0: .string "Hello, %s, it's %d:%02d." .text .globl hello .type hello, @function hello: .LFB2: pushq %rbp .LCFI0: movq %rsp, %rbp .LCFI1: subq $16, %rsp .LCFI2: movq %rdi, -8(%rbp) movl %esi, -12(%rbp) movl %edx, -16(%rbp) movl -16(%rbp), %ecx movl -12(%rbp), %edx movq -8(%rbp), %rsi movl $.LC0, %edi movl $0, %eax

call printf leave ret .LFE2: .size hello, .-hello .section .rodata .LC1: .string "Alan" .text .globl main .type main, @function main: .LFB3: pushq %rbp .LCFI3: movq %rsp, %rbp .LCFI4: movl $55, %edx movl $2, %esi movl $.LC1, %edi call hello movl $0, %eax <..snip..>

A Breakdown of the Output

.file "main.c" .section .rodata .LC0: .string "Hello, %s, it's %d:%02d." .text .globl hello .type hello, @function hello: .LFB2: pushq %rbp .LCFI0: movq %rsp, %rbp .LCFI1: subq $16, %rsp .LCFI2: movq %rdi, -8(%rbp) movl %esi, -12(%rbp) movl %edx, -16(%rbp) movl -16(%rbp), %ecx movl -12(%rbp), %edx movq -8(%rbp), %rsi movl $.LC0, %edi movl $0, %eax

call printf leave ret .LFE2: .size hello, .-hello <..snip..>

Instructions,

Pseudo-Instructions,

& Label Definitions

Instructions: Operands

.file "main.c" .section .rodata .LC0: .string "Hello, %s, it's %d:%02d." .text .globl hello .type hello, @function hello: .LFB2: pushq %rbp .LCFI0: movq %rsp, %rbp .LCFI1: subq $16, %rsp .LCFI2: movq %rdi, -8(%rbp) movl %esi, -12(%rbp) movl %edx, -16(%rbp) movl -16(%rbp), %ecx movl -12(%rbp), %edx movq -8(%rbp), %rsi movl $.LC0, %edi movl $0, %eax

call printf leave ret .LFE2: .size hello, .-hello <..snip..>

Registers,

constants, &

labels

Instruction Set Architecture

Contract between programmer and the

hardware

 Defines visible state of the system

 Defines how state changes in response to

instructions

Assembly Programmer (compiler)

 ISA is model of how a program will execute

Hardware Designer

 ISA is formal definition of the correct way to

execute a program

Often Many Implementations of an ISA

ISA Implementations

x86-

Intel Core i

AMD Phenom II

VIA Nano

SPARC V.

UltraSPARC III

HyperSPARC

Why separate architecture and

implementation?

Compatibility

 VAX architecture: mainframe  single chip

 ARM: 20x performance range

  • high vs. low performance, power, price

Longevity

 20-25 years of ISA

 x86/x86-64 in 10th generation of implementations

(architecture families)

 Retain software investment

 Amortize development costs over multiple markets

Typical Machine State

Memory

General-

Purpose

Registers

Special-Purpose Registers

ALU,

FPU,

CPU

Integer Registers (IA32)

%eax

%ecx

%edx

%ebx

%esi

%edi

%esp

%ebp

%ax

%cx

%dx

%bx

%si

%di

%sp

%bp

%ah

%ch

%dh

%bh

%al

%cl

%dl

%bl

16-bit virtual registers (backwards compatibility)

general purpose

accumulate

counter

data

base

source index

destination index

stack pointer

base pointer

Origin (mostly obsolete)

movl Operand Combinations

Cannot do memory-memory transfer with a

single instruction

movl

Imm

Reg

Mem

Reg

Mem

Reg

Mem

Reg

Source Dest C Analog

movl $0x4,%eax temp = 0x4;

movl $-147,(%eax) *p = -147;

movl %eax,%edx temp2 = temp1;

movl %eax,(%edx) *p = temp;

movl (%eax),%edx temp = *p;

Src,Dest

Simple Memory Addressing

Modes

Normal (R) Mem[Reg[R]]

 Register R specifies memory address

movl (%ecx),%eax

Displacement D(R) Mem[Reg[R]+D]

 Register R specifies start of memory region

 Constant displacement D specifies offset

movl 8(%ebp),%edx