• Tidak ada hasil yang ditemukan

Introduction to 8086 Assembly

N/A
N/A
Protected

Academic year: 2024

Membagikan "Introduction to 8086 Assembly"

Copied!
48
0
0

Teks penuh

(1)

Introduction to 8086 Assembly

Lecture 5

Jump, Conditional Jump, Looping, Compare instructions

(2)

Labels and jumping (the jmp instruction)

mov eax, 1 add eax, eax jmp label1

xor eax, eax label1:

sub eax, 303

(3)

Labels and jumping (the jmp instruction)

mov eax, 1 add eax, eax jmp label1

xor eax, eax label1:

sub eax, 303

jmp label1 add eax,eax mov eax, 1

memory

1008 1006 1001

xor eax,eax

1010

sub eax,303

1012

(4)

Labels and jumping (the jmp instruction)

mov eax, 1 add eax, eax jmp label1

xor eax, eax label1:

sub eax, 303

jmp label1 add eax,eax mov eax, 1

memory

1008 1006 1001

xor eax,eax

1010

sub eax,303

1012

(5)

Labels and jumping (the jmp instruction)

mov eax, 1 add eax, eax jmp label1

xor eax, eax label1:

sub eax, 303

jmp label1 add eax,eax mov eax, 1

memory

1008 1006 1001

xor eax,eax

1010

sub eax,303

1012

address of sub eax,303

(6)

Labels and jumping (the jmp instruction)

mov eax, 1 add eax, eax jmp label1

xor eax, eax label1:

sub eax, 303

jmp 1012 add eax,eax mov eax, 1

memory

1008 1006 1001

xor eax,eax

1010

sub eax,303

1012

(7)

Labels and jumping (the jmp instruction)

mov eax, 1 add eax, eax jmp label1

xor eax, eax label1:

sub eax, 303

jmp +2 add eax,eax mov eax, 1

memory

1008 1006 1001

xor eax,eax

1010

sub eax,303

1012

(8)

Labels and jumping (the jmp instruction)

mov eax, 1 add eax, eax jmp label1

xor eax, eax label1:

sub eax, 303

jmp 1012 add eax,eax mov eax, 1

memory

1008 1006 1001

xor eax,eax

1010

sub eax,303

1012

(9)

Labels and jumping (the jmp instruction)

jmp 1012 add eax,eax mov eax, 1

memory

1008 1006 1001

xor eax,eax

1010

sub eax,303

1012 1006

EIP

CPU

Executing: mov eax, 1

(10)

Labels and jumping (the jmp instruction)

jmp 1012 add eax,eax mov eax, 1

memory

1008 1006 1001

xor eax,eax

1010

sub eax,303

1012 1008

EIP

CPU

Executing: add eax,eax

(11)

Labels and jumping (the jmp instruction)

jmp 1012 add eax,eax mov eax, 1

memory

1008 1006 1001

xor eax,eax

1010

sub eax,303

1012 1010

EIP

CPU

Executing: jmp 1012

(12)

Labels and jumping (the jmp instruction)

jmp 1012 add eax,eax mov eax, 1

memory

1008 1006 1001

xor eax,eax

1010

sub eax,303

1012 1012

EIP

CPU

Executing: jmp 1012

(13)

Labels and jumping (the jmp instruction)

jmp 1012 add eax,eax mov eax, 1

memory

1008 1006 1001

xor eax,eax

1010

sub eax,303

1012 1017

EIP

CPU

Executing: sub eax,303

(14)

Infinite loop

mov eax, 0 loop1:

call print_int call print_nl inc eax

jmp loop1

infinite_loop.asm

(15)

Remember: the FLAGS Register

CF: carry flag OF: overflow flag SF: sign flag

ZF: zero flag PF: parity flag DF: direction flag IF: interrupt flag

Carry Flag (CF)

Parity Flag (PF)

Auxiliary Carry Flag (AF)

Zero Flag (ZF)

Sign Flag (SF)

Trap Flag (TF)

Interrupt Flag (IF)

Direction Flag (DF)

Overflow Flag (OF)

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

(16)

Conditional jumps

JZ Jump if ZF=1 JNZ Jump if ZF=0

