0% found this document useful (0 votes)
139 views14 pages

LearningMaterial ICT4 v6 0 Week5

This document provides information about Laboratory Exercise 5 which focuses on character string manipulation and sorting using SYSCALL functions in MIPS. The goals are to understand storing ASCII and Unicode strings, programming to process and output strings, and sorting a list of elements. It describes the SYSCALL system call mechanism and provides a table of available services including printing data, input/output, files, and dialogs. The services allow manipulating strings and other data.

Uploaded by

Thọ Phạm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views14 pages

LearningMaterial ICT4 v6 0 Week5

This document provides information about Laboratory Exercise 5 which focuses on character string manipulation and sorting using SYSCALL functions in MIPS. The goals are to understand storing ASCII and Unicode strings, programming to process and output strings, and sorting a list of elements. It describes the SYSCALL system call mechanism and provides a table of available services including printing data, input/output, files, and dialogs. The services allow manipulating strings and other data.

Uploaded by

Thọ Phạm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Ha Noi University of Science and Technology

School of Information and Communication Technology

Laboratory Exercise 5
Character string with SYSCALL function, and sorting

Goals
After this laboratory exercise, you should understand the mechanism of storing
ASCII and Unicode string. You will be able to program to process string and put
string to console. In addition, you should know how to sort a list of elements.

Literature
Patterson, Henessy (COD): section 2.8, 2.13

Preparation
Before you start the exercise, you should review the textbook, section 6.1 and read
this laboratory carefully. You should also read the Mips Lab Environment Reference
to find the usage of printf, putchar procedures … and so on.

About SYSCALL
A number of system services, mainly for input and output, are available for use by your
MIPS program. They are described in the table below.

MIPS register contents are not affected by a system call, except for result registers as
specified in the table below.

How to use SYSCALL system services


1. Load the service number in register $v0.
2. Load argument values, if any, in $a0, $a1, $a2, or $f12 as specified.
3. Issue the SYSCALL instruction.
4. Retrieve return values, if any, from result registers as specified.
Example: display the value stored in $t0 on the console
li $v0, 1 # service 1 is print integer
li $a0, 0x307 # the interger to be printed is 0x307
syscall # execute
Ha Noi University of Science and Technology
School of Information and Communication Technology

Table of Frequently Available Services


Service Code Arguments Result
in $v0
print decimal integer 1 $a0 = integer to print
print string 4 $a0 = address of null-
terminated string to
print
read integer 5 $v0 contains integer read
read string 8 $a0 = address of input See note below table
buffer
$a1 = maximum
number of characters
to read
exit 10 (terminate execution)
print character 11 $a0 = character to print See note below table
read character 12 $v0 contains character read
open file 13 $a0 = address of null- $v0 contains file descriptor (negative if
terminated string error). See note below table
containing filename
$a1 = flags
$a2 = mode
read from file 14 $a0 = file descriptor $v0 contains number of characters read (0 if
$a1 = address of input end-of-file, negative if error). See note below
buffer table
$a2 = maximum
number of characters
to read
write to file 15 $a0 = file descriptor $v0 contains number of characters written
$a1 = address of output (negative if error). See note below table
buffer
$a2 = number of
characters to write
close file 16 $a0 = file descriptor
exit2 (terminate with 17 $a0 = termination See note below table
value) result
time (system time) 30 $a0 = low order 32 bits of system time
$a1 = high order 32 bits of system time. See
note below table
MIDI out 31 $a0 = pitch (0-127) Generate tone and return immediately. See
$a1 = duration in note below table
milliseconds
$a2 = instrument (0-
127)
$a3 = volume (0-127)
sleep 32 $a0 = the length of Causes the MARS Java thread to sleep for (at
time to sleep in least) the specified number of milliseconds.
milliseconds. This timing will not be precise, as the Java
implementation will add some overhead.
MIDI out 33 $a0 = pitch (0-127) Generate tone and return upon tone
synchronous $a1 = duration in completion. See note below table
milliseconds
$a2 = instrument (0-
127)
$a3 = volume (0-127)
print integer in 34 $a0 = integer to print Displayed value is 8 hexadecimal digits, left-
hexadecimal padding with zeroes if necessary.
Ha Noi University of Science and Technology
School of Information and Communication Technology

