MIPS Lab 2j - Week 7 tutorial work for unit: Computer Systems Fundamentals (CSF) PDF

Title MIPS Lab 2j - Week 7 tutorial work for unit: Computer Systems Fundamentals (CSF)
Course Computer science
Institution Manchester Metropolitan University
Pages 20
File Size 958.7 KB
File Type PDF
Total Downloads 64
Total Views 138

Summary

Week 7 tutorial work for unit: Computer Systems Fundamentals (CSF)...


Description

MIPS: System Calls

MIPS System Call Instructions for I/O Aim of this lab :

The aim of this workshop is to become familiar with making system calls ( syscall ) to the operating system to provide I/O services.

MIPS SYSCALL AND MARS       

The MIPS instruction set has a software initiated interrupt “ syscall “ instruction The syscall instruction itself takes no parameters (unlike add, sub, and etc.) The OS provides many (syscall) services Value in register $v0 determines which service is invoked. Arguments are mostly placed in register $a0 On syscall, control is temporally switches to OS to perform the service before program resumes Syscalls are expensive in time

Table of commonly used No. print integer print float print double print string

1 2 3 4

read integer read float read double exit

5 6 7 10

Syscall services Arguments

Result

$a0 = integer to print $f12 = float to print $f12 = double to print $a0 = address of null-terminated string

to print $v0 contains integer read $f0 contains float read $f0 contains double read

(terminate execution)

open file time

13 30

$a0,$a1, $a2 – all do different things

print integer

34

$a0 = integer to print

random int

41

$a0 = id. of pseudorandom number generator (any integer).

(system time)

$v0 contains file descriptor (negative if error) $a0 = low order 32 bits of system time $a1 = high order 32 bits of system time Displayed value is 8 hexadecimal digits, left- padding with zeroes if necessary.

in hexadecimal

$a0 contains the next pseudorandom, uniformly distributed integer value from this random number generator's sequence

Syscall : The process …… Step 1 Step 2 Step 3 Step 4

Load the service number in register $v0. Load argument values, if any, in $a0, $a1, $a2, or $f12 as specified. Issue the SYSCALL instruction. Retrieve return values, if any, from result registers as specified.

6G4Z1102:Computer Systems Fundamentals, Dr. Bob Cherry, L Travis

Please turn over

MIPS: System Calls

Assembler pseudo instructions The MIPS instruction set is very small, so to do more complicated tasks we need to employ assembler macros called pseudoinstructions. Below are standard MIPS instructions that are implemented as pseudoinstructions: Abs ble neg bge la sge bgt blt not li move sgt see https://en.wikibooks.org/wiki/MIPS_Assembly/Pseudoinstructions So, Pseudo Instructions are not part of the processor instruction set. They are part of the Assembler (additional) pseudocode instructions. How they are interpreted and actioned is down to the assembler being used (Mars here). Two pseudoinstructions you will use in this lab are li and la.

Load Immediate

li $v0, 55

=

addi $v0, $zero, 55

In more detail : li stands for Load Immediate and is a convenient way of loading an immediate number up to 32 bits in size. Instructions like addi and ori can only encode 16-bit immediates, so the assembler may translate li into multiple instructions.

For example, li $t0,0x12345678 might become: lui $at, 0x1234 ori $t0, $at, 0x5678

So it's just a way to save you from writing those two instructions, and instead letting the assembler work that out for you. There's really no reason why e.g. li $t0, Message wouldn't be supported, since labels are also immediates, but some assemblers might not accept labels for li.

Load Address

la stands for Load Address. It can be used to load integer constants just

like li, e.g. la $t0,0x1234678. But it also works with labels: la $t0, Message # t0 = address of Message. Some assemblers may also allow you to do things like la $t0, 8($t1) # t0 = t1 + 8. When you'd use li and when you'd use la depends on the context. If the value you're loading is going to be used as an address you would typically use la to load it, and otherwise you'd typically use li. Since they are partially interchangeable it's really up to you, but other people might find your code strange-looking if you use la all the time to load integer constants. Ref. Stackoverflow.com

Please turn over

6G4Z1102:Computer Systems Fundamentals, Dr. Bob Cherry, L Travis

MIPS: System Calls

Printing out a NUMBER to the console terminal window Example :

How to



Print an integer



Display the value stored in $t0 on the console

#Put 1 in $v0, using service 1 addi $v0, $zero, 1 #Put 64 in $a0 , Value to print out addi $a0, $zero, 64 #Pass Control to OS syscall

