; An example of a machine-language program
;
; The problem
; Read a series of numbers from memory
; beginning at x4000. Read until the
; number 0 is read. Count how many
; of the numbers are even and odd.
; Store the count of even numbers at
; x3200 and the number of odd numbers
; at x3201.
;
; A first pass in LC-2 Assembly Language:
; A bare-bones sentinel-controlled loop
;
; Register usage
; R1 -- input data pointer
; R2 -- most recent value
;
x3000 LD R1, x006        ; load data pointer
x3001 LDR R2, R1, #0     ; load data value
x3002 BRz x005           ; branch to end if zero
      ;
      ; repeating statements go here
      ;
x3003 ADD R1, R1, #1     ; increment data pointer
x3004 BRnzp x001         ; branch back to top
x3005 HALT
      ;
      ; data section
      ;
x3006 x4000 ; address of data
 

; The translation into LC-2 Machine Code

      0011 0000 0000 0000      ; load at x3000
X3000 0010 001 0 0000 0110     ; LD R1, x006
x3001 0110 010 001 000000      ; LDR R2, R1, #0
x3002 0000 010 0 0000 0101     ; BRz x005
      ;
      ; repeating statements go here
      ;
x3003 0001 001 001 1 00001     ; ADD R1, R1, #1
x3004 0000 111 0 0000 0001     ; BRnzp x001
x3005 1111 0000 0010 0101      ; HALT
       ;
      ; data section
      ;
x3006 0100 0000 0000 0000      ; x4000

 

; The next refinement: Adding the loop body
; in LC-2 Assembly Language

; Register usage
; R1 -- input data pointer
; R2 -- most recent value
; R3 -- scratch (temp results)
; R4 -- even counter
; R5 -- odd counter

        LD R1, xOOF         ; load address of data
        AND R4, R4, #0      ; set even-counter to 0
        AND R5, R5, #0      ; set odd-counter to 0
        ;
        ; begin loop to process numbers
        ;
x3003   LDR R2, R1, #0      ; load next number
        BRz xOOC            ; branch to end if zero
        ;
        ; check for even or odd
        ;
        AND R3, R2, #1      ; use mask to check bit-0
        BRz xOO9            ; branch if even
        ADD R5, R5, #1      ; increment odd-count
        BRnzp xOOA          ; branch to get next number
x3009   ADD R4, R4, #1      ; increment even-count
        ;
        ; get next number
        ;
x300A   ADD R1, R1, #1      ; increment data pointer
        BRNZP xOO3          ; branch to top of loop
        ;
        ; store results when done
        ;
x300C   STI R4, x010        ; store even count
        STI R5, x011        ; store odd count
        HALT                ; stop
        ;
        ; data section
                     ;
x300F   x4000               ; address of input values
x3010   x3200               ; address of even-count
x3011   x3201               ; address of odd-count

 

; Translation into LC-2 Machine Code

0011 0000 0000 0000         ; load at x3000
0010 001 0 0000 1111        ; load data pointer in R1
0101 100 100 1 00000        ; clear R4
0101 101 101 1 00000        ; clear R5
    ;
    ; begin loop to process numbers
           ;
0110 010 001 000000         ; load data value in R2
0000 010 0 0000 1100        ; branch to end if zero
    ;
    ; check for even or odd
    ;
0101 011 010 1 00001        ; use mask to check bit-0
0000 010 0 0000 1001        ; branch if even
0001 101 101 1 00001        ; increment odd-count
0000 111 0 0000 1010        ; branch to get next
0001 100 100 1 00001        ; increment even-count
    ;
    ; get next number
    ;
0001 001 001 1 00001        ; increment data pointer
0000 111 0 0000 0011        ; branch to top of loop
    ;
    ; store results when done
    ;
1011 100 0 0001 0000        ; store even count
1011 101 0 0001 0001        ; store odd count
1111 0000 0010 0101         ; halt
    ;
    ; data section
    ;
0100 0000 0000 0000         ; address of data
0011 0010 0000 0000         ; address of even-count
0011 0010 0000 0001         ; address of odd-count