Docsity

Assembler Programming: Understanding If Statements, Loops, and Indexing in Intel Assembler, Slides of Computer Science

Toggle sidebar
bg1
Still MoreStill More
Assembler ProgrammingAssembler Programming
Computer Architecture
dO i ti
and Organization
Goals for Today
• Introduce assembler language constructs to:
–
If statements
– Loops
– Index an array
•
Write simple assembler programs
•
Write
simple
assembler
programs
.
No Operation
• The NOP instruction is a one byte
i t ti th t d
thi
i
ns
t
ruc
ti
on
th
a
t
d
oes no
thi
ng.
• Executing a NOP does not change any
registers or status bits.
• When patching a machine language
program it is sometimes useful to be able
program
,
it
is
sometimes
useful
to
be
able
to add a few extra instructions that don’t
change the program.
Intel Status Register
• The status register records the results of
ti th i t ti
execu
ti
ng
th
e
i
ns
t
ruc
ti
on.
• Performing arithmetic sets the status
register.
• The compare instruction does a
subtraction but doesn
’
t store the results
subtraction
,
but
doesn t
store
the
results
.
It just sets the status flags.
• All jump instructions are based on the
status register
pf3
pf4
pf5

Partial preview of the text

Download Assembler Programming: Understanding If Statements, Loops, and Indexing in Intel Assembler and more Slides Computer Science in PDF only on Docsity!

Still MoreStill More

Assembler ProgrammingAssembler Programming

Computer Architecture

d O

i^

ti

and Organization

Goals for Today

  • Introduce assembler language constructs to:
    • If statements– Loops– Index an array
      • Write simple assembler programs• Write simple assembler programs.

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.

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

Flag Setting During Execution

int dog=-3, cat=3, bird=5, cow;

asm {

// cow = dog + cat

  • bird;

_

asm

// cow = dog + cat

bird;

mov

eax,dog

add

eax,cat

sub

eax

bird

Zero

Sign

Carry

Overflow

0

0

0

0

0

0

0

0

1

0

0

0

0

1

0

0

sub

eax,bird

mov

cow,eax

0

1

0

0

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.

Jump statements

• A

JMP

instruction (sometimes called

branch

)^

causes the flow of execution to go )^

g

to a specified location.

  • A

JMP

instruction loads the Program

Counter with the specified address.

  • An unconditional jump always jumps.• A conditional jump will do nothing if the

condition is not met.

  • Some architectures have a separate

compare instruction.

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

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{

mov eax, dog

; put dog in eax

again:

inc cow

; cow++

add eax, cat

; add cat to dog

g

cmp eax, 12

jl

again

; repeat if not

mov dog, eax

; save result to dog

Addresses

  • Assembler programs often have to

i^

l t

dd

manipulate addresses.

  • A pointer in C++ represents an address in

assembler.

  • You may need to use addresses to follow

links in a data structure or to get anlinks in a data structure or to get anelement from an array.

Intel Assembler Addresses

  • You can load a register with the address of

l^

ti

b

i^

th

L

d

a memory location by using the LoadEffective Address, lea, instruction.

lea

eax, dog ; eax = addr of dog

  • If the memory location is based on

indexing, the lea instruction will computeindexing, the lea instruction will computethe correct address lea

eax, dog[esi]

; effective addr

Indexing

  • To specify that the address of the data is

in a register in Intel assembler you put thein a register in Intel assembler, you put theregister in the operand field in [brackets]. //

char

cat[47],

goat;

goat

cat[10];

lea

ebx, cat

;^

ebx

= addr of cat

lea

ebx, cat

;^

ebx

addr of cat

add

ebx,

; add 10 to address

mov

al, [ebx]

; al = cat[10]

mov

goat, al

; save in goat

Assembler Pointers

int

*p;

// p is a pointer

i t

5

//

l^

i t

int

x = 5, y;

// regular integers

p = &x;

// p points to x

y = *p;

// y = x = 5

_asm {

mov

ebx p

b

i t

mov

ebx, p

; ebx = pointer p

mov

eax, [ebx]

; eax = x

mov

y, eax

; put value in y

Two Dimensional Arrays

  • Consider the two dimensional array

int aray[2][3];

  • This is allocated in memory as:• To set x = aray[i][j];

0,

0,

0,

1,

1,

1,

  • temp = size of int * (i*3 + j)• get the value at the address computed as

the address of the start of aray + temp

2D Array in Assembler

int aray[2][3], i, j, rat; _asm {

rat = aray[i][j];

mov ebx, i

; ebx = i

imul ebx, 3

; ebx = i*

add ebx, j

; ebx = i*3 + j

mov eax, aray[ebx] ; eax = aray[i][j]mov rat, eax

; store in rat