print integer in 35 $a0 = integer to print Displayed value is 32 bits, left-padding with
binary zeroes if necessary.
print integer as 36 $a0 = integer to print Displayed as unsigned decimal value.
unsigned
(not used) 37-39
set seed 40 $a0 = i.d. of No values are returned. Sets the seed of the
pseudorandom number corresponding underlying Java
generator (any int). pseudorandom number generator
$a1 = seed for (java.util.Random). See note below
corresponding table
pseudorandom number
generator.
random int 41 $a0 = i.d. of $a0 contains the next pseudorandom,
pseudorandom number uniformly distributed int value from this
generator (any int). random number generator's sequence. See
note below table
random int range 42 $a0 = i.d. of $a0 contains pseudorandom, uniformly
pseudorandom number distributed int value in the range 0 = [int]
generator (any int). [upper bound], drawn from this random
$a1 = upper bound of number generator's sequence. See note below
range of returned table
values.
ConfirmDialog 50 $a0 = address of null- $a0 contains value of user-chosen option
terminated string that 0: Yes
is the message to user 1: No
2: Cancel
InputDialogInt 51 $a0 = address of null- $a0 contains int read
terminated string that $a1 contains status value
is the message to user 0: OK status
-1: input data cannot be correctly parsed
-2: Cancel was chosen
-3: OK was chosen but no data had been
input into field
InputDialogString 54 $a0 = address of null- See Service 8 note below table
terminated string that $a1 contains status value
is the message to user 0: OK status. Buffer contains the input string.
$a1 = address of input -2: Cancel was chosen. No change to buffer.
buffer -3: OK was chosen but no data had been
$a2 = maximum input into field. No change to buffer.
number of characters -4: length of the input string exceeded the
to read specified maximum. Buffer contains the
maximum allowable input string plus a
terminating null.
MessageDialog 55 $a0 = address of null- N/A
terminated string that
is the message to user
$a1 = the type of
message to be
displayed:
0: error message,
indicated by Error icon
1: information
message, indicated by
Information icon
2: warning message,
indicated by Warning
icon
Ha Noi University of Science and Technology
School of Information and Communication Technology

3: question message,
indicated by Question
icon
other: plain message
(no icon displayed)
MessageDialogInt 56 $a0 = address of null- N/A
terminated string that
is an information-type
message to user
$a1 = int value to
display in string form
after the first string
MessageDialogString 59 $a0 = address of null- N/A
terminated string that
is an information-type
message to user
$a1 = address of null-
terminated string to
display after the first
string

1. print decimal integer


print an integer to standard output (the console).
Argument(s):
$v0 = 1
$a0 = number to be printed
Return value:
none

Example:
li $v0, 1 # service 1 is print integer
li $a0, 0x307 # the interger to be printed is 0x307
syscall # execute
and result is

2. MessageDialogInt
show an integer to a information-type message dialog.
Argument(s):
$v0 = 56
$a0 = address of the null-terminated message string
$a1 = int value to display in string form after the first string
Return value:
none
Ha Noi University of Science and Technology
School of Information and Communication Technology

Example:
.data
Message: .asciiz "So nguyen la "
.text
li $v0, 56
la $a0, Message
li $a1, 0x307 # the interger to be printed is 0x307
syscall # execute
and result is

3. print string
Formatted print to standard output (the console).
Argument(s):
$v0 = 1
$a0 = value to be printed
Return value:
none

Example:
.data
Message: .asciiz "Bomon \nKy thuat May tinh"
.text
li $v0, 4
la $a0, Message
syscall
and result is

4. MessageDialogString
Show a string to a information-type message dialog
Argument(s):
$v0 = 59
$a0 = address of the null-terminated message string
$a1 = address of null-terminated string to display
Return value:
none

Example:
Ha Noi University of Science and Technology
School of Information and Communication Technology

.data
Message: .asciiz "Bomon \nKy thuat May tinh:"
Address: .asciiz " phong 502, B1"
.text
li $v0, 59
la $a0, Message
la $a1, Address
syscall
and result is

5. read integer
Get a integer from standard input (the keyboard).
Argument(s):
$v0 = 5
Return value:
$v0 = contains integer read
Example:
li $v0, 5
syscall
and result is

