1. Trang chủ
  2. » Giáo Dục - Đào Tạo

037 basic conditional branching kho tài liệu training

25 55 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 25
Dung lượng 892,31 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Objectives  We will learn about the JZ/JNZ conditional jump instructions, and see example of their usage..  The condition is usually based on the values inside the flags register... J

Trang 1

Basic Conditional branching

Assembly language programming

Trang 2

Objectives

 We will learn about the JZ/JNZ conditional jump

instructions, and see example of their usage

 We will briefly mention some other basic conditional jumps

Trang 3

Jumping according to flags

 The JMP instruction changes the value of eip,

unconditionally

 We would like to be able to “jump” only on certain

conditions

There is a family of instructions of the form Jcc, where

the “cc” is replaced by some condition

 The jump is taken only if the condition is fulfilled

 The condition is usually based on the values inside the flags register

Trang 4

Jump Zero (JZ)

JZ label

 Takes the jump only if the zero flag is set

 Only if the result of the last calculation was zero

 Otherwise flow continues as usual

 Examples:

The JNZ instruction does the opposite

 Jumps only if the zero flag is cleared

mov ax,1 dec ax

jz my_label add ax,5 my_label:

add ax,2

; The jump is taken

; ax == 2

mov ax,1 inc ax

jz my_label add ax,5 my_label:

add ax,2

; The jump is not taken

; ax == 9

Trang 5

Jump Zero (Example)

 Simple loop:

mov eax,0 mov ecx,3 again:

add eax,ecx dec ecx

jz outside jmp again outside:

Trang 6

Jump Zero (Example)

 Simple loop:

mov eax,0 mov ecx,3 again:

add eax,ecx dec ecx

jz outside jmp again outside:

???????? ???????? ?

Trang 7

Jump Zero (Example)

 Simple loop:

mov eax,0 mov ecx,3 again:

add eax,ecx dec ecx

jz outside jmp again outside:

00000000 ???????? ?

Trang 8

Jump Zero (Example)

 Simple loop:

mov eax,0 mov ecx,3 again:

add eax,ecx dec ecx

jz outside jmp again outside:

00000000 00000003 ?

Trang 9

Jump Zero (Example)

 Simple loop:

mov eax,0 mov ecx,3 again:

add eax,ecx dec ecx

jz outside jmp again outside:

00000003 00000003 0

Trang 10

Jump Zero (Example)

 Simple loop:

mov eax,0 mov ecx,3 again:

add eax,ecx dec ecx

jz outside jmp again outside:

00000003 00000002 0

Trang 11

Jump Zero (Example)

 Simple loop:

mov eax,0 mov ecx,3 again:

add eax,ecx dec ecx

jz outside jmp again outside:

00000003 00000002 0

Trang 12

Jump Zero (Example)

 Simple loop:

mov eax,0 mov ecx,3 again:

add eax,ecx dec ecx

jz outside jmp again outside:

00000003 00000002 0

Trang 13

Jump Zero (Example)

 Simple loop:

mov eax,0 mov ecx,3 again:

add eax,ecx dec ecx

jz outside jmp again outside:

00000005 00000002 0

Trang 14

Jump Zero (Example)

 Simple loop:

mov eax,0 mov ecx,3 again:

add eax,ecx dec ecx

jz outside jmp again outside:

00000005 00000001 0

Trang 15

Jump Zero (Example)

 Simple loop:

mov eax,0 mov ecx,3 again:

add eax,ecx dec ecx

jz outside jmp again outside:

00000005 00000001 0

Trang 16

Jump Zero (Example)

 Simple loop:

mov eax,0 mov ecx,3 again:

add eax,ecx dec ecx

jz outside jmp again outside:

00000005 00000001 0

Trang 17

Jump Zero (Example)

 Simple loop:

mov eax,0 mov ecx,3 again:

add eax,ecx dec ecx

jz outside jmp again outside:

00000006 00000001 0

Trang 18

Jump Zero (Example)

 Simple loop:

mov eax,0 mov ecx,3 again:

add eax,ecx dec ecx

jz outside jmp again outside:

00000006 00000000 1

Trang 19

Jump Zero (Example)

 Simple loop:

mov eax,0 mov ecx,3 again:

add eax,ecx dec ecx

jz outside jmp again outside:

00000006 00000000 1

Trang 20

Jump Zero (Example)

 Simple loop:

 Calculates: 1 + 2 + 3 = 6

mov eax,0 mov ecx,3 again:

add eax,ecx dec ecx

jz outside jmp again outside:

00000006 00000000 1

Trang 21

Jump Zero (Example)

 Simple loop:

 Calculates: 1 + 2 + 3 = 6

 How could you change the program to make it calculate 1 + 2 + 3 + … + 100 ?

mov eax,0 mov ecx,3 again:

add eax,ecx dec ecx

jz outside jmp again outside:

00000006 00000000 1

Trang 22

Using JNZ

 We could use JNZ instead of JZ, to get simpler code:

 Same behavior, simpler code

mov eax,0 mov ecx,3 again:

add eax,ecx dec ecx

jz outside jmp again outside:

mov eax,0 mov ecx,3 again:

add eax,ecx dec ecx jnz again

Trang 23

Basic conditional jumps

 Some other basic conditional jumps:

 We will get to using those later

Conditional jump Description

Trang 24

Summary

 The conditional jump instruction Jcc allows us to take

branch decisions based on the flags register

 We created a loop that sums 1+2+3

 The conditional jump instructions are an indirect way

of reading the flags register

Trang 25

Exercises

 Code reading

 Code writing

 Have fun :)

Ngày đăng: 17/11/2019, 08:18

TỪ KHÓA LIÊN QUAN