Docsity

Understanding Assembler Language: A Low-Level Programming Approach, Slides of Computer Science

Toggle sidebar
bg1
Assembler LanguageAssembler Language
Assembler
LanguageAssembler
Language
5
Computer Architecture and Organization
Goals for Today
• Introduce Intel assembler language.
• Learn how to write simple assembler
programs.
High and Low Levels
• C++, Java, Pascal, Cobol, Basic and the
lik ll hi h l l l Th
lik
e are a
ll
hi
g
h
l
eve
l
l
anguages.
Th
ey are
machine independent. The machine
dependent compilers translate the high
level language to machine language.
•
Assembler language is a low level
Assembler
language
is
a
low
level
language. Each different computer
architecture has its own assembler
language.
Microcode
• Assembler or machine language is the
ltll ht
l
owes
t
l
eve
l
access a programmer
h
as
t
o
the hardware.
• Internally, the machine language is
implemented with microcode.
•
Microcode is a series of instruction words
•
Microcode
is
a
series
of
instruction
words
that turn switches on and off to implement
each machine language instruction.
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Understanding Assembler Language: A Low-Level Programming Approach and more Slides Computer Science in PDF only on Docsity!

Assembler LanguageAssembler LanguageAssembler

Language

Assembler Language

Computer Architecture and Organization

Goals for Today

  • Introduce Intel assembler language.• Learn how to write simple assembler

programs.

High and Low Levels

  • C++, Java, Pascal, Cobol, Basic and the

lik

ll hi h l

l l

Th

like are all high level languages. They aremachine independent. The machinedependent compilers translate the highlevel language to machine language.

  • Assembler language is a low level

Assembler language is a low levellanguage. Each different computerarchitecture has its own assemblerlanguage.

Microcode

  • Assembler or machine language is the

l^

t l

l^

h

t

lowest level access a programmer has tothe hardware.

  • Internally, the machine language is

implemented with microcode.

  • Microcode is a series of instruction words• Microcode is a series of instruction words

that turn switches on and off to implementeach machine language instruction.

Compilers

  • A compiler translates a high level

l^

h

C

C

i t

hi

language, such as C or C++, into machinelanguage.

  • Each line of a high level language usually

produces many machine instructions.

  • Compilers can rearrange the instructions• Compilers can rearrange the instructions

to produce optimal code.

Compiled Results

// C++ or Java method int isflush( void ) {int i, ok;

// Portion of same method in assembler

mov

eax,

PTR hand+

add

eax, 117

mov

PTR ok[ebp], eax

ok = FLUSHBASE + hand[4].face - 5;for (i = 0; i < 4; i++)

if (hand[i].suit != hand[i+1].suit)

ok = 0;

return ok;}

[^

p],

mov

PTR i[ebp], 0

jmp

SHORT label

label3:

mov

eax,

PTR i[ebp]

add

eax, 1

mov

PTR i[ebp], eax

label4:

cmp

PTR i[ebp], 4

jge

SHORT label

mov

eax,

PTR i[ebp]

mov

ecx,

PTR i[ebp] d^

PTR h

d[^

*8 4]

mov

edx, PTR hand[eax*8+4]

cmp

edx,

PTR hand[ecx*8+12]

je^

SHORT label

mov

PTR ok[ebp], 0

label1:

jmp

SHORT label

label2:

mov

eax, PTR ok[ebp]

Architecture Specific

  • The same C++, Fortran, Pascal or Java

d

b

il d

source code can be compiled on anyarchitecture. When executed, it will givethe same results.

  • Each architecture has its own assembler

language. Assembler for one type oflanguage. Assembler for one type ofmachine will not run on another machine.

  • Assembler language is a simplified way of

writing machine language.

Writing Assembler

  • You need an assembler program that will

t^

l t

bl

i t

hi

translate your assembler source into machinelanguage.

  • Microsoft Visual Studio (any version) will

allow you to embed assembler into a C++program.program.

  • Details on using inline assembler are athttp://msdn2.microsoft.com/en-us/library/4ks26t93(VS.80).aspx

Registers Do Everything

  • All data moves through the registers.
    • Register to register instructions

Register to register instructions

  • Memory to register instructions– Memory to memory instructions

(rare)

  • Although arithmetic is done in the ALU, it

appears

to be done in the register.

  • Registers can hold addresses.• Instructions accessing data in memory can