Example :

How to

# Service 1 is “print integer” li $v0, 1 # Load desired value into argument register $a0 add $a0, $t0, $zero syscall

Exercise 1 Modify the MIPS program below to print the integer number 7542 to the console terminal screen. # Put 1 in $v0, using SYSCALL service 1 # ( print numeral (NUMBER) # out to terminal screen ) addi $v0, $zero, 1 # Put 64 in $a0 , Value to print out addi $a0, $zero, 64

OS service call & argument numbers All service calls are set up using service call number loaded in register

$v0

CALL ARGUMENT RESULT DESCRIPTION ($v0)

1 11 5 12 4

$a0 $a0

$a0

Print Print $v0 Read $v0 Read Print

OUT Numeral (s) OUT ‘Character (s)’ IN Integer value IN Character(s) OUT null terminated string

( Print output until null value found )

# Pass Control to Operating System syscall

Register purposes $t = TEMP $v = return VALUE

6G4Z1102:Computer Systems Fundamentals, Dr. Bob Cherry, L Travis

$s = STORAGE $a = ARGUMENT

( Add value to OS )

MIPS: System Calls

Reading in a NUMBER from the console keyboard Example :

How to



Read in a single integer

#Put 5 into $v0, using service 5 - Read an integer addi $v0, $zero, 5 #pass control to OS Syscall # type in “88” followed by pressing the keyboard “enter” key # 88 that was entered is now in $v0

Exercise 2 Write a MIPS program to

read a number from the user (console keyboard) and echo the number back to the console terminal screen.

(Hint – use system call number (5)

i.e. move 5 into $v0 )

Exercise 3 Write a MIPS program that

collects ONE number from the user via the keyboard, adds 101 to the number and prints the result to the console terminal screen.

6G4Z1102:Computer Systems Fundamentals, Dr. Bob Cherry, L Travis

MIPS: System Calls

Printing out an ASCII CHARACTERs to the console terminal window Exercise 4 Write a MIPS program that

collects TWO numbers from the user via the keyboard, adds 202 to each number and prints the resulting numbers to the console terminal screen. The program should read both numbers first then display the result with both numbers separated by a comma (“,”) symbol. (Hint – to print a comma “,” between each of the output numbers : Use “addi, $a0, $zero,‘’,’ ” or “li, $a0, ‘’,’ “ and use system call number (11) to do this) (do not cut and paste this instruction from this lab sheet.)



Use the “ ” symbol on the “

@” key of a UK Windows keyboard to type “ $a0,‘’,’ “.

6G4Z1102:Computer Systems Fundamentals, Dr. Bob Cherry, L Travis

MIPS: System Calls

Printing out ASCII character STRINGS to the console terminal window and formatting the display using the “line break” When using the MARS assembler, Null Terminated Strings are placed before your assembly code. The syntax is to begin with .data and indicate the end of the .data segment with .text - your assembler code (text characters) continues from this point. The Null Terminated String syntax is to label the string (str1), define this as ascii characters using .ascii followed by the character string you want to display on the console terminal screen …. ("Battle not with monsters, \n lest ye become a monster" in Example 5) The final line of the Null Terminated string should begin with .asciiz If the character string only uses one line, then only .asciiz should be used. (see the example for Exercise 5).  Also note that the control character \n creates a line break.

Example :

How to



.data message: .asciiz "Hello World"

Print out a character string This is data and not instructions This is the message we wish to store in memory

.text

The data is an ascii string with a null terminator ( asciiz rather than ascii ) la $a0, message addi $v0, $zero, 4 syscall

Example :

How To

This is the label to use as an alias for the memory address for where the text string is



Print an ASCII character string

.data # Notice that “\n” creates a LINE BREAK joke: .asciiz "Knock Knock\nwho is there?" .text la $a0, joke addi $v0, $zero, 4 syscall

6G4Z1102:Computer Systems Fundamentals, Dr. Bob Cherry, L Travis

MIPS: System Calls

Exercise 5 Modify the MIPS program below to display the following 80’s song lyric. "Never gonna give you up, never gonna let you down”

# Notice once again, that “\n” creates a LINE BREAK

.data str1: .asciiz "Battle not with monsters, \n lest ye become a monster" .text # using pseudo instruction “Load Address” to load the character string la $a0, str1 # the System Call service number for printing a string is 4 addi $v0, $zero, 4 syscall

