Ken D. Nguyen Department of Computer Science Georgia State University

Slides:



Advertisements
Similar presentations
SPARC Architecture & Assembly Language
Advertisements

I/O: SPARC Assembly Department of Computer Science Georgia State University Georgia State University Updated Spring 2014.
1 Computer Architecture MIPS Simulator and Assembly language.
Dale Roberts Basic I/O – scanf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
Computer Organization - Syscalls David Monismith Jan. 28, 2015 Based on notes from Patterson and Hennessy Text and from Dr. Bill Siever.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
CMPE13 Cyrus Bazeghi Chapter 18 I/O in C. CMPE Standard C Library I/O commands are not included as part of the C language. Instead, they are part.
CSC 3210 Computer Organization and Programming Chapter 9 EXTERNAL DATA AND TEXT D.M. Rasanjalee Himali.
Chapter 3 Elements of Assembly Language. 3.1 Assembly Language Statements.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Chapter 18 I/O in C.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 3 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patternson and Hennessy Text.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 3 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
1 Chapter 1: Introduction Appendix A: Binary and Hexadecimal Tutorial Assembly Language for Intel-Based Computers, 3rd edition Kip R. Irvine.
CSC 482/582: Computer Security
C Formatted Input/Output
Data in Memory variables have multiple attributes symbolic name
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
Computer Science 210 Computer Organization
Formatted Input/Output
Chapter 2 - Introduction to C Programming
Introduction to Programming
Chapter 7 Text Input/Output Objectives
Computer Science 210 Computer Organization
TMF1414 Introduction to Programming
File Access (7.5) CSE 2031 Fall July 2018.
Chapter 18 I/O in C.
Getting Started with C.
A First Book of ANSI C Fourth Edition
L7 – Assembler Directives
Chapter 2 - Introduction to C Programming
Formatted Input/Output
Plan of the Day: More on type conversions scanf printf format strings
Input and Output Lecture 4.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Programming in C Input / Output.
CSI 121 Structured Programming Language Lecture 7: Input/Output
Engr 0012 (04-1) LecNotes
توابع ورودي-خروجي.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Chapter 2 - Introduction to C Programming
A First Book of ANSI C Fourth Edition
Chapter 18 I/O in C.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Chapter 2 - Introduction to C Programming
Chapter 18 I/O in C.
Chapter 2 - Introduction to C Programming
Ken D. Nguyen Department of Computer Science Georgia State University
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.2160 ECE Application Programming
Introduction to C Programming
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Getting Started With Coding
Chapter 18 I/O in C.
Presentation transcript:

Ken D. Nguyen Department of Computer Science Georgia State University I/O: SPARC Assembly Ken D. Nguyen Department of Computer Science Georgia State University

I/O on SPARC (File I/O will be covered in chapter 10) I/O thru buffers allocated for each terminal is an tremendous work in assembly. In SPARC, using system call printf and scanf is an easy way to perform I/O These functions perform conversion and extraction automatically. Your data could be resided in any memory block (.data, .bss, or stack – for stack)

printf To use printf function, at least the following must be satisfied: %o0 must be containing the address of a null terminated string (c-string). A null terminated string is a string ended with 1 byte zero. Ex: !in your data section myString: .asciz “hello world!” . . . !and in your code set myString, %o0 ! Get the address of the label call printf !this should print hello world! nop

printf cont. printf allows you to substitute some other data into your output. If your string pointed by %o0 contains %F, you must have your register %o1-%o5 containing the values to be substituted in the same order as the %F (except string), where F is one of the following d or i Decimal signed integer. o Octal integer. x or X Hex integer. U Unsigned integer. c Character. s C-String, i.e. null terminated string. f Double e or E Double. g or G Double. n Number of characters written by this printf.

Output formats o 0 prefix inserted. x or X (Hex) 0x prefix added to non-zero values. e or E Always show the decimal point. f Always show the decimal point. g or G Always show the decimal point trailing zeros not removed.

printf examples !assuming your string declared as follow myString: .asciz “it’s %d dollars” … !In your code set myString, %o0 ! The address of the string call printf !will print it’s 20 dollars mov 20, %o1 !substitutes for the first %d

printf examples cont. !assuming your string declared as follow myString: .asciz “it’s %d dollars and %d cents” … !In your code set myString, %o0 ! The address of the string mov 20, %o1 ! substituted for the first %d call printf ! Will print it’s 20 dollars and 5 cents mov 5, %o2 ! substituted for the second %d

Printing a string To use printf to print a string, the address of the substituted string must be in the correspondent register. Ex: !assuming your strings are in data section as follows mystring: .asciz “Hello %s!” name: .asciz “Michael” . . . ! and your code should be set myString, %o0 ! address of the output set name, %o1 ! Address of the substituted string call printf ! Will print Hello Michael! nop

scanf scanf is used for reading user input scanf requires %o0 containing the address of a c-string containing the input format Subsequence registers (%o1-%o5) contain the address of the memory where the input should be stored %d and %i always give you a 32-bit (word-size) input, so the address must be aligned by word boundary (aligned by 4)

scanf examples !assuming your strings are in data section as follows input: .word 0 !aligned by 4 format: .asciz “%d” . . . ! and your code should be set format, %o0 ! Address of the format set input, %o1 ! Address of the location for the input call scanf ! Reads user input, converts to a nop ! number and stores at the memory ! referenced by input Note: the new line (the ENTER key) the user enters will still be in the buffer, one way to extract that is to read an additional character (or flush it)

scanf examples cont. !assuming your strings are in data section as follows input: .word 0 !aligned by 4 nl: .byte 0 ! A byte to store the input \n format: .asciz “%d%c” . . . ! and your code should be set format, %o0 ! Address of the format set input, %o1 ! Address of the location for the input set nl , %o2 ! location for a character call scanf ! Reads user input, converts to a nop ! number and stores at the memory ! referenced by input This example removes the ENTER key from the buffer

Scanf: string example !assuming your strings are in data section as follows format: .asciz “%s” input: .asciz “ ” !Allocate some space for the input string . . . ! and your code should be set format, %o0 ! Address of the format set input, %o1 ! Address of the location for the input call scanf ! Reads user input up to a blank or ENTER key nop Note: if the user input is longer than the space you allocated, the input will overflow to the subsequence bytes  A very well known buffer overflow problem

An sample program .section ".data“ /* These are variables */ prompt: .asciz "\nPlease enter your name: “ format: .asciz "%s" format2: .asciz "Your name is:%s\n" input: .asciz “ " /* Program starts */ .align 4 .section ".text" .global main main: save %sp, -96, %sp ! save the stack set prompt, %o0 ! point o0 to the prompt call printf ! call printf to print the prompt nop set format, %o0 ! point o0 to the input format string set input, %o1 ! point o1 at the input variable call scanf ! get the input into this variable set format2, %o0 ! point o0 to the output format set input, %o1 ! point o1 to the string to be displayed call printf ! print the string pointed by o1 ret ! return restore ! get out An sample program

More example This program will keep re-prompting for a new input http://www.cs.gsu.edu/knguyen/teaching/slides/csc3210/read.s

Accessing your data in memory Covering in chapter 5