120 likes | 270 Views
Computer Science 210 Computer Organization. Type Conversions and Numeric I/O. Numeric Input and Output. length = int ( input ( "Enter the length: " )) width = int ( input ( "Enter the width: " ) ) print ( " The area is " , length * width). In Python
E N D
Computer Science 210Computer Organization Type Conversions and Numeric I/O
Numeric Input and Output length =int(input("Enter the length: ")) width = int(input("Enter the width: ")) print("The area is", length * width) • In Python • input returns a string from the keyboard • int converts a string of digits to an integer • print automatically converts its arguments to strings
Input and Output in LC-3 • In LC-3, all I/O is character-based • Strings may be output (trap service routine PUTS) and input (our own subroutine) • Add subroutines GETS, TOINT, and TOSTRING to handle numeric I/O
The LC3 Numeric I/O Interface We stay away from R0, which is used by the LC-3 trap routines and by our own stack routines (next week)
Code for Main Program ;; Author: Ken Lambert ;; This program prompts the user for a length and a width and outputs ; the area. ;; Pseudocode design: ; length = int(input("Enter the length: ")) ; width = int(input("Enter the width: ")) ; print("The area is", length * width) .ORIG x3000
;; Main program register usage: ; R0 = base address for outputs ; R1 = base address for inputs ; R2 = size of arrays ; R3 = area ; Main program code LEA R0, LPROMPT ; print("Enter the length: ") PUTS LEA R1, LENGTH ; length = input() LD R2, SIZE JSR GETS LEA R0, WPROMPT ; print("Enter the width: ") PUTS LEA R1, WIDTH ; width = input() JSR GETS JSR TOINT ; width = int(width) ADD R3, R2, #0 LEA R1, LENGTH ; length = int(length) JSR TOINT ADD R1, R2, #0 ; area = length * width ADD R2, R3, #0 JSR MUL ADD R2, R3, #0 ; area = str(area) LEA R1, AREA JSR TOSRTRING LEA R0, AREAMESSAGE ; print("The area is ") PUTS ADD R0, R1, #0 ; print(area) PUTS HALT ; Data for main program LPROMPT .STRINGz "Enter the length: " WPROMPT .STRINGz "Enter the width: " AREAMESSAGE .STRINGz "The area is " LENGTH .BLKW 6 WIDTH .BLKW 6 SIZE .FILL #6
A Better Design • Keep the subroutines GETS, TOINT, and TOSTRING • Add two new routines • GETI, which displays the prompt, takes the input string from the user, coverts it to an integer, and returns the integer • PUTI, which takes the integer from the program, converts it to a string, and displays the string
;; Main program register usage: ; R0 = base address for outputs ; R1 = base address for inputs ; R2 = size of arrays ; R3 = area ; Main program code LEA R0, LPROMPT ; print("Enter the length: ") PUTS LEA R1, LENGTH ; length = input() LD R2, SIZE JSR GETS LEA R0, WPROMPT ; print("Enter the width: ") PUTS LEA R1, WIDTH ; width = input() JSR GETS JSR TOINT ; width = int(width) ADD R3, R2, #0 LEA R1, LENGTH ; length = int(length) JSR TOINT ADD R1, R2, #0 ; area = length * width ADD R2, R3, #0 JSR MUL LEA R1, AREA ; area = str(area) ADD R2, R3, #0 JSR TOSRTRING LEA R0, AREAMESSAGE ; print("The area is ") PUTS ADD R0, R1, #0 ; print(area) PUTS HALT ; Data for main program LPROMPT .STRINGz "Enter the length: " WPROMPT .STRINGz "Enter the width: " AREAMESSAGE .STRINGz "The area is " LENGTH .BLKW 6 WIDTH .BLKW 6 SIZE .FILL #6
;; Main program register usage: ; R0 = base address for outputs ; R1 = base address for inputs ; R2 = size of arrays ; R3 = area ; Main program code LEA R0, LPROMPT ; length = int(input("Enter the length: ")) JSR GETI ADD R2, R1, #0 LEA R0, WPROMPT ; width = int(input("Enter the width: ")) JSR GETI JSR MUL ; area = length * width LEA R0, AREAMESSAGE ; print("The area is ", area) PUTS ADD R1, R3, #0 JSR PUTI HALT ; Data for main program LPROMPT .STRINGz "Enter the length: " WPROMPT .STRINGz "Enter the width: " AREAMESSAGE .STRINGz "The area is "
Convert a String of Digits to an int defourInt(aString): number = 0 for digit inaString: number = 10 * number + ord(digit) - ord('0') return number ord(digit) - ord('0') == int(digit) ord returns the ASCII value of its argument
Convert an int to a String of Digits defstr(number): if number == 0: return'0' string = '' while number > 0: remainder = number % 10 string = chr(remainder) + string number //= 10 return string Prepending characters to a string is not easy, when you’re representing the string as an array of memory cells
Convert an int to a String of Digits defstr(number): if number == 0: return'0' stack = Stack() while number > 0: remainder = number % 10 stack.push(chr(remainder)) number //= 10 string = '' while not stack.isEmpty(): string += stack.pop() return string Use a stack to unload digits to the string in the reverse order