Programming BSAC304

  • Isidor10
    20th April Member 0 Permalink

    Some of you may have been following the computer tutorial series, which as of CL305 has reached the point of being at least somewhat usable for useful work.


    For historical reasons, I'm naming the thing BSAC, for Bray Storage Automatic Calculator. Going forward, each 'model' number will correspond to the numbering of the save it appears in -- for example, BSAC302 supports only the ADD, SUB, HALT and NOP instructions, since CL302 added ADD and SUB.



    To accompany the release of the in-game hex editor for BSAC304, I will start posting a few small programming challenges to practice writing raw machine code for this antique CPU.


    I’ve been writing programs on paper with this kind of syntax:


    FIBBO2
    Calculates Fibbonacci numbers.
    address | op ; comment
    ----------------------------------------------------------------
    010 | T 140 ; zero out the accumulator
    011 | A 015 ; acc += :one
    012 | U 140 ; M[140] = 1
    013 | A 015 ; acc += :one
    014 | U 141 ; M[141] = 2
    015 :one | X 001 ; no-op: double duty as storing the literal constant 1
    016 :loop | A 140 ; acc += M[140]
    017 | U 140 ; M[140] = acc
    018 | A 141 ; acc += M[141]
    019 | U 141 ; M[141] = acc
    01a | E 012 ; if(acc > 0) goto :loop
    01b | Z

    Only the column under ‘op’ is actually entered into the computer,
    everything else is a comment. The left-hand column shows the address of
    each instruction.



    Challenge 304.1 - Swap


    Write a program which swaps two 24-bit numbers A and B, located at A=0x000 and B=0x001. By this I mean: your program should HALT with the values in 0x001 and 0x000 swapped. You may ignore any higher bits.



    Challenge 304.2 - Software Shifts


    1. Write a program that takes in a number x, located at 0x000, and HALTs with 8*x stored at 0x100 by doing acc += x eight times.
    2. Write a faster version that computes `8 * x` using eight or fewer instructions.
      Hint 1: `8*x = 2*2*2*x`
      Hint 2: Calculate `2*x` the usual way, then store it somewhere. Double the new number twice to get 8*x.
    3. Use what you learned in b) to compute 2048*291.


    Challenge 304.3 - Software Multiplication


    1. Write a program starting at 0x030 that reads two positive numbers from memory at A=0x000 and B=0x001,
      then adds A to A B many times. The result should be stored at C=0x002.
    2. This program has O(B) complexity. Add an optimisation that swaps A and B if B > A.
    3. Does this program work for negative numbers? If not, find a way to correct it.


    Solutions will be posted in two weeks' time.
    Edited 4 times by Isidor10. Last: 20th April
  • ALumpOfPowderToy
    22nd April Member 0 Permalink

    I'd love for the assembly opcodes to not be one letter each. May I suggest the following?

    T -> MOV/LOD/SET/CPY

    U -> UPD

    A -> ADD

    S -> SUB

    X -> NOP

    Z -> HLT

     

  • Isidor10
    22nd April Member 0 Permalink
    You're more than welcome to use those mnemonics for the opcodes, but I've already mentally committed to naming these after the original codes used in the EDSAC instruction set (go to page 58).
    Edited once by Isidor10. Last: 22nd April