I/O: SPARC Assembly Department of Computer Science Georgia State University Georgia State University Updated Spring 2014.

Slides:



Advertisements
Similar presentations
5Z032 Processor Design SPIM, a MIPS simulator Henk Corporaal
Advertisements

SPARC Architecture & Assembly Language
SPIM and MIPS programming
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Syscall in MIPS Xinhui Hu Yuan Wang.
MIPS Assembly Language Programming
1 Computer Architecture MIPS Simulator and Assembly language.
Functions Functions and Parameters. History A function call needs to save the registers in use The called function will use the registers The registers.
C Programming. printf int printf ( const char * format,... ); printf ("Characters: %c \n", 'a'); printf ("Decimals: %d %f\n", 1977, 3.14); specifierOutputExample.
Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2.
Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
1 CS 161 Introduction to Programming and Problem Solving Chapter 9 C++ Program Components Herbert G. Mayer, PSU Status 10/20/2014.
Dale Roberts Basic I/O – scanf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
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.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
 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.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
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.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
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.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
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.
More on Assembly 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Lecture 161 Lets examine a SPIM program in detail. io.asm This program is a simple routine which demonstrates input/output using assembly code. SPIM.
Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Chapter 8 – Machine Instructions These are lecture notes to accompany the book.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patternson and Hennessy Text.
The SPIM Trap Handler Syscall Trap handler services String operations File operations Memory allocation Central Connecticut State University, MIPS Tutorial.
1 CSC103: Introduction to Computer and Programming Lecture No 28.
More on Assembly 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
Files A collection of related data treated as a unit. Two types Text
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Preliminary to Assembly Language Programming CE 140 A1/A2 28 June 2003.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Chapter 18 I/O in C Original slides from Gregory Byrd, North Carolina State University Modified slides by C. Wilcox, Y. Malaiya Colorado State University.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 3 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
CSC 482/582: Computer Security
C Formatted Input/Output
Data in Memory variables have multiple attributes symbolic name
Computer Science 210 Computer Organization
Formatted Input/Output
Standard Input - Output
The Stack Chapter 5 Lecture notes for SPARC Architecture, Assembly Language Programming and C, Richard P. Paul by Anu G. Bourgeois.
Introduction to Programming
Chapter 7 Text Input/Output Objectives
TMF1414 Introduction to Programming
Debugging with gdb gdb is the GNU debugger on our CS machines.
Chapter 18 I/O in C.
Formatted Input/Output
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.
Week 5 Computers are like Old Testament gods; lots of rules and no mercy. Joseph Campbell.
Ken D. Nguyen Department of Computer Science Georgia State University
Engr 0012 (04-1) LecNotes
Introduction to CS Your First C Programs
Computer Science 210 Computer Organization
A First Book of ANSI C Fourth Edition
Debugging with printf you can call printf from an ARM assembly language program some of the details will be explained later, but for now use this skeleton.
Ken D. Nguyen Department of Computer Science Georgia State University
Introduction to C Programming
Presentation transcript:

I/O: SPARC Assembly Department of Computer Science Georgia State University Georgia State University Updated Spring 2014

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.  UUnsigned 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 ► o0 prefix inserted. ► x or X(Hex) 0x prefix added to non-zero values. ► e or EAlways show the decimal point. ► fAlways show the decimal point. ► g or GAlways 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

!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 ld [%o1], %l1! Load word size input value into register %l1 ldub[%o2], %l2! Load byte size input value into register %l2 Note: After the scanf function, %o1 and %o2 are pointing to the memory locations holding the input values read in. The ld and ldub commands are then accessing those memory locations to read the input values

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

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 nop 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 nop nop ret ! return restore ! get out

Sample program (program skeleton highlighted).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 nop 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 nop ret ! return restore ! get out

More example This program will keep re-prompting for a new input es/csc3210/read.s

Accessing your data in memory ► Covering in chapter 5