Friday, 20 December 2013

Write a "bit pattern" output routine that prints to the console the value stored in a register (say R2) as a 16-bit binary number. So if R2 contains the value x1234, then the o/p would be just: b0001 0010 0011 0100 i.e. the ascii symbols for ‘b’ followed by the appropriate sequence of 1's and 0's (the space separating groups of 4 bits is optional, but the initial ‘b’ is required). For this exercise, you can have the program itself set up the register with the value to be output: LD R2, number ;set up R2 with stored value ... HALT number .FILL x1234 ;hard coded value

    .ORIG x3000
    LD R2 Number
    LD R3 Max
Start
    NOT R0 R3
    ADD R0 R0 #1
    ADD R0 R2 R0
    ADD R6 R0 #0
    BRn Zero

    ADD R3 R3 #0
    BRz Done

    LD R0 OneASCII
    TRAP x21
    ADD R2 R6 #0
    JSR DivBy2
    BR Start
Zero
    LD R0 ZeroASCII
    TRAP x21
    JSR DivBy2
    BR Start
Done
    HALT
DivBy2
    AND R0 R0 #0
    ADD R4 R3 #0
Again
    ADD R4 R4 #-2
    BRn Skip
    ADD R0 R0 #1
    BR Again
Skip
    ADD R3 R0 #0
    RET
Number    .FILL #64
Max    .FILL #32768
ZeroASCII .FILL x30
OneASCII .FILL X31
    .END

No comments:

Post a Comment