Friday, 20 December 2013

The inverse of exercise 1: write a bit pattern input routine. Use the .STRINGZ pseudo-op and the trap instruction PUTS to prompt the user to input a 16-bit binary number preceded by ‘b’, and terminating with the CR/LF character (ASCII x0A, the newline character). The user inputs the characters b0001001000110100 (without spaces), which you are to store in a register (R2) as the corresponding 16-bit number. Assume the user will always input the initial ‘b’ (which you may simply discard), and exactly 16 bits. Combine this with the code of exercise 1 – i.e. replace the hard-coded setup of R2 with the user input code.

    .ORIG x3000
    LD R3 Zr
    LD R1 On
    LEA R2 Binary
    AND R6 R6 #0    ; it'll contain your answer
    LD R5 Max
Start
    ADD R2 R2 #1
    LDR R7 R2 #0
    BRz Done

    NOT R7 R7
    ADD R7 R7 #1
    ADD R7 R3 R7
    BRz Zero

    ADD R6 R6 R5
    JSR DivBy2
    BR Start
Zero
    JSR DivBy2
    BR Start
Done
    HALT
DivBy2
    AND R0 R0 #0
Again
    ADD R5 R5 #-2
    BRn Skip
    ADD R0 R0 #1
    BR Again
Skip
    ADD R5 R0 #0
    RET
Binary    .STRINGZ "b0001001000110100"
Max    .FILL #32768
Zr    .FILL x030
On    .FILL X031
    .END

No comments:

Post a Comment