






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







“A programming language is low level
h
it
i^
tt
ti
t^
th
when its programs require attention to theirrelevant.”
“You can do everything in assembler, butno one wants to program in assembler
p
g
anymore.”
purpose registersp
p
g
mov
instruction moves data between
d
i t
b t
t
memory and a register or between tworegisters.
mov
destination,
source
where destination and source can be
to load data into a register
constantsconstants.
mov
eax,
mov
al
mov
al,
mov
aardvark,
f b t of bytes.
memory.
the address of the data in memorythe address of the data in memory.
data typesdata types– long, int and short
(8, 4 & 2 bytes)
(4 & 8 bytes)
(1 byte)
g
yp
g
unsigned
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-
int
dog=4, cat=3,
bird=5;_asm
{
_
mov
eax,dog
sub
eax,cat
mov
bird,eax
}
i t
t^
t^
64 bit
l
registers to store a 64 bit value.
most significant bits in the EDX registerand the least significant bits in EAX.
bits 63, 62, … 33, 32
bits 31, 30 … 2, 1, 0
three forms.
imul
mem
or
reg
imul
reg
mem
reg
imul
reg
mem
or
reg
registers is divided by the 32 bit value in aregisters is divided by the 32 bit value in amemory location or another register.
g
y
used, you do not have to specify them.
idiv
memoryAddr
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
int dog=9,
cat=4,
bird=5,
cow;
_asm {
// cow = dog % cat + bird;
mov
eax,dog
; move dog to eax reg
mov
edx,
clear
idiv
cat
; edx = dog % cat
add
edx,bird
; add bird to dog % cat
mov
cow,edx
; save result in cow
left.left.^00000101
Left shift 2
Right shift 1
Arith R shift 1
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;
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;
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;
inc
and
dec
instructions are one of
th
f^
th t
l^
ti
the few that can run on memory locationswithout using the registers.
in a register for memory location
inc
eax
dec
memoryAddr
ti
th
i^
t^
ti
executing the instruction.
register.
subtraction but doesn’t store the resultssubtraction, but doesn t store the results.It just sets the status flags.
status register
cmp
instruction compares two values
d
t^
th
t t
fl
i t l
and sets the status flags appropriately.
cmp
register,
operand
location or a registerThe compare instruction subtracts the
operand from the register value, but doesnot save the results.
Pointer Register contains
cycle, the CPU fetches the instructionwhose address is in the program counter.
program counter register.
instruction. Jumps are based on theinstruction. Jumps are based on thecompare.
–for–while–do while–switch–break–else portion of an if
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
cow=0,
dog=0,
cat=3;
g
cin
dog
cat;
_asm{
if (dog > cat)
cow = 1;cow
else
dog = cat;
that directly implement loops (i.e. for,
y
p
p
while, do)
jumps.
while (what == ever) {
again: mov
eax,what
cmp
eax,ever
j^
dl
// something }
jne
endloop
// something^ jmp
again
endloop:
cow=0,
dog=0,
cat=3;
_asm{
do {
// convert this to assembler
cow++;cow
dog = dog + cat;
} while (dog < 12);
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
instruction is a one byte
i^
t^
ti
th t d
thi
instruction that does
nothing
does not change any
registers or status bits.
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.