Solution

Architecture, Low-Level Programming

Problems to be Submitted (20 points)

  1. (4 points)

    212 = 4096 cells of memory could be used.

    The only way for instructions to use a memory cell as data is by referencing it as the operand in an instruction such as STO, LOD, or ADD. Based on the existing instruction formats, the opcode is 4 bits and the operand is a 12-bit reference to a memory cell containing the operand. Similarly, all of the jump instructions have 12-bit operands, resetting the PC to a 12-bit memory address. Given that 12 bits are being used to address a memory cell, there can be at most 212 distinct such addresses.

  2. (3 points)

    21 was the input.

    Recall instructions are numbered from 0, not 1. The only way for instruction 3 to be skipped is if the accumulator held value 0 at the time instruction 2 (JZR BYE) was executed. The value inthe accumulator at that time came from the subtraction in step 1, subtracting VAL (21) from the input. Thus, the input must have been 21 for that subtraction to result in 0.

  3. (3 points)


    0011000000001101
    0111000000000000
    1111000000000000

    Please note that the problem asked for machine code as a solution. Of course, the above is equivalent to the assembly code,
    LOD 13
    OUT
    STO

  4. (5 points)

    There is more than one way to succeed. Here is one possible solution:

         INP
         STO A
         INP
         STO B
         LOD A
         SUB B
         OUT
         STP
    A    DAT
    B    DAT
     

  5. (5 points)

    There is more than one way to succeed. Here is one possible solution:

         INP
         STO A
         INP
         STO B
         SUB A
         JNG MAXA    ; A is the bigger
         LOD B       ; else B is the bigger
         JMP DONE    
    MAXA LOD A
    DONE OUT         ; output the answer
         STP
    A    DAT
    B    DAT
     


Extra Credit

  1. (2 points)

         INP
         STO D
         INP         ; this is N
    LOOP SUB D       ; subtract D (again)
         JNG NEG     ; until we get negative number
         JMP LOOP
    NEG  ADD D       ; we subtracted one time too many
         OUT
         STP
    D    DAT
     

  2. (2 points)

         INP
    LOOP STO CNT     ; we'll need this
         ADD SUM     ; add current cnt to sum
         STO SUM     ; and re-store new sum
         LOD CNT
         SUB ONE     ; subtract 1 from count
         JZR DONE    ; exit loop if count reaches zero
         JMP LOOP
    DONE LOD SUM     ; reload the answer
         OUT
         STP
    SUM  DAT 0
    CNT  DAT
    ONE  DAT 1      
    


Last modified: Thursday, 13 March 2008