SlideServe
Menu
Préférences
© SlideServe · v2026

Computer Science 210 Computer Organization

xylia avatar
12 diapositives · 299 Views · Oct 05, 2014

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

Computer Science 210 Computer Organization - slide 1
01 / 12Computer Science 210 Computer Organization
1 / 17

Télécharger la présentation

Computer Science 210 Computer Organization
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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