Docsity

Assembler Language and Arithmetic Operations: A Beginner's Guide, Slides of Computer Science

Toggle sidebar
bg1
MoreMore
Assembler LanguageAssembler Language
Computer Architecture and Organization
Assembler Programming
“A programming language is low level
h it i tt ti t th
w
h
en
it
s programs requ
i
re a
tt
en
ti
on
t
o
th
e
irrelevant.” - Alan J. Perlis
“You can do everything in assembler, but
no one wants to
p
ro
g
ram in assembler
pg
anymore.” - Yukihiro Matsumoto
Goals for Today
• Introduce assembler language constructs to:
–
Compute integer equations
– If statements
– loops
•
Write simple assembler programs
•
Write
simple
assembler
programs
.
Intel Registers
• The Intel Pentium has eight 32-bit general-
p
ur
p
ose re
g
isters
pp g
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Assembler Language and Arithmetic Operations: A Beginner's Guide and more Slides Computer Science in PDF only on Docsity!

MoreMore

Assembler LanguageAssembler LanguageComputer Architecture and Organization

Assembler Programming

“A programming language is low level

h

it

i^

tt

ti

t^

th

when its programs require attention to theirrelevant.”

  • Alan J. Perlis

“You can do everything in assembler, butno one wants to program in assembler

p

g

anymore.”

  • Yukihiro Matsumoto

Goals for Today

  • Introduce assembler language constructs to:
    • Compute integer equations– If statements– loops
      • Write simple assembler programs• Write simple assembler programs.

Intel Registers

  • The Intel Pentium has eight 32-bit general-

purpose registersp

p

g

mov

Instruction

  • The

mov

instruction moves data between

d

i t

b t

t

memory and a register or between tworegisters.

  • The format is

mov

destination,

source

where destination and source can be

  • where destination and source can be
    • register, memory

to load data into a register

  • memory, register to store data into memory– register, register to move data between regs

Constants

  • Assembler programs can also use small

constantsconstants.

  • If you want to move the number 47 to eax

mov

eax,

  • Constants can also be characters

mov

al

‘Z’

mov

al,

Z

  • You can also move constants to memory

mov

aardvark,

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.

Hardware Data Types

  • The hardware provides only a few primitive

data typesdata types– long, int and short

(8, 4 & 2 bytes)

  • float and double

(4 & 8 bytes)

  • char or byte

(1 byte)

  • Integer data types can be signed or

g

yp

g

unsigned

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

}

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.

EDX

EAX

EDX

EAX

bits 63, 62, … 33, 32

bits 31, 30 … 2, 1, 0

Multiplication

  • The imul signed multiply instruction has

three forms.

  • Multiply memory * EAX, save in EDX:EAX

imul

mem

or

reg

  • Multiply memory * register, save in register

imul

reg

mem

reg

imul

reg

mem

or

reg

Division

  • The 64 bit number in the EDX:EAX pair of

registers is divided by the 32 bit value in aregisters is divided by the 32 bit value in amemory location or another register.

  • The resulting quotient is stored in EAX• The resulting remainder is stored in EDX• Since the EDX:EAX registers are always

g

y

used, you do not have to specify them.

idiv

memoryAddr

Arithmetic Example 2

int dog=9,

cat=4,

bird=5,

cow;

_asm {

// cow = dog * cat / bird;

mov

eax,dog

; move dog to eax reg

imul

cat

; edx:eax = dog*cat

idiv

bird

; eax = dog*cat/bird

mov

cow,eax

; save result in cow

Arithmetic Example 3

int dog=9,

cat=4,

bird=5,

cow;

_asm {

// cow = dog % cat + bird;

mov

eax,dog

; move dog to eax reg

mov

edx,

clear

EDX

idiv

cat

; edx = dog % cat

add

edx,bird

; add bird to dog % cat

mov

cow,edx

; save result in cow

Shifting Bits

  • You can move the bits in a register right or

left.left.^00000101

Left shift 2

Right shift 1

Arith R shift 1

What is the result after

shr ax,

when the initial value of ax is

Try It

  • Complete this program to compute the

average of two numbers int

bull,

dog,

goat,

two=2;

cin

bull

dog;

_asm{

// set goat to the average of dog and bull

} cout

goat

endl;

Possible Solution

int

bull,

dog,

goat,

two=2;

cin

bull

dog;

_asm{

mov eax,

bull

; eax = bull

add eax,

dog

; eax = bull + dog

mov edx,

; clear for divide

idiv

two

;divide by 2

mov goat,

eax

; save result

} cout

goat

endl;

Another Possible Solution

int

bull,

dog,

goat;

cin

bull

dog;

_asm{

mov eax,

bull

; eax = bull

add eax,

dog

; eax = bull + dog

sar eax,

; divide by 2

mov goat,

eax

; save result

} cout

goat

endl;

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

dec

memoryAddr

Intel Status Register

  • The status register records the results of

ti

th

i^

t^

ti

executing the instruction.

  • Performing arithmetic sets the status

register.

  • The compare instruction does a

subtraction but doesn’t store the resultssubtraction, but doesn t store the results.It just sets the status flags.

  • All jump instructions are based on the

status register

Intel Status Register

Compare Instruction

  • The

cmp

instruction compares two values

d

t^

th

t t

fl

i t l

and sets the status flags appropriately.

cmp

register,

operand

  • where the operand can be a memory

location or a registerThe compare instruction subtracts the

  • The compare instruction subtracts the

operand from the register value, but doesnot save the results.

Program Counter

  • The Program Counter or Instruction

th

dd

Pointer Register contains

the address

of the next instruction to beexecuted.

  • At the beginning of the fetch/execute

cycle, the CPU fetches the instructionwhose address is in the program counter.

  • A jump instruction is just a load of the

program counter register.

Software Controls

  • Assembler only has a simple compare

instruction. Jumps are based on theinstruction. Jumps are based on thecompare.

  • Assembler does not have:

–for–while–do while–switch–break–else portion of an if

If statements

  • The high level language IF statement is

easily implemented by a conditional jump. if (cat ==dog)

cow = goat; else

cow = bull;

MOV

eax,cat

CMP

eax,dog

JNE

noteq

MOV

edx,goat

JMP

after

cow

bull;

noteq: MOV

edx,bull

after:

MOV

cow,edx

Try It

  • Complete this program in assembler int

cow=0,

dog=0,

cat=3;

g

cin

dog

cat;

_asm{

if (dog > cat)

cow = 1;cow

else

dog = cat;

Loops

  • There are usually no hardware instructions

that directly implement loops (i.e. for,

y

p

p

while, do)

  • Loops are implemented with conditional

jumps.

while (what == ever) {

again: mov

eax,what

cmp

eax,ever

j^

dl

(^

// something }

jne

endloop

// something^ jmp

again

endloop:

Try It

  • Complete this program in assembler int

cow=0,

dog=0,

cat=3;

_asm{

do {

// convert this to assembler

cow++;cow

dog = dog + cat;

} while (dog < 12);

Possible Solution

cow=0; dog=0; cat=3;asm{ _as

{ mov eax,

dog

; put dog in eax

again:

inc cow

; cow++

add

eax

cat

; add cat to dog

add

eax

cat

;^

add cat to dog

cmp eax,

jl

again

; repeat if not

No Operation

  • The

NOP

instruction is a one byte

i^

t^

ti

th t d

thi

instruction that does

nothing

  • Executing a

NOP

does not change any

registers or status bits.

  • When patching a machine language

program it is sometimes useful to be ableprogram, it is sometimes useful to be ableto add a few extra instructions that don’tchange the program.