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

06 loops 09 11 tủ tài liệu bách khoa

11 47 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 11
Dung lượng 143,86 KB

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

Nội dung

Trang 1

¢  

§  

¢  

while ( sum != 0 ) {

<loop body>

}

loopTop: cmpl $0, %eax

je loopDone <loop body code> jmp loopTop

loopDone:

Trang 2

int fact_do(int x)

{

int result = 1;

do {

result *= x;

x = x-1;

} while (x > 1);

return result;

}

int fact_goto(int x) {

int result = 1;

loop:

result *= x;

x = x-1;

if (x > 1) goto loop;

return result;

}

¢  

¢  

Trang 3

int

fact_goto(int x)

{

int result = 1;

loop:

result *= x;

x = x-1;

if (x > 1)

goto loop;

return result;

}

fact_goto:

.L11:

Trang 4

do

while (Test);

loop:

if (Test) goto loop

¢  

¢  

{

Statement1 Statement2 Statementn

}

Trang 5

int fact_while(int x)

{

int result = 1;

while (x > 1) {

result *= x;

x = x-1;

};

return result;

}

int fact_while_goto(int x) {

int result = 1;

goto middle;

loop:

result *= x;

x = x-1;

middle:

if (x > 1)

goto loop;

return result;

}

¢  

¢  

Trang 6

int fact_while(int x)

{

int result = 1;

while (x > 1) {

result *= x;

x ;

};

return result;

}

# x in %edx, result in %eax jmp .L34 # goto Middle L35: # Loop:

imull %edx, %eax # result *= x decl %edx # x

.L34: # Middle:

cmpl $1, %edx # x:1

jg .L35 # if >, goto # Loop

Trang 7

¢  

§   p = z0 · z1 2 · (z2 2) 2 · … · (…((zn –12) 2 )…)

zi = 1 i

zi = x i = 1

§  

/* Compute x raised to nonnegative power p */

int ipwr_for(int x, unsigned int p)

{

int result;

for (result = 1; p != 0; p = p>>1) {

if (p & 0x1)

result *= x;

x = x*x;

}

return result;

}

3 10 2

Trang 8

/* Compute x raised to nonnegative power p */ int ipwr_for(int x, unsigned int p)

{

int result;

for (result = 1; p != 0; p = p>>1) {

if (p & 0x1)

result *= x;

x = x*x;

}

return result;

}

Trang 9

for (Init; Test; Update)

int result;

for (result = 1; p != 0; p = p>>1)

{

if (p & 0x1)

result *= x;

x = x*x;

}

if (p & 0x1) result *= x;

x = x*x;

}

Trang 10

Init;

}

Init;

goto middle;

loop:

Body

middle:

if (Test) goto loop;

done:

Trang 11

Init;

goto middle;

loop:

Body

middle:

if (Test)

goto loop;

done:

for (result = 1; p != 0; p = p>>1) {

if (p & 0x1) result *= x;

x = x*x;

}

result = 1;

goto middle;

loop:

if (p & 0x1) result *= x;

x = x*x;

p = p >> 1;

middle:

if (p != 0)

goto loop;

done:

Ngày đăng: 09/11/2019, 06:40

🧩 Sản phẩm bạn có thể quan tâm