Friday, 20 December 2013

Now modify the routine of exercise 2 to perform full input validation, as well as allowing the user to input the bit pattern with spaces separating groups of bits – e.g. the user might input the number as b0001 0010 0011 0100. This means that you must test the first digit for ‘b’, then each succeeding digit for ‘0’, ‘1’, or ‘ ‘. If it is ‘0’ or ‘1’, continue building the binary number; if it is ‘ ‘, ignore it & get the next digit; if it is anything else, output an error message & quit the program. Likewise, if the user attempts to enter a 17th digit, or a CR/LF before the 16th digit, output an error message and quit.

    .ORIG x3000
    LD R2 Counter
    LD R5 MinusTen
    LD R4 Max
    AND R3 R3 #0    ; will store ans

    LEA R0 Prompt
    TRAP x22
Input
    TRAP x20
    TRAP x21
   
    LD R7 Space
    ADD R7 R0 R7
    BRz Input

    ADD R1 R0 R5
    BRz Enter

    ADD R2 R2 #0
    BRz Invalid

    LD R7 Svntn
    ADD R7 R2 R7
    BRz Checkb

    LD R7 Zero
    ADD R7 R0 R7
    BRz ZeroLbl

    LD R7 One
    ADD R7 R0 R7
    BRz OneLbl
    BR Invalid

Enter
    ADD R2 R2 #0
    BRnp Invalid
    ADD R0 R3 #0
    TRAP x21
    BR Done
Checkb
    ADD R2 R2 #-1
    LD R7 B
    ADD R7 R0 R7
    BRnp Invalid
    BR Input
ZeroLbl
    ADD R2 R2 #-1
    JSR DivBy2
    BR Input
OneLbl
    ADD R2 R2 #-1
    ADD R3 R3 R4
    JSR DivBy2
    BR Input
Invalid
    AND R0 R0 #0
    ADD R0 R0 #10
    TRAP x21
    LEA R0 ErrMsg
    TRAP x22
Done
    HALT
DivBy2                ; subroutine that divides max by 2
    AND R0 R0 #0
    ADD R1 R4 #0
Loop
    ADD R1 R1 #-2
    BRn Skip
    ADD R0 R0 #1
    BR Loop
Skip
    ADD R4 R0 #0
    RET

Prompt    .STRINGZ "Enter Binary: "
ErrMsg    .STRINGZ "Invalid Input"
Counter    .FILL #17
Svntn    .FILL #-17
MinusTen .FILL #-10
B    .FILL #-98
Zero    .FILL #-48
One    .FILL #-49
Space    .FILL #-32
Max    .FILL #32768
    .END

No comments:

Post a Comment