JO Jump if OF=1

JNO Jump if OF=0

JS Jump if SF=1

JNS Jump if SF=0

JC Jump if CF=1

JNC Jump if CF=0

JP Jump if PF=1

JNP Jump if PF=0

(17)

Practice

JZ Jump if ZF=1 JNZ Jump if ZF=0

JO Jump if OF=1

JNO Jump if OF=0

JS Jump if SF=1

JNS Jump if SF=0

JC Jump if CF=1

JNC Jump if CF=0

JP Jump if PF=1

JNP Jump if PF=0

unsigned integer:

if (eax == ebx) esi = 0

(18)

Practice

JZ Jump if ZF=1 JNZ Jump if ZF=0

JO Jump if OF=1

JNO Jump if OF=0

JS Jump if SF=1

JNS Jump if SF=0

JC Jump if CF=1

JNC Jump if CF=0

JP Jump if PF=1

JNP Jump if PF=0

unsigned integer:

if (eax == ebx) esi = 0

sub eax, ebx jnz next

mov esi, 0 next:

(19)

Practice

JZ Jump if ZF=1 JNZ Jump if ZF=0

JO Jump if OF=1

JNO Jump if OF=0

JS Jump if SF=1

JNS Jump if SF=0

JC Jump if CF=1

JNC Jump if CF=0

JP Jump if PF=1

JNP Jump if PF=0

signed integer:

if (eax == ebx) esi = 0

sub eax, ebx jnz next

mov esi, 0 next:

(20)

Practice

JZ Jump if ZF=1 JNZ Jump if ZF=0

JO Jump if OF=1

JNO Jump if OF=0

JS Jump if SF=1

JNS Jump if SF=0

JC Jump if CF=1

JNC Jump if CF=0

JP Jump if PF=1

JNP Jump if PF=0

signed integer:

if (eax == - ebx) edi = 4

(21)

Practice

JZ Jump if ZF=1 JNZ Jump if ZF=0

JO Jump if OF=1

JNO Jump if OF=0

JS Jump if SF=1

JNS Jump if SF=0

JC Jump if CF=1

JNC Jump if CF=0

JP Jump if PF=1

JNP Jump if PF=0

signed integer:

if (eax == - ebx) edi = 4

add eax, ebx jnz next

mov edi, 4 next:

(22)

Practice

JZ Jump if ZF=1 JNZ Jump if ZF=0

JO Jump if OF=1

JNO Jump if OF=0

JS Jump if SF=1

JNS Jump if SF=0

JC Jump if CF=1

JNC Jump if CF=0

JP Jump if PF=1

JNP Jump if PF=0

unsigned integer:

if (eax >= ebx) esp -= 4

(23)

Practice

JZ Jump if ZF=1 JNZ Jump if ZF=0

JO Jump if OF=1

JNO Jump if OF=0

JS Jump if SF=1

JNS Jump if SF=0

JC Jump if CF=1

JNC Jump if CF=0

JP Jump if PF=1

JNP Jump if PF=0

unsigned integer:

if (eax >= ebx) esp -= 4 sub eax, ebx jc next

sub esp, 4 next:

(24)

Practice

signed integer:

if (eax < ebx)

ebp += 8 x < y => SF = 1 x >= y => SF = 0

x - y

(25)

Practice

signed integer:

if (eax < ebx) ebp += 8

OF=0 x < y => SF = 1

x >= y => SF = 0 x - y

(26)

Practice

signed integer:

if (eax < ebx) ebp += 8

OF=0 x < y => SF = 1

x >= y => SF = 0

OF=1

x < 0 < y => SF = 0 x > 0 > y => SF = 1 x - y

(27)

Practice

signed integer:

if (eax < ebx) ebp += 8 sub eax, ebx

jo ovflow jns endl if_cond:

add ebp, 8 jmp endl ovflow:

jns if_cond endl:

OF=0 x < y => SF = 1 x >= y => SF = 0

OF=1

x < 0 < y => SF = 0 x > 0 > y => SF = 1

x - y

(28)

Practice

signed integer:

if (eax < ebx) ebp += 8 else

ebp -= 8

