1 / 12

Computer Science 210 Computer Organization

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

xylia
Download Presentation

Computer Science 210 Computer Organization

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science 210Computer Organization Type Conversions and Numeric I/O

  2. 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

  3. 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

  4. 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)

  5. 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

  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 ; 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

  7. 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

  8. ;; 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

  9. ;; 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 "

  10. 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

  11. 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

  12. 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

More Related