1 / 20

โครงสร้างควบคุม

โครงสร้างควบคุม. ธนาวินท์ รักธรรมานนท์ fengtwr@ku.ac.th. หัวข้อในวันนี้. โครงสร้างควบคุม โครงสร้างการตัดสินใจแบบ if-then-else โครงสร้างการกระทำซ้ำแบบ repeat-until โครงสร้างการกระทำซ้ำแบบ while โครงสร้างการกระทำซ้ำแบบ for. condition. True. False. Statement1. Statement2.

Download Presentation

โครงสร้างควบคุม

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. โครงสร้างควบคุม ธนาวินท์ รักธรรมานนท์ fengtwr@ku.ac.th

  2. หัวข้อในวันนี้ • โครงสร้างควบคุม • โครงสร้างการตัดสินใจแบบ if-then-else • โครงสร้างการกระทำซ้ำแบบ repeat-until • โครงสร้างการกระทำซ้ำแบบ while • โครงสร้างการกระทำซ้ำแบบ for

  3. condition True False Statement1 Statement2 IF – THEN – ELSE IF condition THEN statement 1 ELSE statement 2 ;

  4. condition1 False True condition2 True False Statement1 Statement2 Statement3 Nested Condition IF cond1 THEN statement1 ELSE IFcond2 THEN Statement2 ELSE statement3

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

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

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

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

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

  10. กำหนดค่าเริ่มต้น เท็จ ค่าของตัวแปร อยู่ในขอบเขต จริง คำสั่ง คำสั่ง ปรับค่าตัวแปร โครงสร้าง 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:

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

  12. mov ax,0 mov cx,100 startloop: add ax,data[bx] add bx,2 cmp data[bx],0 looopnz startloop AX:=0; CX:=100; repeat AX:=AX+data[BX]; BX:=BX+2; CX:=CX-1; until (data[BX]=0) or (CX=0); โครงสร้าง for • ใช้คำสั่ง LOOP สะดวกกว่าในการสร้างวงรอบ แต่ไม่สามารถใช้ในการวนรอบที่ซับซ้อนได้ จึงนิยมใช้คำสั่ง JMP มากกว่า • โครงสร้างการทำงานของคำสั่ง LOOPZ และ LOOPNZ มีลักษณะปนกันระหว่างโครงสร้าง for และ repeat EXAMPLE

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

  14. Example 2Convert Celcius to Farenheit Celcius Farenheit 0.0 32.0 1.0 33.8 2.0 35.6 …… …… 99.0 210.2 100.0 212.0 Farenheit = Celcius * (9/5) + 32

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

  16. โจทย์ • อ่านตัวเลขฐานสิบจากผู้ใช้ • ใช้บริการดอสหมายเลข 0Ah ในการอ่านข้อความ • แปลงข้อความทั้ง 8 บิตให้เป็นตัวเลข • ตรวจสอบว่าเป็นจำนวนเฉพาะหรือไม่ • ใช้วิธีทดลองหาร จงเขียนโปรแกรมตรวจสอบเลขฐานสิบที่รับจากผู้ใช้ (ไม่เกิน 8 บิต) ว่าเป็นจำนวนเฉพาะหรือไม่ โดยแสดงข้อความ yes หรือ no

  17. maxlen 4 strlen 3 str ‘1’ ‘2’ ‘3’ 13 0 * 10 + 1 * 10 + 2 * 10 + 3 ตัวอย่าง - การคำนวณ • ตัวอย่างการแปลง 123 = 1*102 + 2*10 + 3 = (((0*10 + 1) *10 + 2)*10 + 3) .data maxlen db 4 strlen db ? str db 4 dup (?) .code mov ax,@data mov ds,ax mov dx,offset maxlen mov ah,0Ah int 21h

  18. แปลงตัวเลข ทดสอบจำนวนเฉพาะ mov cl , al mov ch , 0 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” mov cl,strlen mov ch,0 mov bx,offset str mov al,0 extract: mov dh,10 mul dh ;AX=AL*10 ;discard AH mov dl,[bx] sub dl,’0’ add al,dl;AL=AL+digit inc bx loop extract

  19. โจทย์การบ้าน • รับเลขฐาน 10 จากผู้ใช้มีค่าไม่เกิน 65535 (16-bit) และให้แยกตัวประกอบ จากนั้นให้หาผลบวกตัวประกอบเหล่านั้น • ตัวอย่าง 100 => 2x2 + 5x5 = 29 3000 => 2x2x2 + 3 + 5x5x5 = 136 • วิธีการ 1. รับอินพุต จากข้อความแปลงเป็นเลขฐาน 10 => DX:AX 2. แยกตัวประกอบ 3. หาคำตอบและแสดงผลลัพธ์เป็นข้อความ

  20. Question ?

More Related