OF=0 x < y => SF = 1 x >= y => SF = 0

OF=1

x < 0 < y => SF = 0 x > 0 > y => SF = 1

x - y

(29)

Practice:

(30)

Other conditional jump commands

sub x, y

unsigned signed

JE label jump if x == y (same as JZ) JE label jump if x == y (same as JZ) JNE label jump if x != y (same as JNZ) JNE label jump if x != y (same as JNZ)

JA label

JNBE label jump if x > y JG label

JNLE label jump if x > y

JB label

JNAE label jump if x < y JL label

JNGE label jump if x < y

JAE label

JNB label jump if x >= y JGE label

JNL label jump if x >= y

JBE label

JNA label jump if x <= y JLE label

JNG label jump if x <= y

(31)

Practice:

(32)

Practice:

rem.asm rem2.asm

(33)

Practice:

rem.asm

Practice: Also print quotient

(34)

Practice:

rem.asm

div.asm

(35)

Practice:

(36)

The LOOP instruction

(37)

The loop commands

loop lbl ecx--; if (ecx!=0) goto lbl

loopz lbl ecx--; if (ecx!=0 && ZF=1) goto lbl

loopnz lbl ecx--; if (ecx!=0 && ZF=0) goto lbl

(38)

The loop commands

loop lbl ecx--; if (ecx!=0) goto lbl

loopz lbl ecx--; if (ecx!=0 && ZF=1) goto lbl

loopnz lbl ecx--; if (ecx!=0 && ZF=0) goto lbl

loope ≡ loopz

loopne ≡ loopnz

(39)

Example: Count up to N

(40)

Example: Count up to N

(41)

Example: Count up to N

(42)

using sub before jump; what's wrong?

(43)

the cmp instruction

(44)

The cmp instruction

sub eax, ebx cmp eax, ebx

cmp x, y

● subtracts y from x (like sub x,y)

● does not store the result (x is not changed)

● flags are set (as though a subtraction has taken place)

(45)

The cmp instruction

cmp x, y

unsigned signed

JE label jump if x == y JE label jump if x == y JNE label jump if x != y JNE label jump if x != y

JA label

JNBE label jump if x > y JG label

JNLE label jump if x > y

JB label

JNAE label jump if x < y JL label

JNGE label jump if x < y

JAE label

JNB label jump if x >= y JGE label

JNL label jump if x >= y

JBE label

JNA label jump if x <= y JLE label

JNG label jump if x <= y

(46)

Practice

(signed)

if (eax > ebx) edi=1

else

edi=2

(47)

Practice

(signed)

if (eax > ebx) edi=1

else

edi=2

cmp eax, ebx jle else_lbl mov edi, 1 jmp endif

else_lbl:

mov edi, 2

endif:

(48)

Practice

(signed)

if (eax > ebx) edi=1

else

edi=2

cmp eax, ebx jle else_lbl mov edi, 1 jmp endif

else_lbl:

mov edi, 2 endif:

executed at the same time?

Referensi

Dokumen terkait

Remember: Compiling and linking C files test.c fact.c test.o fact.o gcc -c test.c required libraries executable gcc -c fact.c easier: let gcc do the linking gcc test.o fact.o

Remember: Compiling and linking C files test.c fact.c test.o fact.o gcc -c test.c required libraries executable gcc -c fact.c easier: let gcc do the linking gcc test.o fact.o

Column-major ● What does row-major and column-major mean?. ○ Matlab vs Numpy

● Does not matter when 2's complement signed integers are used ● Not the case for multiplication and division... Overflow ● carry flag CF: unsigned ● overflow flag OF:

IEEE 754 standard floating point representation ● single precision 32 bits ● double precision 64 bits look at http://cs.boisestate.edu/~alark/cs354/lectures/ieee754.pdf... Example:

IEEE 754 standard floating point representation ● single precision 32 bits ● double precision 64 bits look at http://cs.boisestate.edu/~alark/cs354/lectures/ieee754.pdf... Example:

IEEE 754 - Representation ● Relative Precision after decimal point of significand: ○ Single Precision: 2-23 6 digits after decimal point ○ Double Precision: 2-52 16 digits after