{"id":5304,"date":"2024-05-18T03:00:00","date_gmt":"2024-05-18T07:00:00","guid":{"rendered":"https:\/\/www.both.org\/?p=5304"},"modified":"2024-05-25T06:51:35","modified_gmt":"2024-05-25T10:51:35","slug":"my-first-programming-language","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=5304","title":{"rendered":"My first programming language"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"5304\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">1    <\/span>\r\n<\/div><\/div>\n<p class=\"wp-block-paragraph\">The BASIC programming language <a href=\"https:\/\/www.computermuseumofamerica.org\/2024\/05\/01\/basic-turns-60\/\">turned 60 years old<\/a> this month. On May 1, 1964, professor John Kemeny with a student <a href=\"https:\/\/www.dartmouth.edu\/basicfifty\/basic.html\">ran the first BASIC program<\/a> on a Dartmouth College computer system. If you don\u2019t know BASIC, the <em>Beginner\u2019s All-purpose Symbolic Instruction Code<\/em>, it was a simple programming language that made it easy for beginners to write their first computer program.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">BASIC became a popular programming language to include in early personal computers. The Commodore PET, TRS-80, and Apple computers (1977) all came with their own versions of BASIC. The IBM PC 5150 (1981) also included a version of BASIC. With BASIC, it was finally possible for everyday computer users to create their own useful and interesting programs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And because BASIC has turned 60 years old, I\u2019d like to look back and celebrate BASIC as my first programming language.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"apple-basic\">Apple BASIC<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">My family bought an Apple II computer, and my brother and I read books to teach ourselves how to write our own BASIC programs. My first programs were pretty simple: math quizzes and puzzles, like a game where the computer picked a random number from 1 to 100 and you had to guess it. BASIC made this easy for me. Its simple syntax and numbered lines gave me a certain flexibility to create programs. As I became more advanced, I learned about graphics programming and created more complex and visually interesting programs like adventure games and simulations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I can go back to Apple BASIC thanks to an open source <a href=\"https:\/\/www.scullinsteel.com\/apple2\">Apple II emulator<\/a> called <code>apple2js<\/code> by Will Scullin. You can find the source code at <a href=\"https:\/\/github.com\/whscullin\/apple2js\">apple2js on GitHub<\/a>, under the MIT license.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Scullin\u2019s Apple II emulator provides the virtual hardware, and you can select different virtual floppy disks using the menu. Booting from the DOS 3.3 Master gives me the same environment I used to create my Apple BASIC programs back in the late 1970s and early 1980s.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"620\" height=\"853\" src=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/05\/basic0.png\" alt=\"\" class=\"wp-image-5305\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Apple provided a very simple environment that immediately put you into a \u201cprogrammer\u201d mode. You could enter one-line BASIC commands here, such as <code>PRINT \"Hello\"<\/code> to print some text, or a one-line instruction like <code>FOR I = 1 TO 21 : PRINT I : NEXT I<\/code> to print the numbers from 1 to 21. To start a new program, use the <code>NEW<\/code> command, and to clear all variables from memory, use the <code>CLEAR<\/code> instruction. The <code>HOME<\/code> command clears the screen and positions your prompt at the upper-left corner.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"592\" height=\"416\" src=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/05\/basic1.png\" alt=\"\" class=\"wp-image-5306\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"low-resolution-graphics\">Low-resolution graphics<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The Apple II had two graphics modes: low resolution and high resolution. The low-resolution mode provided blocky pixels from 0,0 in the upper-left to 39,39 at the lower-right, with a few lines of text at the bottom. You could also select an alternate mode that eliminated the text lines and gave you a few extra rows of graphics.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s explore this graphics mode by writing a simple program to draw a chart. For this sample, we\u2019ll start the graphics at the middle-left of the screen at <em>x=0<\/em> and <em>y=20<\/em>. At each iteration, we\u2019ll generate a random number using <code>RND<\/code>. This gives a floating point value from 0.0 to 0.999\u2026 We\u2019ll move the next pixel \u201cup\u201d or \u201cdown\u201d depending on the value of that random number; if it\u2019s less than 0.5, we\u2019ll move it up, otherwise we\u2019ll move it down.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can describe the functionality using this pseudocode:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>y = 20\nfor x = 0 to 39 {\n  plot x,y\n\n  if (rnd&lt;.5) {\n    y--\n  }\n  else {\n    y++\n  }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With that overview of the program, we can enter the corresponding BASIC code into the Apple II:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"592\" height=\"416\" src=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/05\/basic2.png\" alt=\"\" class=\"wp-image-5307\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">If we run this program, we\u2019ll see a series of low-resolution pixels that start at 0,20 and move up and down according to the values returned by the random number generator. In this case, the plot stays pretty close to <em>y=20<\/em>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"592\" height=\"416\" src=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/05\/basic3.png\" alt=\"\" class=\"wp-image-5308\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">It might be interesting to draw a horizontal line across the finished chart, so we can see how much the random fluctuations deviated from the \u201czero\u201d point. It\u2019s easy to add to a BASIC program by just typing in new lines. Let\u2019s replace the <code>END<\/code> instruction at line 90 with some extra lines to draw a horizontal line in a different color:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"592\" height=\"416\" src=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/05\/basic4.png\" alt=\"\" class=\"wp-image-5309\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The new chart now shows a horizontal line across the middle of the screen, from 0,20 to 39,20.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"592\" height=\"416\" src=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/05\/basic5.png\" alt=\"\" class=\"wp-image-5310\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"high-resolution-graphics\">High-resolution graphics<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Apple BASIC also supported a higher graphics mode at 280,160 resolution, with a few lines of text at the bottom of the screen. As with low resolution graphics, you could also use an alternate mode that used full-screen graphics, and gave you more graphics lines. But let\u2019s use the standard high graphics mode to write a simple program.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Sierpinski\u2019s Triangle is a fun program that demonstrates how you can create a fractal image through successive random values. To generate the triangle, start by defining three points on the screen, and a random <em>x,y<\/em> starting value. To draw successive pixels, pick a vertex of the triangle, and draw a dot at the <em>midpoint<\/em> between the last location and the triangle corner. After many iterations, you will see Sierpinski\u2019s Triangle.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Writing a nontrivial BASIC program can be challenging, so I always started by describing the program using a flow chart or by writing it out as words. In this case, we can describe this Sierpinski Triangle algorithm using pseudocode:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x(1) = 0 ; y(1) = 0\nx(2) = 279 ; y(2) = 0\nx(3) = 140 ; y(3) = 159\n\nxi = random(0 to 279)\nyi = random(0 to 159)\n\nfor n = 1 to 1000 {\n  hplot xi,yi\n\n  i = random(1 to 3)\n  xi = int( (xi+x(i)) \/ 2 )\n  yi = int( (yi+y(i)) \/ 2 )\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now we need to enter that program into the Apple BASIC environment:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"592\" height=\"416\" src=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/05\/basic6.png\" alt=\"\" class=\"wp-image-5311\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">There\u2019s a lot to enter, but the Apple only shows 25 lines of text at a time, and we\u2019ll need more than that to enter the entire program. Here\u2019s what it looks like when I\u2019m done:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"592\" height=\"416\" src=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/05\/basic7.png\" alt=\"\" class=\"wp-image-5312\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">and:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"592\" height=\"416\" src=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/05\/basic8.png\" alt=\"\" class=\"wp-image-5313\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">And when we run the program, the Apple will draw Sierpinski\u2019s Triangle to the screen. This takes several minutes on the Apple II, because 4,000 iterations is a lot for a 1MHz processor, but eventually we\u2019ll see this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"592\" height=\"416\" src=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/05\/basic9.png\" alt=\"\" class=\"wp-image-5314\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"exploring-basic-programming\">Exploring BASIC programming<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Apple BASIC was how I first learned how to write my own computer programs. This simple interface and programming syntax made it easy for me to enter, run, and debug programs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A few years later, my family replaced our Apple II at home with a new IBM Personal Computer PC-5150, running DOS and a new version of BASIC. But it was still BASIC, and I continued to use BASIC until I went to university and learned other programming languages like FORTRAN77 to write data analysis programs and C to write command line utilities. While I prefer more modern programming languages, I still found it fun to rediscover \u201cretro\u201d programming with Apple BASIC using this open source Apple emulator.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Apple BASIC was how I first learned how to write my own computer programs.<\/p>\n","protected":false},"author":33,"featured_media":2803,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[69,150],"tags":[399,147,152],"class_list":["post-5304","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-fun","category-programming","tag-basic","tag-fun","tag-programming"],"modified_by":"David Both","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/5304","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5304"}],"version-history":[{"count":1,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/5304\/revisions"}],"predecessor-version":[{"id":5315,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/5304\/revisions\/5315"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/2803"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}