Exercise 6a The control char \n creates a line break. .data joke: .asciiz "Knock Knock\nwho is there?" .text la $a0, joke addi $v0, $zero, 4 syscall

Exercise 6b Whilst using .ascii allows a multi-line string such as a menu to be printed out all at once .data menu: .ascii "\n\tMenu item 1" .ascii "\n\tMenu item 2" .ascii "\n\tMenu item 3" .asciiz "\n\n Choose an option ---->" .text la $???, ???? addi $v0, $zero, 4 syscall

6G4Z1102:Computer Systems Fundamentals, Dr. Bob Cherry, L Travis

MIPS: System Calls

Printing out multiple line ASCII STRINGS to the console terminal window and formatting using a “tab” space Using .ascii allows a multi-line string such as a menu to be printed out all at once. The sequence of ascii strings is terminated by using the null terminator .asciiz before the last string. Notice that \t tabs the display

Example :

How To



Display an ASCII multiple line string

.data menu: .ascii "\n\tMenu item 1" .ascii "\n\tMenu item 2" .ascii "\n\tMenu item 3" .asciiz "\n\n Choose an option ---->" .text la $???, ???? addi $v0, $zero, 4 syscall

Example :

How to



Display a multiple line ascii character string : and use the .word function

.data str1: .asciiz "Hello Bob - you are " str2: .asciiz " years old" age: .word 12 .text la $a0, str1 addi $v0, $zero, 4 syscall lw $a0, age add $v0, $zero, 1 syscall la $a0, str2 addi $v0, $zero, 4 syscall

6G4Z1102:Computer Systems Fundamentals, Dr. Bob Cherry, L Travis

MIPS: System Calls

Reading in ascii text characters from the console keyboard Example :

How To



Read in a single ASCII character



Read in a text string

#Put 12 into $v0, using service 12 addi $v0, $zero, 12 #pass control to OS Syscall #char entered is now in $v0

Example :

How to

.data str1: .space 20 dashline: .asciiz "123\n" .text #Going to put input chars into str1 la $a0, str1 #Maximum number of chars in buffer addi $a1,$zero, 20 addi $v0, $zero, 8 syscall la $a0, dashline addi $v0, $zero, 4 syscall la $a0, str1 addi $v0, $zero, 4 syscall

6G4Z1102:Computer Systems Fundamentals, Dr. Bob Cherry, L Travis

MIPS: System Calls

Example :

How to



Input a text string

The code example below works fine in MARS simulator and originated from the following website : https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=2ahUKEwijiKVo4bfAhWTWsA KHRKjCVoQFjAAegQICRAB&url=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F7969565%2Fmips-how-to-store-userinputstring&usg=AOvVaw0iHmc3V71XFgqjy9GxS_0u

.data buffer: .space 20 str1: .asciiz "Enter string " str2: .asciiz "You wrote:\n“ .text main: # Load and print string asking for string la $a0, str1 li $v0, 4 syscall # take in input li $v0, 8

syscode 8

# load byte space into address la $a0, buffer

call

read $a0 = pointer to buffer, string $a1 = length of buffer

# allot the byte space for string li $a1, 20 # save string to t0 move $t0, $a0 syscall # load and print "You wrote" string la $a0, str2 li $v0, 4 syscall # reload byte space to primary address la $a0, buffer # primary address = t0 address (load pointer) move $a0, $t0 # print string li $v0, 4 syscall #end program li $v0, 10 syscall

arguments

results string stored in buffer

just an array of bytes, where each byte is a character, and the final byte is the value 0 and acts as a (null value) terminator. The start location of the array must be known and is generated by the assembler (for you) and is stored in register $a0 using the label you have defined: Here it is “buffer:”. The assembler must also allocate a block of memory with adjacent addresses (and no gaps) available for use by each character. The .space directive instructs the assembler of the size of a block of memory it must allocate which must be kept clear. The length of the array (that will use part or all of the allocated memory space) must be known and is stored in register $a1. [ So, the length of the ASCIIZ encoded (null terminated) array described by the value in $a1, can be smaller than the .space allocated by the value in $a0. This allows the data to be interrogated by other parts of the programme at a later point, and allows other parts of the programme to contribute values into the allocated “string” memory space locations ]

6G4Z1102:Computer Systems Fundamentals, Dr. Bob Cherry, L Travis

MIPS: System Calls

Exercise 7 Write a program to prompt for a number double it and display the result. Usage of the program should be as follows : Please enter a number 22 doubled is

:

22 44

