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

I/O: SPARC Assembly

Embed Size (px)

DESCRIPTION

I/O: SPARC Assembly. Department of Computer Science 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. - PowerPoint PPT Presentation

Citation preview

Page 1: I/O: SPARC Assembly

I/O: SPARC Assembly I/O: SPARC Assembly

Department of Computer Department of Computer ScienceScience

Georgia State University Georgia State University

Updated Spring 2014

Page 2: I/O: SPARC Assembly

I/O on SPARCI/O on SPARC

(File I/O will be covered in chapter 10)(File I/O will be covered in chapter 10)► I/O thru buffers allocated for each terminal I/O thru buffers allocated for each terminal

is an tremendous work in assembly.is an tremendous work in assembly.► In SPARC, using system call In SPARC, using system call printfprintf and and

scanfscanf is an easy way to perform I/O is an easy way to perform I/O►These functions perform conversion and These functions perform conversion and

extraction automatically.extraction automatically.►Your data could be resided in any memory Your data could be resided in any memory

block (.data, .bss, or stack – for stack)block (.data, .bss, or stack – for stack)

Page 3: I/O: SPARC Assembly

printfprintf► To use printf function, at least the To use printf function, at least the

following must be satisfied:following must be satisfied: %o0 must be containing the address of a null %o0 must be containing the address of a null

terminated string (c-string). A null terminated terminated string (c-string). A null terminated string is a string ended with 1 byte zero.string is a string ended with 1 byte zero.

Ex: Ex: !in your data section!in your data sectionmyString: .asciz “hello world!”myString: .asciz “hello world!”. . .. . .!and in your code!and in your codeset myString, %o0set myString, %o0 ! Get the address of the label! Get the address of the labelcall printfcall printf !this should print !this should print hello hello

world!world!nopnop

Page 4: I/O: SPARC Assembly

printf cont.printf cont.► printfprintf allows you to substitute some other data into your allows you to substitute some other data into your

output.output.► If your string pointed by %o0 contains %If your string pointed by %o0 contains %F, F, you must have you must have

your register %o1-%o5 containing the values to be substituted your register %o1-%o5 containing the values to be substituted in the same order as the in the same order as the %F%F (except string), where (except string), where FF is one of is one of the following the following dd or or ii Decimal signed integer. Decimal signed integer. oo Octal integer.Octal integer. xx or or XX Hex integer.Hex integer. UU Unsigned integer.Unsigned integer. cc Character. Character. ss C-String, i.e. null terminated string. C-String, i.e. null terminated string. ff Double Double ee or or EE Double.Double. gg or or GG Double. Double. nn Number of characters written by this Number of characters written by this printfprintf..

Page 5: I/O: SPARC Assembly

Output formatsOutput formats

►oo 0 prefix inserted. 0 prefix inserted. ►xx or or XX (Hex) 0x prefix added to non-(Hex) 0x prefix added to non-

zero values. zero values. ►ee or or EE Always show the decimal Always show the decimal

point. point. ►ff Always show the decimal point. Always show the decimal point. ►gg or or GG Always show the decimal Always show the decimal

point trailing zeros not removed. point trailing zeros not removed.

Page 6: I/O: SPARC Assembly

printf examples printf examples

!assuming your string declared as follow!assuming your string declared as follow

myString: .asciz “it’s myString: .asciz “it’s %d%d dollars” dollars”

……

!In your code!In your code

set myString, %o0set myString, %o0 ! The address of the string! The address of the string

call printfcall printf !will print !will print it’s 20 dollarsit’s 20 dollars

mov 20, %o1mov 20, %o1 !substitutes for the first !substitutes for the first %d%d

Page 7: I/O: SPARC Assembly

printf examples cont.printf examples cont.

!assuming your string declared as follow!assuming your string declared as followmyString: .asciz “it’s myString: .asciz “it’s %d%d dollars and dollars and %d%d cents” cents”……!In your code!In your codeset myString, %o0set myString, %o0 ! The address of the string! The address of the stringmov 20, %o1mov 20, %o1 ! substituted for the first %d! substituted for the first %dcall printfcall printf ! Will print ! Will print it’s 20 dollars and 5 it’s 20 dollars and 5

centscentsmov 5, %o2mov 5, %o2 ! substituted for the second %d! substituted for the second %d

Page 8: I/O: SPARC Assembly

