Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 210 Computer Organization

Similar presentations


Presentation on theme: "Computer Science 210 Computer Organization"— Presentation transcript:

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

2 Numeric Input and Output
# In Python 2.7 yourAge = input("Enter your age: ") print yourAge # In Python 3.2 yourAge = int(input("Enter your age: ")) print(yourAge) In Python 3 there is no raw_input input returns a string, which must be cast to an int print automatically converts its argument to a string

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, INT, and TOSTRING to handle numeric I/O

4 The Numeric I/O Interface
Subroutine Input Parameters Output Parameters PUTS R0 - address of string GETS R1 - address of string R2 – maximum size (Upon RETurn, address starting at R1 will contain our string) INT R2 - integer value TOSTRING PUTS is built into the assembler (we get it for free) Routines in red we must write ourselves We stay away from R0, which is used by the LC-3 trap routines and by our own stack routines

5 Simple Raw Input (basis of GETS)
;; Author: Ken Lambert ;; This program prompts the user for a string, inputs it, and prints it. .ORIG x3000 ;; Register usage: ; R0 = the address of a string for output ; R1 = the address of a string for input ; R6 = the stack pointer ; Main program code JSR INITSTACK ; Initialize the stack LEA R0, PROMPT ; Display the prompt PUTS LEA R1, BUFFER ; Input a string LD R2, MAXSIZE JSR GETS ADD R0, R1, #0 ; Output the string (same as LEA R0, BUFFER) HALT ; Main program data variables PROMPT .STRINGZ "Enter a string: ” ; The input prompt BUFFER .BLKW ; String buffer for I/O (including null) MAXSIZE .FILL ; Maximum number of characters

6 Print the Sum of Two Input Integers
; Main program code JSR INITSTACK ; Initialize the stack LEA R0, PROMPT ; Prompt for an integer PUTS LEA R1, BUFFER ; Input a string LD R2, MAXSIZE JSR GETS JSR INT ; Convert to an integer and save it ST R2, FIRST ST R2, SECOND LD R1, FIRST ; Add the two integers LD R2, SECOND ADD R2, R1, R2 LEA R1, BUFFER ; Convert the sum to a string and print it JSR TOSTRING ADD R0, R1, #0 HALT ; Main program data variables PROMPT .STRINGZ "Enter an integer: " ; The input prompt FIRST .BLKW ; Two integers SECOND .BLKW 1 BUFFER .BLKW ; String buffer for I/O (including null) MAXSIZE .BLKW ; Maximum number of characters

7 Convert a String of Digits to an int
def int(string): number = 0 for digit in string: number = 10 * number + ord(digit) - ord('0') return number The ord function returns a character’s integer ASCII value int(digit) == ord(digit) - ord('0')

8 Code for subroutine INT
; Input parameter: R1 - the address of the string buffer ; Output parameter: R2 - the integer represented by the string ; Register usage: ; R3 = temporary working storage ; R4 = the pointer into the string buffer INT PUSH R7, R1, R3, and R ; Save registers AND R3, R3, #0 ; Clear the sum ST R3, INTSUM ADD R4, R1, #0 ; Set the pointer into the buffer INTLOOP LDR R1, R4, #0 ; Get the next digit from the buffer BRz ENDINT ; Quit when it's null LD R2, ORDZERO ; Convert the digit to an int ADD R1, R1, R2 ST R1, INTDIGIT LD R1, INTSUM ; Multiply the sum by 10 LD R2, INT10 JSR MUL LD R1, INTDIGIT ; Add int value of digit to the sum ADD R3, R3, R1 ADD R4, R4, #1 ; Advance to the next character in the buffer BR INTLOOP ENDINT LD R2, INTSUM ; Set the output parameter POP R4, R3, R1, and R7 ; Restore registers RET ; Subroutine INT data variables INTDIGIT .BLKW ; Holds the integer value of each digit ORDZERO .FILL # ; ASCII for the digit '0' (negated) INT10 .FILL #10 ; Base of 10 INTSUM .FILL #0 ; Holds the running total for the sum Code for subroutine INT

9 Convert an int to a String of Digits
def str(number): if number == 0: return '0' string = '' while number > 0: remainder = number % 10 number //= 10 string = chr(remainder + ord('0')) + string return string The chr function returns the character corresponding to an integer ASCII value str(singleDigitInt) == chr(singleDigitInt + ord('0'))

10 Convert an int to a String of Digits
def str(number): if number == 0: return '0' string = '' while number > 0: remainder = number % 10 number //= 10 string = chr(remainder + ord('0')) + string return string Prepending characters to a string is not easy when you’re representing the string as an array of memory cells

11 Convert an int to a String of Digits
def str(number): if number == 0: return '0' stack = Stack() while number > 0: remainder = number % 10 number //= 10 stack.push(chr(remainder + ord('0'))) string = '' while not stack.isEmpty(): string += stack.pop() return string Use a stack to unload digits to the string in the reverse order

12 Code for subroutine TOSTRING
; Converts the integer in R2 to a string with base address R1 ; Input parameters: R1 = base address of string buffer ; R2 = an integer ; Output parameter: R1 = base address of the string buffer TOSTRING ST R1, STRBASE ; Save return address, parameters, PUSH R7, R4, R3, and R2 ; and temporaries AND R0, R0, #0 ; Push the null character onto the stack JSR PUSH ADD R1, R2, #0 ; Set the initial dividend STRLOOP LD R2, INT10 ; Divide the number by 10 JSR DIV LD R2, CHRZERO ; Convert the remainder and push it ADD R0, R4, R2 ; onto the stack ADD R1, R3, #0 ; Set the dividend to the quotient BRp STRLOOP ; While quotient > 0 LD R1, STRBASE ; Move characters from the stack to the POPLOOP JSR POP ; string buffer, stopping after the null STR R0, R1, #0 ; character is moved ADD R0, R0, #0 BRz ENDPOP ADD R1, R1, #1 BR POPLOOP ENDPOP LD R1, STRBASE ; Restore all registers POP R2, R3, R4, and R7 RET ; Subroutine TOSTRING data variables (also uses INT10 from INT) CHRZERO .FILL #48 ; ASCII for the digit '0' STRBASE .BLKW 1 ; Base address of string buffer for the sum Code for subroutine TOSTRING

13 Managing Dynamic Memory With a System Heap
For Monday Managing Dynamic Memory With a System Heap


Download ppt "Computer Science 210 Computer Organization"

Similar presentations


Ads by Google