Download presentation
Presentation is loading. Please wait.
Published byΆριστόδημος Δασκαλοπούλου Modified over 5 years ago
1
Ken D. Nguyen Department of Computer Science Georgia State University
I/O: SPARC Assembly Ken D. Nguyen Department of Computer Science Georgia State University
2
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)
3
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
4
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.
5
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.
6
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
7
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
8
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
9
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)
10
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)
11
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
12
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
13
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
14
More example This program will keep re-prompting for a new input
15
Accessing your data in memory
Covering in chapter 5
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.