{"id":395,"date":"2016-12-31T15:14:32","date_gmt":"2016-12-31T15:14:32","guid":{"rendered":"http:\/\/teachcomputerscience.com\/?p=395"},"modified":"2024-07-24T11:34:38","modified_gmt":"2024-07-24T11:34:38","slug":"lmc-iteration-structures","status":"publish","type":"post","link":"https:\/\/teachcomputerscience.com\/lmc-iteration-structures\/","title":{"rendered":"LMC Iteration Structures: While\/Endwhile Loop"},"content":{"rendered":"<div class=\"gb-container gb-container-1d692a9e upsell-block\"><div class=\"gb-inside-container\">\n<div class=\"gb-grid-wrapper gb-grid-wrapper-66bcf1bd\">\n<div class=\"gb-grid-column gb-grid-column-4f382063\"><div class=\"gb-container gb-container-4f382063\"><div class=\"gb-inside-container\">\n\n<h2 class=\"gb-headline gb-headline-24b9a624 gb-headline-text\">GCSE Basic Programming Constructs (14-16 years)<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>An editable PowerPoint lesson presentation<\/li><li>Editable revision handouts<\/li><li>A glossary which covers the key terminologies of the module<\/li><li>Topic mindmaps for visualising the key concepts<\/li><li>Printable flashcards to help students engage active recall and confidence-based repetition<\/li><li>A quiz with accompanying answer key to test knowledge and understanding of the module<\/li><\/ul>\n\n\n<div class=\"gb-button-wrapper gb-button-wrapper-aff1bb2c\">\n\n<a class=\"gb-button gb-button-7eae67ca gb-button-text\" href=\"https:\/\/teachcomputerscience.com\/gcse\/programming\/basic-programming-constructs\/\">View GCSE Basic Programming Constructs Resources<\/a>\n\n<\/div>\n<\/div><\/div><\/div>\n\n<div class=\"gb-grid-column gb-grid-column-d1c761df\"><div class=\"gb-container gb-container-d1c761df\"><div class=\"gb-inside-container\">\n\n<h2 class=\"gb-headline gb-headline-f012084b gb-headline-text\">A-Level Introduction to programming (16-18 years)<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>An editable PowerPoint lesson presentation<\/li><li>Editable revision handouts<\/li><li>A glossary which covers the key terminologies of the module<\/li><li>Topic mindmaps for visualising the key concepts<\/li><li>Printable flashcards to help students engage active recall and confidence-based repetition<\/li><li>A quiz with accompanying answer key to test knowledge and understanding of the module<\/li><\/ul>\n\n\n<div class=\"gb-button-wrapper gb-button-wrapper-7c9345e9\">\n\n<a class=\"gb-button gb-button-3891e385 gb-button-text\" href=\"https:\/\/teachcomputerscience.com\/a-level\/problem-solving-and-programming\/introduction-to-programming\/\">View A-Level Introduction to programming Resources<\/a>\n\n<\/div>\n<\/div><\/div><\/div>\n<\/div>\n<\/div><\/div>\n\n\n\n\n<p>The following program will show the use of branch instructions to create a WHILE\/ENDWHILE iteration (loop) structure in a program.<\/p>\n\n\n\n<p>The WHILE\/ENDWHILE iteration structure tests the condition at the beginning of a loop (in this program, the loop continues while value1 is not zero). This means that if this test result is false (value1 is zero) at the beginning of the loop then the instructions inside the loop are never carried out.<\/p>\n\n\n\n<p>Running the program: when prompted, INPUT a number, if the number is 0 then the program just ends. If any other number is entered then the program will OUTPUT the number 999 that number of times.<\/p>\n\n\n\n<figure class=\"wp-block-table rcp-table\"><table><tbody><tr><td>INP<br>STA value1<br><span class=\"style6\"><strong>while LDA value1<br>BRZ endwhile<br>SUB count<br>STA value1<\/strong><\/span><strong><br><\/strong><span class=\"style5\"><strong>&nbsp;&nbsp; LDA nineninenine<br>OUT<\/strong><\/span><strong><br><\/strong><span class=\"style6\"><strong>BRA while<br>endwhile HLT<\/strong><\/span><br>value1 DAT<br>count DAT 1<br>nineninenine DAT 999<\/td><td>0 INP<br>1 STA 10<br><span class=\"style6\"><strong>2 LDA 10<br>3 BRZ 9<br>4 SUB 11<br>5 STA 10<\/strong><\/span><strong><br><\/strong><span class=\"style7\"><strong>6 LDA 12<br>7 OUT<\/strong><\/span><strong><br><\/strong><span class=\"style6\"><strong>8 BRA 2<br>9 HLT<\/strong><\/span><br>10 DAT 0<br>11 DAT 1<br>12 DAT 999<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Instructions<\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>Copy the 13 line program above and paste it into the Program box.<\/li><li>Click on the &#8220;Assemble Program&#8221; button.<\/li><li><img loading=\"lazy\" decoding=\"async\" class=\"alignright size-full wp-image-396\" src=\"http:\/\/teachcomputerscience.com\/wp-content\/uploads\/2016\/12\/img25.jpg\" alt=\"Image\" width=\"377\" height=\"88\" title=\"\" srcset=\"https:\/\/teachcomputerscience.com\/wp-content\/uploads\/2016\/12\/img25.jpg 377w, https:\/\/teachcomputerscience.com\/wp-content\/uploads\/2016\/12\/img25-300x70.jpg 300w\" sizes=\"auto, (max-width: 377px) 100vw, 377px\" \/>After the program is assembled you should see RAM addresses 0 to 9 contain the machine code instructions shown in the image.<\/li><li>Click on the RUN button.<\/li><li>If you have difficulty following what is happening, read the explanation below and use STEP instead of RUN so you can follow each step.<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Explanation:<\/h4>\n\n\n\n<p><strong>value1, count<\/strong> and <strong>nineninenine<\/strong> are used to label <strong>DAT<\/strong> instructions. DAT identifies the 11th, 12th and 13th instructions as data. The labels therefore refer to RAM addresses 10, 11 and 12 (0-indexed counting).<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">0 INP \/\/the first INPUT value is copied into the accumulator.\n 1 STA value1 \/\/the accumulator contents are stored in RAM address 10\n (labelled value1).\n 2 while LDA value1 \/\/the start of the while loop, the data stored in RAM\n address 10 (labelled value1) is loaded into the accumulator.\n 3 BRZ endwhile \/\/the check to exit the loop, branch to RAM address 9\n (labelled endwhile) if the accumulator contains zero.\n 4 SUB count \/\/subtract the contents of RAM address 11 (labelled count) from\n the contents of the accumulator and store the result in the\n accumulator.\n 5 STA value1 \/\/the accumulator contents are stored in RAM address 10\n (labelled value1).\n 6 LDA nineninenine \/\/the data stored in RAM address 12 (labelled nineninenine) is\n loaded into the accumulator.\n 7 OUT \/\/the value in the accumulator is sent to the OUTPUT.\n 8 BRA while \/\/the end of the while loop, branch to the memory location 2\n (labelled while).\n 9 endwhile HLT \/\/the program halts.<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Example results<\/h4>\n\n\n\n<p>INPUT = 5<br>OUTPUT = 999, 999, 999, 999, 999<\/p>\n\n\n\n<h3 class=\"gb-headline gb-headline-fac7eb97 gb-headline-text\">Further Readings:<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Do_while_loop\" target=\"_blank\" rel=\"noopener\">Do while loop<\/a><\/li><li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Little_man_computer\" target=\"_blank\" rel=\"noopener\">Little man computer<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The following program will show the use of branch instructions to create a WHILE\/ENDWHILE iteration (loop) structure in a program. The WHILE\/ENDWHILE iteration structure tests the condition at the beginning of a loop (in this program, the loop continues while value1 is not zero). This means that if this test result is false (value1 is &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"LMC Iteration Structures: While\/Endwhile Loop\" class=\"read-more button\" href=\"https:\/\/teachcomputerscience.com\/lmc-iteration-structures\/\" aria-label=\"More on LMC Iteration Structures: While\/Endwhile Loop\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_generate-full-width-content":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[16],"tags":[204,47],"class_list":["post-395","post","type-post","status-publish","format-standard","hentry","category-lmc","tag-article","tag-hide-old-upsell","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50"],"acf":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/teachcomputerscience.com\/wp-json\/wp\/v2\/posts\/395","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/teachcomputerscience.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/teachcomputerscience.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/teachcomputerscience.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/teachcomputerscience.com\/wp-json\/wp\/v2\/comments?post=395"}],"version-history":[{"count":1,"href":"https:\/\/teachcomputerscience.com\/wp-json\/wp\/v2\/posts\/395\/revisions"}],"predecessor-version":[{"id":605482,"href":"https:\/\/teachcomputerscience.com\/wp-json\/wp\/v2\/posts\/395\/revisions\/605482"}],"wp:attachment":[{"href":"https:\/\/teachcomputerscience.com\/wp-json\/wp\/v2\/media?parent=395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/teachcomputerscience.com\/wp-json\/wp\/v2\/categories?post=395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/teachcomputerscience.com\/wp-json\/wp\/v2\/tags?post=395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}