Computer Science 210 Computer Organization

Slides:



Advertisements
Similar presentations
Computer Science 210 Computer Organization Strings, I/O, and Trap Service Routines.
Advertisements

Chapter 7 Introduction to LC-3 Assembly Language Assembly Language Assembly Process Using the Editor & Simulator for Assembly Language Programming.
Chapter 10 And, Finally.... Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display A Final Collection of ISA-related.
1 System Calls (TRAPS) and Subroutines Patt and Patel Ch. 9.
I/O: SPARC Assembly Department of Computer Science Georgia State University Georgia State University Updated Spring 2014.
Overview I/O – memory mapped programmed / interrupt driven Traps mechanism & RET Subroutines & JSR & JSRR & RET Interrupt mechanism & RTI.
Chapter 9 Overview Traps mechanism & RET Subroutines & JSR & JSRR & RET Interrupt mechanism & RTI.
Chapter 6 Programming in Machine Language The LC-3 Simulator
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
Choice for the rest of the semester New Plan –assembler and machine language –Operating systems Process scheduling Memory management File system Optimization.
Chapter 9 Trap Routines & RET Subroutines (or Functions) & JSR & JSRR & RET.
Chapters 4 & 5: LC-3 Computer Architecture Machine Instructions Assembly language Programming in Machine and Assembly Language.
Chapter 10 And, Finally... The Stack Stack: An Abstract Data Type An important abstraction that you will encounter in many applications. We will.
Computer Science 111 Fundamentals of Programming I Number Systems.
Computer Science 210 Computer Organization Introduction to Subroutines.
Chapter 9 Chapter 9 Subroutines and TRAPs l Privileged Instructions l TRAP Routines l Subroutines.
Computer Science 210 Computer Organization
Chapter 10 And, Finally... The Stack
Computer Science 210 Computer Organization
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Computer Science 210 Computer Organization
Chapter 9 TRAP Routines and Subroutines
Chapter 7 & 9.2 Assembly Language
Computer Science 210 Computer Organization
Chapter 17 Recursion.
Computer Science 210 Computer Organization
HKN ECE 220: Fall 2017 Midterm 1 AJ Schroeder, Evan Lissoos, Utsav Kawrani 23rd September, 2017.
Computer Science 210 Computer Organization
COSC121: Computer Systems: LC3 Traps and Subroutines
Computer Science 210 Computer Organization
CS-401 Computer Architecture & Assembly Language Programming
Computer Science 210 Computer Organization
Chapter 9 TRAP Routines and Subroutines
Chapter 10 The Stack.
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Chapter 7 LC-2 Assembly Language.
Ken D. Nguyen Department of Computer Science Georgia State University
Computer Science 210 Computer Organization
Chapter 9 TRAP Routines and Subroutines
Computer Science 210 Computer Organization
Popping Items Off a Stack Lesson xx
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
HKN ECE 220: Spring 2018 Midterm 1
Passing Parameters Data passed to a subroutine is called a parameter.
Computer Science 210 Computer Organization
Chapter 9 TRAP Routines and Subroutines
Computer Science 210 Computer Organization
TRAP Routines Subroutines Privileged Instructions
Chapter 9 TRAP Routines and Subroutines
Chapter 7 LC-2 Assembly Language.
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Chapter 17 Recursion.
Chapter 9 TRAP Routines and Subroutines
Chapter 7 Assembly Language
Computer Science 210 Computer Organization
TRAP Routines Privileged Instructions Subroutines
Chapter 9 TRAP Routines and Subroutines
Computer Science 210 Computer Organization
HKN ECE 220: Fall 2018 Midterm 1 Andrew Fortunat, Jeff Chang, Srijan Chakraborty, Kanad Sarkar February 16, 2019.
Ken D. Nguyen Department of Computer Science Georgia State University
Chapter 9 TRAP Routines and Subroutines
Chapter 9 TRAP Routines and Subroutines
Chapter 17 Recursion.
Midterm 2 Review Chapters 4-16 LC-3
Chapter 9 TRAP Routines and Subroutines
Implementing Functions: Overview
Presentation transcript:

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

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

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

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

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 6 ; String buffer for I/O (including null) MAXSIZE .FILL 5 ; Maximum number of characters

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 1 ; Two integers SECOND .BLKW 1 BUFFER .BLKW 6 ; String buffer for I/O (including null) MAXSIZE .BLKW 5 ; Maximum number of characters

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')

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 R4 ; 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 1 ; Holds the integer value of each digit ORDZERO .FILL #-48 ; 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

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'))

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

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

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

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