Printing a stringPrinting a string

► To use To use printfprintf to print a string, the address of the to print a string, the address of the substituted string must be in the correspondent substituted string must be in the correspondent register.register.

Ex: Ex: !assuming your strings are in data section as follows!assuming your strings are in data section as followsmystring: .asciz “Hello mystring: .asciz “Hello %s%s!”!”name: .asciz “Michael”name: .asciz “Michael”. . . . . . ! and your code should be! and your code should beset myString, %o0set myString, %o0 ! address of the output! address of the outputset name, %o1set name, %o1 ! Address of the substituted string ! Address of the substituted string call printfcall printf ! Will print ! Will print Hello Michael!Hello Michael!nopnop

Page 9: I/O: SPARC Assembly

scanfscanf

►scanf is used for reading user inputscanf is used for reading user input►scanf requires %o0 containing the scanf requires %o0 containing the

address of a c-string containing the input address of a c-string containing the input formatformat

►Subsequence registers (%o1-%o5) Subsequence registers (%o1-%o5) contain contain the address of the memorythe address of the memory where the input should be storedwhere the input should be stored

►%d and %i always give you a 32-bit %d and %i always give you a 32-bit (word-size) input, so the address must be (word-size) input, so the address must be aligned by word boundary (aligned by 4)aligned by word boundary (aligned by 4)

Page 10: I/O: SPARC Assembly

scanf examplesscanf examples

!assuming your strings are in data section as follows!assuming your strings are in data section as followsinput: .word 0input: .word 0 !aligned by 4 !aligned by 4 format: .asciz “%d”format: .asciz “%d”. . . . . . ! and your code should be! and your code should beset format, %o0set format, %o0 ! Address of the format! Address of the formatset input, %o1set input, %o1 ! Address of the location for the ! Address of the location for the

input input call scanfcall scanf ! Reads user input, converts to a ! Reads user input, converts to a nopnop ! number and stores at the ! number and stores at the

memory memory ! referenced by ! referenced by inputinput

► Note: the new line (the ENTER key) the user enters will still be 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 in the buffer, one way to extract that is to read an additional character (or flush it) character (or flush it)

Page 11: I/O: SPARC Assembly

scanf examples cont.scanf examples cont.

!assuming your strings are in data section as follows!assuming your strings are in data section as followsinput: .word 0input: .word 0 !aligned by 4 !aligned by 4 nl: .byte 0nl: .byte 0 ! A byte to store the input \n! A byte to store the input \nformat: .asciz “%d%c”format: .asciz “%d%c”. . . . . . ! and your code should be! and your code should beset format, %o0set format, %o0 ! Address of the format! Address of the formatset input, %o1set input, %o1 ! Address of the location for the input! Address of the location for the inputset nl , %o2set nl , %o2 ! location for a character ! location for a character call scanfcall scanf ! Reads user input, converts to a ! Reads user input, converts to a nopnop ! number and stores at the memory ! number and stores at the memory

! referenced by input! referenced by input

This example removes the ENTER key from the bufferThis example removes the ENTER key from the buffer

Page 12: I/O: SPARC Assembly

!assuming your strings are in data section as follows!assuming your strings are in data section as followsinput: .word 0input: .word 0 !aligned by 4 !aligned by 4 nl: .byte 0nl: .byte 0 ! A byte to store the input \n! A byte to store the input \nformat: .asciz “%d%c”format: .asciz “%d%c”. . . . . . ! and your code should be! and your code should beset format, %o0set format, %o0 ! Address of the format! Address of the formatset input, %o1set input, %o1 ! Address of the location for the input! Address of the location for the inputset nl , %o2set nl , %o2 ! location for a character ! location for a character call scanfcall scanf ! Reads user input, converts to a ! Reads user input, converts to a nopnop ! number and stores at the memory ! number and stores at the memory

! referenced by input! referenced by input

ld [%o1], %l1ld [%o1], %l1 ! Load word size input value into register ! Load word size input value into register %l1%l1

ldub[%o2], %l2ldub[%o2], %l2 ! Load byte size input value into register %l2! Load byte size input value into register %l2

Note: After the scanf function, %o1 and %o2 are pointing to the Note: After the scanf function, %o1 and %o2 are pointing to the memory locations holding the input values read in. The ld and memory locations holding the input values read in. The ld and ldub commands are then accessing those memory locations to ldub commands are then accessing those memory locations to read the input valuesread the input values

