; Program to test the ReadInt subroutine ; Written By: Deepak Kumar ; Date: 4/9/2024 ; .ORIG x3000 TEST JSR ReadInt HALT ; ; ReadInt: Reads an integer typed at keyboard and places it in R0 ReadInt ST R7, RIR7 ; First read an entire string (until LF) and store starting addr of string in R0 JSR ReadString PUTS ; Then, convert string to a number and leave it in R0 JSR StrToInt LD R7, RIR7 RET RIR7 .BLKW 1 ; ; ReadString ; Reads a string into R0 (i.e. anything that is typed until LF) ReadString ST R1, RSR1 ST R7, RSR7 LEA R1, String ; Load address where string is to be stored GetChar IN ; input a char into R0 OUT STR R0, R1, #0 ; Store char in String ADD R1, R1, #1 ; Next char location ADD R0, R0, -10 ; Check to see if char was LF BRnp GetChar ; Got LF, now null terminate it ADD R1, R1, #-1 ; move back over LF STR R0, R1, #0 ; Store 0 at end of string LEA R0, String ; R0 = Address of string read in LD R1, RSR1 LD R7, RSR7 RET ; Data Needed for ReadString RSR1 .BLKW 1 RSR7 .BLKW 1 String .BLKW 80 ; max length of input string ; ; StrToInt ; Convert a string whose starting address is in R0 to a number in R0 StrToInt ST R1, StR1 ST R2, StR2 ST R3, StR3 St R7, StR7 AND R2, R2, #0 ; R2 = 0 (where number is accumulated) Loop LDR R1, R0, #0 ; Load char in R1 BRz STRDone ; Found null, we are done LD R3, ASCII ; Load x-30 ADD R1, R1, R3 ; Convert to a digit JSR Mult10 ; R2 = R2 * 10 ADD R2, R2, R1 ; R2 = R2 + R1 ADD R0, R0, #1 ; Next char location BR LOOP STRDone ADD R0, R2, #0 ; R0 = R2 (the result should be in R0) LD R1, StR1 LD R2, StR2 LD R3, StR3 LD R7, StR7 RET ASCII .FILL #-48 StR1 .BLKW 1 StR2 .BLKW 1 StR3 .BLKW 1 StR7 .BLKW 1 ; Mult10 ; R2 has a number in it, multiply by 10 and leave it in R2 ; This is very quick and dirty! ; Mult10 ST R3, MuR3 ; Save R3 & R4 ST R4, MuR4 AND R4, R4, #0 ; R4 <- 0 (result) AND R3, R3, #0 ; R3 <- 10 ADD R3, R3, #10 AGAIN BRz MultDone ADD R4, R4, R2 ; R4 < R4 + R2 ADD R3, R3, #-1 ; R3 <- R3 - 1 BR AGAIN MultDone ADD R2, R4, #0 ; R2 <- R4 (return value) LD R3, MuR3 ; Restore R3 & R4 LD R4, MuR4 RET MuR3 .BLKW 1 MuR4 .BLKW 1 MuR7 .BLKW 1 .END