Exercise 8 Write a program to prompt for two numbers to add together and then displays the result. Usage of the program should be as follows : Please enter the first number : 34 Please enter the second number : 21 The sum of the two numbers is 55 -----------------------------------------------------------------------------------

Using Floating point numbers Example :

How to



Multiply real numbers

The technique of handling floating point numbers can be applied to the assignment.

#-----------------------------# lecture facililation code #-----------------------------.data #----------------------------#This is the data segment #----------------------------variable1: .double 3.14 variable2: .double 2.71 .text #------------------------------#This is the body of the code #------------------------------main: #-------------------------------------------------------------# Multiply with reals #-------------------------------------------------------------ldc1 $f2, variable1 ldc1 $f4, variable2 # # $f12 = double to print # add.d $f12, $f2, $f4 # # Print double prescison number to screen # li $v0, 3 syscall # # system call code for exit = 10 # li $v0, 10 syscall

6G4Z1102:Computer Systems Fundamentals, Dr. Bob Cherry, L Travis

MIPS: System Calls

Example :

How to



use floating point numbers

# SOLUTION example for 77 degrees celsius to Farenheit conversion from Lab 1 : using SYSTEM CALL instructions # and floating point numbers .data inputvaluemessage: degrees: endmessage:

.asciiz .asciiz .asciiz

"The temperature in Celcius is " " degrees" "\nThe equivalent temperature in Farenheit is "

celsiustemperature: numerator: denominator: offset:

.float .float .float .float

77 9 5 32

.text main: # Print input message : The input temperature in celsius is : li $v0, 4 la $a0, inputvaluemessage syscall # copy the input temperature from the string "celsiustemperature: .float 77" into $f1 : 77 CELSIUS lwc1 $f1, celsiustemperature # print input value in celsius li $v0, 2 mov.s $f12, $f1 syscall # print "degrees" li $v0, 4 la $a0, degrees syscall # Print input message : The input temperature in celsius is : li $v0, 4 la $a0, endmessage syscall # Now the conversion : first x 9, then / 5, then add 32 # ====================================================== #celsiustemperature * 9 lwc1 $f5, numerator mul.s $f2, $f1, $f5 #/5 lwc1 $f6, denominator div.s $f3, $f2, $f6 #+32 lwc1 $f7, offset add.s $f4, $f3, $f7 # print OUTPUT value in FARENHEIT li $v0, 2 mov.s $f12, $f4 syscall # print "degrees" li $v0, 4 la $a0, degrees syscall #================================ # End the programme in a tidy way #================================ li $v0, 10 syscall

6G4Z1102:Computer Systems Fundamentals, Dr. Bob Cherry, L Travis

MIPS: System Calls

References 1. https://en.wikibooks.org/wiki/MIPS_Assembly/System_Instructions 2. http://courses.missouristate.edu/kenvollmar/mars/help/syscallhelp.html 3. https://en.wikipedia.org/wiki/MIPS_instruction_set 4. http://www.cs.cmu.edu/afs/cs/academic/class/15740-f97/public/doc/mips-isa.pdf 5. http://www.mrc.uidaho.edu/mrc/people/jff/digital/MIPSir.html 6. http://logos.cs.uic.edu/366/notes/mips%20quick%20tutorial.htm 7. In computer programming, a null-terminated stringis a character string stored as an array containing the characters and terminated with a null character( '\0' , called NUL in ASCII). Source : https://en.wikipedia.org/wiki/Null-terminated_string

6G4Z1102:Computer Systems Fundamentals, Dr. Bob Cherry, L Travis

MIPS: System Calls

APPENDIX SYSCALL

extracts from references 1 and 2

--

Software Interrupt

1. https://en.wikibooks.org/wiki/MIPS_Assembly/System_Instructions Instruction :

syscall

code call

arguments

results

1

print integer

$a0 = integer to print

2

print float

$f12 = float to print

3

print double

$f12 = float to print

4

print string

$a0 = address of beginning of string

5

read integer

integer stored in $v0

6

read float

float stored in $f0

7

read double

double stored in $f0

8

read string

9

sbrk (allocate memory buffer)

10

exit

11

Print out ‘Character (s)’

12

Read a single character

$a0 = pointer to buffer, $a1 = length of buffer $a0 = size needed

string stored in buffer $v0 = address of buffer

$a0 $v0

Example: printing the number 12 li $a0, 12→;loads the number we want printed, 12 in this case, into the first argument register li $v0, 1→;s...


Similar Free PDFs