{"id":11896,"date":"2025-09-12T03:00:00","date_gmt":"2025-09-12T07:00:00","guid":{"rendered":"https:\/\/www.both.org\/?p=11896"},"modified":"2025-09-08T14:44:02","modified_gmt":"2025-09-08T18:44:02","slug":"my-start-with-basic-programming","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=11896","title":{"rendered":"My start with BASIC programming"},"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=\"11896\" 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\">    <\/span>\r\n<\/div><\/div>\n<p>Microsoft recently released the <a href=\"https:\/\/opensource.microsoft.com\/blog\/2025\/09\/03\/microsoft-open-source-historic-6502-basic\/\">source code to 6502 BASIC<\/a>. If you don&#8217;t know, Microsoft got its start by selling BASIC. As the website points out:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Microsoft BASIC began in 1975 as the company&#8217;s very first product: a BASIC interpreter for the Intel 8080, written by Bill Gates and Paul Allen for the Altair 8800. That codebase was soon adapted to run on other 8-bit CPUs, including the MOS 6502, Motorola 6800, and 6809.<\/p>\n<\/blockquote>\n\n\n\n<p>and:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>The 6502 port was completed in 1976 by Bill Gates and Ric Weiland. In 1977, Commodore licensed it for a flat fee of $25,000, a deal that placed Microsoft BASIC at the heart of Commodore&#8217;s PET computers and, later, the VIC-20 and Commodore 64.<\/p>\n<\/blockquote>\n\n\n\n<p>and:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>This is BASIC M6502 8K VER 1.1, the 6502 BASIC lineage that powered an era of home computing and formed the foundation of Commodore BASIC in the PET, VIC-20, and the legendary Commodore 64. This very source tree also contains adaptations for the Apple II (&#8220;Applesoft BASIC&#8221;), built from the same core BASIC source. The original headers still read, &#8220;BASIC M6502 8K VER 1.1 BY MICRO-SOFT&#8221;\u2014a time capsule from 1978.<\/p>\n<\/blockquote>\n\n\n\n<p>Yes, that early port of BASIC to the 6502 became what many programmers of my generation learned as Applesoft BASIC. I learned BASIC programming on the Apple II at school, and my family bought an Apple II+ clone called the Franklin ACE 1000. My brother and I read books to teach ourselves how to write programs. That was my start with programming!<\/p>\n\n\n\n<p>To celebrate this release of Microsoft&#8217;s 6502 BASIC, I&#8217;d like to show you how I wrote programs on the Apple II:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"the-basics-of-basic\">The basics of BASIC<\/h2>\n\n\n\n<p>Applesoft BASIC programs used line numbers; every instruction for the computer had a line number next to it. So if you wanted to write a simple 3-line program that counted from 1 to 10, you had to use line numbers like 10, 20, and 30.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10  FOR I = 1 TO 10\n20  PRINT I\n30  NEXT I<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1148\" height=\"665\" src=\"http:\/\/www.both.org\/wp-content\/uploads\/2025\/09\/basic_1_10.png\" alt=\"Code listing for the '1 to 10' program\" class=\"wp-image-11891\"\/><figcaption class=\"wp-element-caption\">An Applesoft BASIC program to count from 1 to 10, running on an Apple II emulator<\/figcaption><\/figure>\n\n\n\n<p>Using line numbers may seem awkward today, but back then it was the only way to enter a program into the computer. And the line numbers made it easy to edit an instruction, or to add a new instruction. For example, I could modify this program to print ten random numbers instead, just by modifying line 20. To do that, I would type a new line 20, and Applesoft BASIC would insert that as a new line 20 in the program:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>20  PRINT RND (1)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1148\" height=\"665\" src=\"http:\/\/www.both.org\/wp-content\/uploads\/2025\/09\/basic_rnd.png\" alt=\"Code listing for the '10 random numbers' program\" class=\"wp-image-11892\"\/><\/figure>\n\n\n\n<p>The <code>RND(1)<\/code> function generates a random value between 0 and 1, although it never <em>quite<\/em> gets to 1. The closest it can get is 0.999999. This feature of random numbers in Applesoft BASIC means you can &#8220;scale&#8221; the random number to fit any range.<\/p>\n\n\n\n<p>Let&#8217;s see how to do that by updating the program one more time to print random numbers from 1 to 100. To do that, we need to write a new line 20 that multiplies the <code>RND(1)<\/code> function by 100, which gives a number between 0 and 99.999999. If we add the <code>INT<\/code> function to take the <em>integer<\/em> of that value, we are left with a number from 0 to 99. We can add 1 to the end, to get a random number from 1 to 100:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>20  PRINT  INT ( RND (1) * 100) + 1<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1148\" height=\"700\" src=\"http:\/\/www.both.org\/wp-content\/uploads\/2025\/09\/basic_rnd_100.png\" alt=\"Code listing for the '1 to 100' program\" class=\"wp-image-11893\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"a-random-number-program\">A random number program<\/h2>\n\n\n\n<p>One way to exercise random numbers in Applesoft BASIC is with a program to draw the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Sierpi%C5%84ski_triangle\">Sierpinski Triangle<\/a>. There are several ways to draw the triangle, but one method uses a chaos &#8220;game&#8221; that uses this process:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Start with three points that define a triangle (I&#8217;ll use the upper-left corner of the screen, the upper-right, and the bottom-middle)<\/li>\n\n\n\n<li>Draw a dot at a random location anywhere on the screen<\/li>\n\n\n\n<li>Iterate by selecting a random corner of the triangle; draw the next dot halfway between the corner and the previous dot<\/li>\n\n\n\n<li>Repeat<\/li>\n<\/ol>\n\n\n\n<p>When I started to write BASIC programs, I found it helped to write down a version of the program in plain text that described what that program would do. Another term for this is <em>pseudocode<\/em>, which might look like this to draw the Sierpinski Triangle:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>set high resolution mode\n\ndefine arrays to hold the 3 corners\n  and set the x,y coordinates for the corners\n\npick a random x,y coordinate\n\nloop:\n  plot the x,y coordinate\n\n  pick a random corner: 1, 2, or 3\n\n  find the halfway x,y point between the old\n    coordinate and that corner<\/code><\/pre>\n\n\n\n<p>And we could type in an Applesoft BASIC program to implement this routine:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10  HGR\n20  DIM X(3): DIM Y(3)\n30  LET X(1) = 0: LET Y(1) = 0\n40  LET X(2) = 279: LET Y(2) = 0\n50  LET X(3) = 140: LET Y(3) = 159\n60  LET XA =  INT ( RND (1) * 280)\n70  LET YA =  INT ( RND (1) * 160)\n80  FOR I = 1 TO 100\n90  HPLOT XA,YA\n100 LET N =  INT ( RND (1) * 3) + 1\n110 LET XN = (XA + X(N)) \/ 2: LET XA =  INT (XN)\n120 LET YN = (YA + Y(N)) \/ 2: LET YA =  INT (YN)\n130 NEXT I<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1148\" height=\"818\" src=\"http:\/\/www.both.org\/wp-content\/uploads\/2025\/09\/basic_sierp.png\" alt=\"Code listing for the Sierpinski Triangle program\" class=\"wp-image-11894\"\/><\/figure>\n\n\n\n<p>This program also demonstrates a few other features in Applesoft BASIC:<\/p>\n\n\n\n<p>First, variables can only be one or two letters long, which is why I&#8217;m stuck using variables like <code>XN<\/code> where I might write <code>x_new<\/code> in a modern program. Note that BASIC used the <code>LET<\/code> statement to assign a value to a variable.<\/p>\n\n\n\n<p>Also, arrays start at 1. This is different from the C programming language and other languages, which use 0 for the first array index.<\/p>\n\n\n\n<p>The Apple II supported two graphics modes: <code>GR<\/code> for a low-resolution mode and <code>HGR<\/code> for a high-resolution mode. &#8220;High-resolution&#8221; is relative here; <code>HGR<\/code> was 280&#215;160 pixels. Pixel coordinates started at 0, so the <em>x<\/em> coordinate could be 0 to 279, and the <em>y<\/em> coordinate could be 0 to 159.<\/p>\n\n\n\n<p>You could get a few more rows of pixels by using <code>HGR2<\/code>, which eliminated the last few text rows, but I&#8217;ll use normal <code>HGR<\/code> mode so I can see what I might need to type after the program runs.<\/p>\n\n\n\n<p>If you run the program on an Apple II computer, you&#8217;ll get 100 random points on the screen that start to approximate the Sierpinski Triangle. If you update line 80 to iterate through a much larger set of points, like 2000 points, you can see the triangle emerge from the chaos:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1148\" height=\"818\" src=\"http:\/\/www.both.org\/wp-content\/uploads\/2025\/09\/basic_sierp_done.png\" alt=\"Sierpinski Triangle as drawn on an Apple II computer\" class=\"wp-image-11895\"\/><\/figure>\n\n\n\n<p>If you want to try it for yourself, type the program into an <a href=\"https:\/\/www.scullinsteel.com\/apple2\/\">Apple II emulator<\/a> and watch it run. Or you can see it in action by watching this <a href=\"https:\/\/www.youtube.com\/watch?v=cxolyi7yUaM\">video demonstration<\/a> on my YouTube channel.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s how I wrote my first BASIC programs on the Apple II.<\/p>\n","protected":false},"author":33,"featured_media":3514,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[69,150],"tags":[147,152],"class_list":["post-11896","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-fun","category-programming","tag-fun","tag-programming"],"modified_by":"Jim Hall","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/11896","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=11896"}],"version-history":[{"count":1,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/11896\/revisions"}],"predecessor-version":[{"id":11897,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/11896\/revisions\/11897"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/3514"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}