use an index register to specify the address

Usual Assembler

dog = cat;

-^

move the value from memory location cat into aregisterregister

-^

move the value from the register to memorylocation dog

dog = cat + cow;

-^

move the value from memory location cat into a

y

register

-^

Add the value from memory location cow to theregister

-^

Move the value from register to memory dog

Intel Pentium has 8 User Registers

EAX

General use, division only in EAX

EBX

General use

ECX

General use

EDX

General use

EBP

Base pointer

ESI

Source pointer

EDI

Destination pointer

ESP

Stack pointer

Changing Names

  • The first IBM PC had an Intel 8088 with 16

bit

i t

bit registers.

  • The registers were named AX, BX, etc.• When Intel extended the processor to 32

bit registers, they called the longerregisters EAX EBX etcregisters EAX, EBX, etc.

  • AX is the lower 16 bits of EAX.• AH and AL are the high and low byte of

the 16 bit register, now bytes 3 & 4.

Intel Registers

  • The Intel Pentium has eight 32-bit general-

purpose registersp

p

g

General or Specialized

  • In some architectures all of the registers

have the same functionality. In othermachines the registers each have a specificmachines the registers each have a specificpurpose.

  • The Intel registers have special purposes,

although most can do several operations.– EAX Accumulator for arithmetic

EBX P i t

t^

d t

  • EBX Pointer to data– ECX Counter for loop operations– EDX I/O pointer– ESP Stack pointer

How many user registers does

the Intel Pentium have?A 4A.4B.8C.16D 32D.

Load and Store

• A

Load

instruction copies a value from

i t

i t

(R

d

memory into a register. (Reads memory)

  • A

Store

instruction copies a value from a

register into memory. (Writes memory)

  • The Intel assembler is confusing because

it

th

i^

mov

f

b th

it uses the same mnemonic

mov

for both

load and store.

Memory Model

  • Memory is a huge one dimensional array

f b t of bytes.

  • Both data and instructions are stored in

memory.

  • Registers can hold copies of the data or

the address of the data in memorythe address of the data in memory.

Arithmetic

  • All arithmetic and logical functions (AND,

OR XOR

t^

)^

t^

b

d

i^

th

OR, XOR, etc.) appear to be done in theregisters.

  • Each instruction has one operand in a

register and the other in memory oranother register.another register.

add

eax,

dog

  • The result is saved in the first register.

Arithmetic and Logical Instructions

mnemonic

operation

ADD

Add

ADD

Add

SUB

Subtract

MUL

Unsigned Multiply

IMUL

Signed Multiply

DIV

Unsigned Divide

DIV

Unsigned Divide

IDIV

Signed Divide

AND

Logical AND

OR

Logical OR

Arithmetic Example

int dog=3, cat=4, bird=5;_asm {

// bird = dog + cat;

mov

eax,dog

add

eax,cat

mov

bird,eax

Arithmetic Example 2

int dog=3,

cat=4,

bird=5,

cow;

_asm {

// cow = dog + cat - bird;

mov

eax,dog

add

eax,cat

sub

eax,bird

mov

cow,eax

asm1-

What value is in EAX at the end?

int

dog=4, cat=3,

bird=5;_asm

{

_

mov

eax,dog

sub

eax,cat

mov

bird,eax

}

Increment and Decrement

  • The

inc

and

dec

instructions are one of

th

f^

th t

l^

ti

the few that can run on memory locationswithout using the registers.

  • You can increment or decrement the value

in a register for memory location

inc

eax

inc

eax

dec

memoryAddr

Big Operands

  • Multiplication and Division use two

i t

t^

t^

64 bit

l

registers to store a 64 bit value.

  • A number is stored in EDX:EAX with the

most significant bits in the EDX registerand the least significant bits in EAX.

Shifts

  • The shift instructions can shift the values in

a register or memory location.

  • The SHR and SHL instructions shift the bits

right or left by the specified number of bits.

  • The SAR and SAL instructions shift the bit

right or left, but not the sign bit. The SARcopies the sign bit into the emptied bitscopies the sign bit into the emptied bits.

  • The shift count can be a constant or the

cl

reg sar

eax,

shl eax,

cl

Shift Example

int dog=3;_asm

mov

eax,dog

eax

sal

eax,

eax

sar

eax,

eax