6. InputDialogInt
Show a message dialog to read a integer with content parser
Argument(s):
$v0 = 51
$a0 = address of the null-terminated message string
Return value:
$a0 = contains int read
$a1 contains status value
0: OK status
-1: input data cannot be correctly parsed
-2: Cancel was chosen
Ha Noi University of Science and Technology
School of Information and Communication Technology

-3: OK was chosen but no data had been input into field
Example:
.data
Message: .asciiz "Nhap so nguyen:”
.text
li $v0, 51
la $a0, Message
syscall
and result is

7. read string
Get a string from standard input (the keyboard).
Argument(s):
$v0 = 8
$a0 = address of input buffer
$a1 = maximum number of characters to read
Return value:
none
Remarks:
For specified length n, string can be no longer than n-1.
 If less than that, adds newline to end.
 In either case, then pads with null byte

Just in special cases:


If n = 1, input is ignored and null byte placed at buffer
address.
If n < 1, input is ignored and nothing is written to the buffer.
Example:
.data
Message: .space 100 # Buffer 100 byte chua chuoi ki tu can
.text
li $v0, 8
la $a0, Message
li $a1, 100
syscall
and result is
Ha Noi University of Science and Technology
School of Information and Communication Technology

8. InputDialogString
Show a message dialog to read a string with content parser
Argument(s):
$v0 = 54
$a0 = address of the null-terminated message string
$a1 = address of input buffer
$a2 = maximum number of characters to read
Return value:
$a1 contains status value
0: OK status
-2: OK was chosen but no data had been input into field.
No change to buffer.
-3: OK was chosen but no data had been input into field
-4: length of the input string exceeded the specified
maximum. Buffer contains the maximum allowable input string
plus a terminating null.
Example:
.data
Message: .asciiz "Nhap so nguyen:”
string: .space 100
.text
li $v0, 54
la $a0, Message
la $a1, string
la $a2, 100
syscall
and result is
Ha Noi University of Science and Technology
School of Information and Communication Technology

9. print character
Print a character to standard output (the console).
Argument(s):
$v0 = 11
$a0 = character to print (at the lowest significant byte)
Return value:
none
Example:
li $v0, 11
li $a0, 'k'
syscall
and result is

10. read character


Get a character from standard output (the keyboard).
Argument(s):
$v0 = 12
Return value:
$v0 contains character read
Example:
li $v0, 12
syscall
and result is

11. ConfirmDialog
Show a message bog with 3 button: Yes | No | Cancel
Argument(s):
$v0 = 50
$a0 = address of the null-terminated message string
Return value:
$a0 = contains value of user-chosen option
0: Yes
1: No
Ha Noi University of Science and Technology
School of Information and Communication Technology

2: Cancel
Example:
.data
Message: .asciiz "Ban la SV Ky thuat May tinh?"
.text
li $v0, 50
la $a0, Message
syscall
and result is

12. MessageDialog
Show a message bog with icon and button OK only
Argument(s):
$v0 = 55
$a0 = address of the null-terminated message string
$a1 = the type of message to be displayed:
0: error message, indicated by Error icon
1: information message, indicated by Information icon
2: warning message, indicated by Warning icon
3: question message, indicated by Question icon
other: plain message (no icon displayed)
Return value:
none
Example:
.data
Message: .asciiz "Xin chao"
.text
li $v0, 55
la $a0, Message
syscall
and result is
Ha Noi University of Science and Technology
School of Information and Communication Technology

13. MIDI out


Make a sound
Argument(s):
$v0 = 31
$a0 = pitch (0-127)
$a1 = duration in milliseconds
$a2 = instrument (0-127)
$a3 = volume (0-127)
Return value:
Generate tone and return immediately
Example:
li $v0, 33
li $a0, 42 #pitch
li $a1, 2000 #time
li $a2, 0 #musical instrusment
li $a3, 212 #volume

14. MIDI out synchronous


Make a sound
Argument(s):
$v0 = 33
$a0 = pitch (0-127)
$a1 = duration in milliseconds
$a2 = instrument (0-127)
$a3 = volume (0-127)
Return value:
Generate tone and return upon tone completion
Example:
li $v0, 33
li $a0, 42 #pitch
li $a1, 2000 #time
li $a2, 0 #musical instrusment
li $a3, 212 #volume
syscall