Page 13: I/O: SPARC Assembly

Scanf: string exampleScanf: string example!assuming your strings are in data section as follows!assuming your strings are in data section as follows

format: .asciz “%s”format: .asciz “%s”input: .asciz “ ” !Allocate some space for the input stringinput: .asciz “ ” !Allocate some space for the input string. . . . . .

! and your code should be! and your code should be

set format, %o0set format, %o0 ! Address of the format! Address of the formatset input, %o1set input, %o1 ! Address of the location for the input! Address of the location for the inputcall scanf call scanf ! Reads user input up to a ! Reads user input up to a blankblank or ENTER key or ENTER key nopnop

Note: if the user input is longer than the space you allocated, the input Note: if the user input is longer than the space you allocated, the input will overflow to the subsequence bytes will overflow to the subsequence bytes A very well known buffer A very well known buffer overflow problemoverflow problem

Page 14: I/O: SPARC Assembly

Sample Sample programprogram

.section ".data“.section ".data“ /* These are variables */ /* These are variables */ prompt: .asciz "\nPlease enter your name: “prompt: .asciz "\nPlease enter your name: “format: .asciz "%s" format: .asciz "%s" format2: .asciz "Your name is:%s\n" format2: .asciz "Your name is:%s\n" input: .asciz “ " input: .asciz “ "

/* Program starts */ /* Program starts */ .align 4 .align 4 .section ".text" .section ".text" .global main .global main main: main: save %sp, -96, %spsave %sp, -96, %sp ! save the stack ! save the stack

set prompt, %o0 set prompt, %o0 ! point o0 to the prompt ! point o0 to the prompt call printf call printf ! call printf to print the prompt ! call printf to print the prompt nop nop

set format, %o0set format, %o0 ! point o0 to the input format string ! point o0 to the input format stringset input, %o1 set input, %o1 ! point o1 at the input variable ! point o1 at the input variable call scanfcall scanf ! get the input into this variable ! get the input into this variable nop nop

set format2, %o0set format2, %o0 ! point o0 to the output format ! point o0 to the output formatset input, %o1 set input, %o1 ! point o1 to the string to be displayed! point o1 to the string to be displayedcall printf call printf ! print the string pointed by o1! print the string pointed by o1 nopnop ret ret ! return ! return restore restore ! get out ! get out

Page 15: I/O: SPARC Assembly

Sample Sample programprogram

(program skeleton (program skeleton highlighted)highlighted)

.section ".data“.section ".data“ /* These are variables */ /* These are variables */ prompt: .asciz "\nPlease enter your name: “prompt: .asciz "\nPlease enter your name: “format: .asciz "%s" format: .asciz "%s" format2: .asciz "Your name is:%s\n" format2: .asciz "Your name is:%s\n" input: .asciz “ " input: .asciz “ "

/* Program starts */ /* Program starts */ .align 4 .align 4 .section ".text" .section ".text" .global main .global main main: main: save %sp, -96, %spsave %sp, -96, %sp ! save the stack ! save the stack

set prompt, %o0 set prompt, %o0 ! point o0 to the prompt ! point o0 to the prompt call printf call printf ! call printf to print the prompt ! call printf to print the prompt nop nop

set format, %o0set format, %o0 ! point o0 to the input format string ! point o0 to the input format stringset input, %o1 set input, %o1 ! point o1 at the input variable ! point o1 at the input variable call scanfcall scanf ! get the input into this variable ! get the input into this variable nop nop

set format2, %o0set format2, %o0 ! point o0 to the output format ! point o0 to the output formatset input, %o1 set input, %o1 ! point o1 to the string to be displayed ! point o1 to the string to be displayedcall printf call printf ! print the string pointed by o1 ! print the string pointed by o1nopnop ret ret ! return ! return restore restore ! get out ! get out

Page 16: I/O: SPARC Assembly

More exampleMore example

This program will keep re-prompting for This program will keep re-prompting for a new inputa new input

http://www.cs.gsu.edu/knguyen/http://www.cs.gsu.edu/knguyen/teaching/slides/csc3210/read.steaching/slides/csc3210/read.s

Page 17: I/O: SPARC Assembly

Accessing your data in Accessing your data in memorymemory

►Covering in chapter 5Covering in chapter 5