Skip to content

Instantly share code, notes, and snippets.

@ZacharyJacobCollins
Last active December 14, 2015 00:26
Show Gist options
  • Save ZacharyJacobCollins/15a25925e07c02362f41 to your computer and use it in GitHub Desktop.
Save ZacharyJacobCollins/15a25925e07c02362f41 to your computer and use it in GitHub Desktop.
LC3 input loop
Just the commments. ValidateOne should also be called ifone or something.
.ORIG x3000;
;REGISTER ASSIGNMENT INFORMATION
; R0 is I/O
; R1 manages the address of the storage memory for the first loop
; R2 is a temporary variable
; R3 is used as a Counter here for the first loop
; R4 is counter for the number of zeros
; R5 temp variable for subtraction
; R6 is 48 throughout program
; R7 is used for Subroutines
LEA R1 MEMORYSPACE ; Saves the address of the storage memory block
LD R3 COUNTER ; Declare counter for input loop
LD R5 SUB48 ; Load R5 with -48
LD R6 ADD48 ; Load R6 with 48
LEA R0 PROMPT;
PUTs;
Input loop just grabs the 16 bit binary number
INPUTLOOP;
GETC ; Input character -> r0
ADD R0 R5 R0 ; Subtract 48 here to get actual value of the digit
if the input here was a zero, then increment the 0 counter
BRz IFZERO ; If zero, know it's a good binary input, skip validate for one
this validate one thing here is like, if it is a zero it skips it bc we good. So only other option is a 1.
if it's not a one then it's not a valid input
JSR VALIDATEONE ; Just checked for a zero, only other possibility is a one for valid input. Validates here
RESUMEINPUT ; If went to zero statement, resume loop here
STR R0 R1 #0 ; r0 -> ( memory address stored in r1 + 0 )
ADD R1 R1 #1 ; increments the memory pointer so that it always points at the next available block
ADD R3 R3 #-1 ; decrement loop counter
BRp INPUTLOOP ; loop until 16 to get full 16 bit binary string
IFZERO;
; Increment zero counter
; Branch to skip input validation, we already know the input was a 0, Ask about jsrr
VALIDATEONE; If it's a zero it would have been skipped already, so just need to make sure that the value is a 1
; Clear R2, to make room for entered number currently in R0
; ADD R0 value to R2 for manipulation
; Subtract 1 from R2 value
; If value is zero then the value was a one, skip mistake prompt
;mistake prompt starts here
; Save R0 value into TEMP label
; Output mistake prompt, let the user they need to enter only binary
;
;
;
;Reload R2 value into R0, which is the io value that was entered
;Skip to here if iunput was one or a zero
RET;
;LABELS
COUNTER .FILL 16;
MEMORYSPACE .blkw 16;
SUB48 .FILL -48;
ENTER .FILL x000A;
TEMP2 .FILL x3155;
MASK .FILL x8000; 1000 on first grouping grabs Most Sig Dig
ORIGINAL .FILL x3050;
ROTATED .FILL x3002;
TEMP .FILL x3100;
AMOUNT .FILL x3113;
NEWLINE .STRINGZ "\n";
PROMPT3 .STRINGZ "Rotated: \n";
MISTAKE .STRINGZ "\nEnter a 1 or 0 character!\n"
ORIGPROMPT .STRINGZ "Enter a 16-bit binary number to rotate: \n";
AMNTPROMPT .STRINGZ "\nEnter a number from 0-16. Single digit must have a '0' in front of it.\n";
.END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment