IF THEN ELSE IF condition THEN statement 1

  • Slides: 20
Download presentation

IF – THEN – ELSE IF condition THEN statement 1 ELSE statement 2 ;

IF – THEN – ELSE IF condition THEN statement 1 ELSE statement 2 ; True Statement 1 condition False Statement 2 3

Nested Condition IF cond 1 THEN statement 1 ELSE IF cond 2 THEN Statement

Nested Condition IF cond 1 THEN statement 1 ELSE IF cond 2 THEN Statement 2 ELSE statement 3 True Statement 1 condition 1 True Statement 2 False condition 2 False Statement 3 4

While Loop While condition do statement ; condition False True Statement 5

While Loop While condition do statement ; condition False True Statement 5

Repeat-until Loop Repeat Statement; … Statement; Until condition ; Statement False condition True 6

Repeat-until Loop Repeat Statement; … Statement; Until condition ; Statement False condition True 6

����� if-then-else if false then jump to else_label then_actions jump to endif Else_label: else_actions

����� if-then-else if false then jump to else_label then_actions jump to endif Else_label: else_actions endif: EXAMPLE if AL<10 then DL: =AL+’ 0’ else DL: =AL+’A’-10; cmp jae mov add jmp abovenine: mov add endif: คำสง จรง เงอนไข คำสง al, 10 abovenine dl, al dl, ’ 0’ endif dl, al al, ’A’-10 7 เทจ

����� repeat-until คำสง startlabel: actions. . . actions if false then jump to startlabel

����� repeat-until คำสง startlabel: actions. . . actions if false then jump to startlabel EXAMPLE repeat DX: =DX+BL*BL; BL: =BL+1; CX: =CX+1; until (DX>100); เงอนไข จรง เทจ startlabel: mov mul add al, bl bl dx, ax inc bl inc cx cmp jbe dx, 100 startlabel 8

����� while เทจ เงอนไข จรง คำสง startlabel: if false then jump to endlabel actions.

����� while เทจ เงอนไข จรง คำสง startlabel: if false then jump to endlabel actions. . . actions jump to startlabel endlabel: startloop: EXAMPLE cmp dl, 13 jz endloop while (DL<>13) and cmp cx, 20 (CX<20) do jae endloop begin add al, dl adc ah, 0 AX: =AX+DL; inc bx BX: =BX+1; mov dl, data[bx] DL: =DATA[BX] inc cx CX: =CX+1; jmp startloop end; endloop: 9

����� for set the value of CX startloop: actions. . . actions LOOP startloop

����� for set the value of CX startloop: actions. . . actions LOOP startloop initialize index variable startloop: if index value is not in the range then jump to endloop action. . . action update index variable Jump to startloop endloop: กำหนดคาเรมตน เทจ คาของตวแปร อยในขอบเขต จรง คำสง ปรบคาตวแปร 10

����� for EXAMPLE CX: =0; for DL: =1 to 100 do begin if DL

����� for EXAMPLE CX: =0; for DL: =1 to 100 do begin if DL mod 7 = 0 then CX: =CX+1; end; mov cx, 0 mov dl, 1 startloop: cmp dl, 100 ja endloop mov mob div al, dl ah, 0 bl, 7 bl cmp jne ah, 0 endif inc cx endif: inc dl jmp startloop endloop: 11

����� for ����� LOOP ��������������������� JMP ������� l ����������� LOOPZ ��� EXAMPLE mov ax,

����� for ����� LOOP ��������������������� JMP ������� l ����������� LOOPZ ��� EXAMPLE mov ax, 0 AX: =0; LOOPNZ ��������� mov cx, 100 CX: =100; startloop: for ��� repeat add ax, data[bx] l AX: =AX+data[BX]; BX: =BX+2; CX: =CX-1; until (data[BX]=0) or (CX=0); add bx, 2 cmp data[bx], 0 looopnz startloop 12

Example 1 Calculate the “n!” (n-factorial( Start Read “n” n>0 False Write “fac” True

Example 1 Calculate the “n!” (n-factorial( Start Read “n” n>0 False Write “fac” True fac : = fac * n End n : = n -1 13

Example 2 Convert Celcius to Farenheit Celcius 0. 0 1. 0 2. 0 …

Example 2 Convert Celcius to Farenheit Celcius 0. 0 1. 0 2. 0 … … 99. 0 100. 0 Farenheit 32. 0 33. 8 35. 6 … … 210. 2 212. 0 Farenheit = Celcius * (9/5) + 32 14

Example 2 Convert Celcius to Farenheit Start Write heading cel <=100 False True Far

Example 2 Convert Celcius to Farenheit Start Write heading cel <=100 False True Far : = Cel * (9/ 5) + 32 Write output Cel : = Cel +1 End 15

���� - ���� l �������� 0 * 10 3 + 10*2 + 102*1 =

���� - ���� l �������� 0 * 10 3 + 10*2 + 102*1 = 123 10*(2 + 10* (1 + 10*0))) =. data (3 + maxlen str. code db db db 4 ? 4 dup (? ) mov ax, @data ds, ax mov int dx, offset maxlen ah, 0 Ah 21 h +1 * 10 +2 4 3 ’ 1‘ ’ 2‘ ’ 3‘ 13 * 10 +3 maxlen str 17

����� mov cl , al cl , strlen �� mov ch , 0 mov

����� mov cl , al cl , strlen �� mov ch , 0 mov ch mov bx mov al extract: mov dh mul dh , 0 , offset str , 0 , 10 ; AX=AL*10 ; discard AH mov dl , [bx[ sub dl , ’ 0’ add al , dl ; AL=AL+digit inc bx loop extract sub cx , 2 ; cx=al-2 mov dl , al mov bl , 2 primetest: mov al , dl mov ah , 0 div bl inc bl cmp ah , 0 loopnz primetest jz notprime ; print “yes” notprime: ; print “no” 18

Question ? 20

Question ? 20