15. Exit
Terminated the software. Make sense that there is no EXIT instruction in the
Instruction Set of any processors. Exit is a service belongs to Operating System.
Argument(s):
$v0 = 10
Return value:
none
Example:
li $v0, 10 #exit
syscall
Ha Noi University of Science and Technology
School of Information and Communication Technology

16. Exit with code


Terminated the software. Make sense that there is no EXIT instruction in the
Instruction Set of any processors. Exit is a service belongs to Operating System.
Argument(s):
$v0 = 17
$a0 = termination result
Return value:
none
Example:
li $v0, 17 # exit
li $a0, 3 # with error code = 3
syscall

Assignments at Home and at Lab

Home Assignment 1
The following simple assembly program will display a welcome string. We use
printf function for this purpose. Read this example carefully, pay attention to the
way to pass parameters for printf function. Read Mips Lab Enviroment Reference
for details.
#Laboratory Exercise 5, Home Assignment 1
.data
test: .asciiz "Hello World"
.text
li $v0, 4
la $a0, test
syscall

Home Assignment 2
Procedure strcpy copies string y to string x using the null byte termination
convention of C. Read this example carefully, try to understand all of this code
section.
#Laboratory Exercise 5, Home Assignment 2
.data
x: .space 1000 # destination string x, empty
y: .asciiz "Hello" # source string y

.text
strcpy:
add $s0,$zero,$zero #s0 = i=0
L1:
add $t1,$s0,$a1 #t1 = s0 + a1 = i + y[0]
# = address of y[i]
lb $t2,0($t1) #t2 = value at t1 = y[i]
add $t3,$s0,$a0 #t3 = s0 + a0 = i + x[0]
# = address of x[i]
sb $t2,0($t3) #x[i]= t2 = y[i]
beq $t2,$zero,end_of_strcpy #if y[i]==0, exit
nop
Ha Noi University of Science and Technology
School of Information and Communication Technology

addi $s0,$s0,1 #s0=s0 + 1 <-> i=i+1


j L1 #next character
nop
end_of_strcpy:

Home Assignment 3
The following program count the length of a null-terminated string. Read this
example carefully, analyse each line of code.

#Laboratory Exercise 5, Home Assignment 3


.data
string: .space 50
Message1: .asciiz "Nhap xau:”
Message2: .asciiz "Do dai la "
.text
main:
get_string: # TODO

get_length: la $a0, string # a0 = Address(string[0])


xor $v0, $zero, $zero# v0 = length = 0
xor $t0, $zero, $zero# t0 = i = 0
check_char: add $t1, $a0, $t0 # t1 = a0 + t0
#= Address(string[0]+i)
lb $t2, 0($t1) # t2 = string[i]
beq $t2,$zero,end_of_str # Is null char?
addi $v0, $v0, 1 # v0=v0+1->length=length+1
addi $t0, $t0, 1 # t0=t0+1->i = i + 1
j check_char
end_of_str:
end_of_get_length:
print_length: # TODO

Assignment 1
Create a new project to implement the program in Home Assignment 1. Compile
and upload to simulator. Run and observe the result. Go to data memory section,
check how test string are stored and packed in memory.

Assignment 2
Create a new project to print the sum of two register $s0 and $s1 according to this
format:
“The sum of (s0) and (s1) is (result)”

Assignment 3
Create a new project to implement the program in Home Assignment 2. Add more
instructions to assign a test string for y variable, and implement strcpy function.
Compile and upload to simulator. Run and observe the result.

Assignment 4
Accomplish the Home Assignment 3 with syscall function to get a string from
dialog, and show the length to message dialog.
Ha Noi University of Science and Technology
School of Information and Communication Technology

Assignment 5
Write a program that let user input a string. Input process will be terminated when
user press Enter or then length of the string exceed 20 characters. Print the reverse
string.

Conclusions
Before you pass the laboratory exercise, think about the questions below:
 What the difference between the string in C and Java?
 In C, with 8 bytes, how many characters that we can store?
 In Java, with 8 bytes, how many characters that we can store?

You might also like