Docsity

Assembly Languages and Euclid's Algorithm, Slides of Assembly Language Programming

Partial preview of the text

Download Assembly Languages and Euclid's Algorithm and more Slides Assembly Language Programming in PDF only on Docsity!

LAST TIME

Languages Syntax: what’s in the language Semantics: what the language means Model: what the language manipulates Specification asks for something Modeling asks what something will do Concurrency Nondeterminism

ASSEMBLY LANGUAGE MODEL

add r1,r

sub r2,r

cmp r3,r

bne I

sub r4,

I1: jmp I

ALU

PC

Registers

Memory

ASSEMBLY LANGUAGE INSTRUCTIONS

Built from two pieces

Add R1, R3, 3

Opcode

What to do with

the data

(ALU operation)

Operands

Where to get

data and put

the results

OPERANDS

Each operand taken from a particular addressing mode: Examples: Register add r1, r2, r Immediate add r1, r2, 10 Indirect mov r1, (r2) Offset mov r1, 10(r3) PC Relative beq 100

Reflect processor data pathways

TYPES OF ASSEMBLY LANGUAGES

Assembly language closely tied to processor architecture At least four main types:

CISC: Complex Instruction-Set Computer RISC: Reduced Instruction-Set Computer DSP: Digital Signal Processor VLIW: Very Long Instruction Word

RISC ASSEMBLY LANGUAGE

Response to growing use of compilers Easier-to-target, uniform instruction sets “Make the most common operations as fast as possible”

Load-store architecture:  Arithmetic only performed on registers  Memory load/store instructions for memory-register transfers

Designed to be pipelined

DSP ASSEMBLY LANGUAGE

Digital signal processors designed specifically for signal processing algorithms Lots of regular arithmetic on vectors Often written by hand

Irregular architectures to save power, area

Substantial instruction-level parallelism

TYPES OF ASSEMBLY LANGUAGES

CISC RISC DSP VLIW

Opcodes Many,

Complex

Few,

Simple

Few,

Complex

Few,

Simple

Registers Few,

Special

Many,

General

Few,

Special

Many,

General

Addressing

modes

Many Few Special Few

Instruction-

level

Parallelism

None None Restricted Plenty

GRATUITOUS PICTURE

Woolworth building

Cass Gilbert, 1913

Application of the Gothic style to a 792’ skyscraper

Tallest building in the world when it was constructed

Downtown: near City Hall

I386 PROGRAMMER’S MODEL

eax

ebx

ecx

edx

esi

edi

ebp

esp

Mostly

general-

purpose

registers

Source index

Destination index

Base pointer

Stack pointer

cs

Code

ds Data

ss Stack

es Extra

fs Data

gs Data

Segment

Registers:

Added during

address

computation

eflags

eip

Status word

Instruction Pointer (PC)

EUCLID’S ALGORITHM ON THE I

.file "euclid.c"

.version "01.01"

gcc2_compiled.:

.text

.align 4

.globl gcd

.type gcd,@function

gcd:

pushl %ebp

movl %esp,%ebp

pushl %ebx

movl 8(%ebp),%eax

movl 12(%ebp),%ecx

jmp .L

.p2align 4,,

Boilerplate

Assembler directives start with “. ”

“This will be executable code”

“Start on a 16-byte boundary”

“gcd is a linker-visible label”

EUCLID’S ALGORITHM ON THE I

jmp .L .p2align 4,, .L4: movl %ecx,%eax movl %ebx,%ecx .L6: cltd idivl %ecx movl %edx,%ebx testl %edx,%edx jne .L movl %ecx,%eax movl - 4(%ebp),%ebx leave ret

“Jump to local label .L6”

“Skip as many as 7 bytes to

start on a 16-byte boundary”

“Sign-extend %eax to %edx:%eax”

“Compute %edx:%eax  %ecx:

quotient in %eax, remainder in %edx”

Register assignments:

%eax m

%ebx r

%ecx n

while ((r = m % n) != 0) { m = n; n = r; } return n;

EUCLID’S ALGORITHM ON THE I

jmp .L .p2align 4,, .L4: movl %ecx,%eax movl %ebx,%ecx .L6: cltd idivl %ecx movl %edx,%ebx testl %edx,%edx jne .L movl %ecx,%eax movl - 4(%ebp),%ebx leave ret

“m = n”

“Branch back to .L4 if the zero flag is

clear, I.e., the last arithmetic

operation did not produce zero”

“n = r”

“compute AND of %edx and %edx,

update the status word, discard the

result”

while ((r = m % n) != 0) { m = n; n = r; } return n;