summaryrefslogtreecommitdiff
path: root/cmpen472hw6_McDonnell/Sources/main.asm
diff options
context:
space:
mode:
Diffstat (limited to 'cmpen472hw6_McDonnell/Sources/main.asm')
-rw-r--r--cmpen472hw6_McDonnell/Sources/main.asm167
1 files changed, 166 insertions, 1 deletions
diff --git a/cmpen472hw6_McDonnell/Sources/main.asm b/cmpen472hw6_McDonnell/Sources/main.asm
index 62592ce..1e9eb0c 100644
--- a/cmpen472hw6_McDonnell/Sources/main.asm
+++ b/cmpen472hw6_McDonnell/Sources/main.asm
@@ -84,6 +84,13 @@ lenBuf dc.w $0010 ; Length of buffer array
buffer2 ds.b $0010 ; Array of 16 bytes for reading and reversal
dc.b NULL ; NULL terminated
lenBuf2 dc.w $0010 ; length of buffer2
+
+badAddr dc.b 'invalid input, address',CR,LF,NULL ; Error message for bad address
+
+badData dc.b 'invalid input, data',CR,LF,NULL ; Error message for bad data
+
+badCom dc.b 'invalid input, command',CR,LF,NULL ; Error message for bad command
+
* There is a second Data Section at the end of the file.
*
**************************************************************************
@@ -134,6 +141,17 @@ mainLoop
ldx #Counter
jsr PrintMem
+ ldx #buffer
+ ldy lenBuf
+ jsr Zeros
+
+ ldx #buffer
+ ldy lenBuf
+ jsr ReadString
+
+ ldx #buffer
+ jsr ReadDecimal
+
bra mainLoop
ldx #msg ; Load the address of msg into X
@@ -179,7 +197,138 @@ twReadLoop jsr getchar ; Read Character from Serial
CheckInput
rts ; Return to caller
+
+;*************************************************************************
+; ReadHex subroutine
+;
+; This subroutine will read an ASCII string of a number in hex and convert it to
+; its value.
+;
+; Input: A memory address in register X.
+; Output: The value of the hex number in the Y register, and any errors printed
+; to the seriel line. Zero bit is set if error occurs.
+; Registers in use: X for the address of the contents and for a buffer while printing,
+; D for multiplication, B for the character, Y for output value.
+; Memory locations in use: Memory Address for serial line, address of the string
+;
+; Comments: This subroutine will return the value in the Y register, and if an error occurs,
+; the Zero bit in the CCR will be set.
+;
+
+ReadHex
+ pshx ; Save X to the stack
+ pshd ; Save D to the stack
+ ldy #0 ; Clear Y register
+ ldab 1,x+ ; Read character from X into B, add 1 to X
+ cmpb #'$' ; Compare B to '$'
+ bne rHError ; If B != '$', jump to error, not hex data
+rHLoop ldab 1,x+ ; Read Next character from X
+ beq rHDone ; If B == 0, exit loop
+ cmpb #' ' ; Compare B to space character
+ beq rHDone ; If B == ' ', exit loop
+ cmpb #'0' ; Compare B to '0' character
+ blt rHError ; If B < '0', bad address, exit loop
+ cmpb #'9' ; Compare B to '9' character
+ bhi rHAlpha ; If B > '9', check if 'A'-'F' characters
+ subb #'0' ; Subtract '0' from B to get true value
+ pshb ; Save B to the stack
+ ldd #16 ; load 16 into D
+ emul ; Multiply Y and D
+ exg d,y ; Transfer data from D to Y
+ pulb ; Restore b from the stack
+ aby ; Add B to Y
+ bra rHLoop ; Branch always to rHLoop
+rHAlpha cmpb #'A' ; Compare B to 'A' character
+ blt rHError ; If B < 'A', bad address, exit loop
+ cmpb #'F' ; Compare B to 'F' character
+ bhi rHError ; If B > 'F', invalid data, error out
+ subb #'A' ; Subtract 'A' from B to get true value
+ addb #$A ; Add $A to B to account for offet
+ pshb ; Save B to the stack
+ ldd #16 ; load 16 into D
+ emul ; Multiply Y and D
+ exg d,y ; Transfer data from D to Y
+ pulb ; Restore b from the stack
+ aby ; Add B to Y
+ bra rHLoop ; Branch always to rHLoop
+rHDone clra ; clear A accumulator
+ tap ; Transfer A into CCR to clear zero bit
+ puld ; Restore D from the stack
+ pulx ; Restore X from the stack
+ rts ; Return to caller
+rHError ldx #badAddr ; Load address of bad address string into X
+ jsr WriteString ; Write string to seriel output
+ ldaa #4 ; Load 4 into A to set zero bit in CCR
+ tap ; Transfer A into CCR to set zero bit and warn error
+ puld ; Restore D from the stack
+ pulx ; Restore X from the stack
+ rts ; Return to caller
+;*************************************************************************
+; ReadDecimal subroutine
+;
+; This subroutine will read an ASCII string of a number in decimal and convert it to
+; its value.
+;
+; Input: A memory address in register X.
+; Output: The value of the number in the Y register, and any errors printed
+; to the seriel line. Zero bit is set if error occurs.
+; Registers in use: X for the address of the contents and for a buffer while printing,
+; D for multiplication, B for the character, Y for output value.
+; Memory locations in use: Memory Address for serial line, address of the string
+;
+; Comments: This subroutine will return the value in the Y register, and if an error occurs,
+; the Zero bit in the CCR will be set.
+;
+
+ReadDecimal
+ pshx ; Save X to the stack
+ pshd ; Save D to the stack
+ ldy #0 ; Clear Y register
+dHLoop ldab 1,x+ ; Read Next character from X
+ beq dHDone ; If B == 0, exit loop
+ cmpb #' ' ; Compare B to space character
+ beq dHDone ; If B == ' ', exit loop
+ cmpb #'0' ; Compare B to '0' character
+ blt dHError ; If B < '0', bad address, exit loop
+ cmpb #'9' ; Compare B to '9' character
+ bhi dHError ; If B > '9', check if 'A'-'F' characters
+ subb #'0' ; Subtract '0' from B to get true value
+ pshb ; Save B to the stack
+ ldd #10 ; load 10 into D
+ emul ; Multiply Y and D
+ exg d,y ; Transfer data from D to Y
+ pulb ; Restore b from the stack
+ aby ; Add B to Y
+ bra dHLoop ; Branch always to rHLoop
+dHDone clra ; clear A accumulator
+ tap ; Transfer A into CCR to clear zero bit
+ puld ; Restore D from the stack
+ pulx ; Restore X from the stack
+ rts ; Return to caller
+dHError ldx #badAddr ; Load address of bad address string into X
+ jsr WriteString ; Write string to seriel output
+ ldaa #4 ; Load 4 into A to set zero bit in CCR
+ tap ; Transfer A into CCR to set zero bit and warn error
+ puld ; Restore D from the stack
+ pulx ; Restore X from the stack
+ rts ; Return to caller
+
+;*************************************************************************
+; PrintMem subroutine
+;
+; This subroutine will print the address and contents of the given memory location.
+;
+; Input: A memory address in register X.
+; Output: The memory address in Hex and the contents in binary, hex, & decimal
+; on the seriel output.
+; Registers in use: X for the address of the contents and for a buffer while printing,
+; D for the contents of the location, Y for another buffer address while
+; reversing strings and printing.
+; Memory locations in use: Memory Address for serial line, address of the string
+;
+; Comments: This subroutine requires the seriel console to be setup before calling.
+;
PrintMem
pshy ; Save Y to the Stack
@@ -221,7 +370,23 @@ PrintMem
puld ; Restore D from the stack
puly ; Restore Y from the stack
rts ; Return to caller
-
+
+;*************************************************************************
+; strrev subroutine
+;
+; This subroutine will reverse a string from one buffer into another.
+;
+; Input: Address of null terminated string in X, address of a large enough
+; buffer in Y.
+; Output: The string in X reversed in Y.
+; Registers in use: X for the address of the string, Y for the address of the buffer,
+; A to read characters from the string.
+; Memory locations in use: Memory Address for serial line, address of the string & buffer
+;
+; Comments: This subroutine will not check that the output buffer is large enough, that
+; is the job of the caller.
+;
+
strrev
pshx ; Save X to the stack
pshy ; Save Y to the stack