Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

University of Management and Technology

Department of Artificial Intelligence


CC2302L Computer Organization and Assembly Language Lab
Spring 2024
Participant ID: Participant Name:

Date: Total Marks: Obtained Marks:

Lab # 02: Introduction to input/output Instructions


1. Learning Objectives
 Explain the role of system calls in facilitating input and output operations within MIPS32
programs
 Identify the key elements involved in initiating system calls for MIPS32 input and output
2. Introduction
MIPS32 doesn't have dedicated instructions for input and output since it relies on system calls
provided by the operating system. These system calls are essentially function calls that trigger
the OS to perform specific tasks, like getting user input or displaying data on the screen. Here's
how input and output work in MIPS32:
1.1. Input
1.1.1. System Calls
The syscall instruction is used to initiate a system call. There are specific system call codes
for different tasks, including reading input.
1.1.2. Arguments
Before the syscall instruction, specific registers are loaded with arguments required by the
system call. For reading an integer, a register might hold the memory address for storing the
input value.
1.1.3. $v0 Register
The $v0 register typically holds the system call code for the desired operation. In the case of
reading an integer, it would have the specific code for that function.
1.2. Output
1.2.1. System Calls
Similar to input, system calls are used for output functions like printing integers or strings. Each
operation has a dedicated code.
1.2.2. Arguments
Registers are loaded with arguments depending on the system call. For printing an integer, $v0
might hold the code and another register might contain the value to be printed.
1.2.3. $v0 Register
Similar to input, $v0 holds the system call code for the output operation.

3. MIPS32 system calls


MIPS32 system calls for output, input, memory management and exit along with their functions
and register usage are given below:
1.3. Output
1.3.1. print_int (code 1)
 Prints an integer to the console.
 Loads $a0 with the integer to be printed.
li $v0, 1 # Load syscall code for print_int
li $a0, integer # Load the integer to be printed into $a0
syscall
1.3.2. print_float (code 2):
 Prints a floating-point number to the console.
 Loads $f12 with the float to be printed.
li $v0, 2 # Load syscall code for print_float
li.s $f12, float # Load the float into $f12 (use .s for single
precision)
syscall
1.3.3. print_double (code 3):
 Prints a double-precision floating-point number to the console.
 Loads $f12 with the double to be printed.
li $v0, 3 # Load syscall code for print_double
li.d $f12, double # Load the double-precision number into $f12
(use .d for double)
syscall
1.3.4. print_string (code 4):
 Prints a string to the console.
 Loads $a0 with the memory address of the string.
li $v0, 4 # Load syscall code for print_string
la $a0, string_var # Load the address of the string into $a0 (use la
for pseudo-load)
syscall
1.3.5. print_byte (code 11):
 Prints a single byte to the console.
 Loads $a0 with the byte to be printed.
li $v0, 11 # Load syscall code for print_byte
li $a0, byte_value # Load the byte to be printed into $a0
syscall
1.4. Input
1.4.1. read_int (code 5):
 Reads an integer from the console.
 Returns the integer in $v0.
li $v0, 5 # Load syscall code for read_int
syscall # Read integer from console
# Integer is returned in $v0
1.4.2. read_float (code 6):
 Reads a floating-point number from the console.
 Returns the float in $v0.
li $v0, 6 # Load syscall code for read_float
syscall # Read float from console
# Float is returned in $v0
1.4.3. read_double (code 7):
 Reads a double-precision floating-point number from the console.
 Returns the double in $v0.
1.4.4. read_string (code 8):
 Reads a string from the console.
 Takes two arguments:
o $a0: Memory address of the string input buffer.
o $a1 Length of the string buffer (n).
li $v0, 8 # Load syscall code for read_string
la $a0, buffer_addr # Load address of input buffer into $a0
li $a1, buffer_size # Load length of the buffer into $a1
syscall # Read string from console (up to buffer size)
1.4.5. read_byte (code 12):
 Reads a single byte from standard input.
 Returns the byte in $v0.
li $v0, 12 # Load syscall code for read_byte
syscall # Read a byte from console
# The read byte is stored in $v0
1.5. Memory Management
1.5.1. sbrk (code 9):
 Allocates memory from the heap.
 Takes $a0 as the amount of memory to allocate.
 Returns the address of the allocated memory in $v0.
li $v0, 9 # Load syscall code for sbrk
li $a0, allocation # Load the amount of memory to allocate
syscall # Allocate memory and store address in $v0
# $v0 will contain the address of the allocated memory
1.6. Program Termination
1.6.1. exit (code 10):
 Terminates the MIPS program.
li $v0, 10 # Load syscall code for exit
li $a0, exit_code # (Optional) Load exit code into $a0 (ignored by
some systems)
syscall # Terminate the program

4. Load/Store Instruction in MIPS


MIPS32 is a load-store architecture, meaning all data transfers between registers and memory
must be done explicitly using load and store instructions. Other instructions like arithmetic or
logical operations work solely on register operands.
These instructions use byte offsets for memory access. The actual memory address is calculated
by adding the base address (often stored in a register) with the byte offset specified in the
instruction.
1.7. Load Instructions
1.7.1. lw (load word)
This instruction copies a word (4 bytes) from the memory location specified by the source
address (RAM_source) into a destination register (register_destination).
# Load the word at memory address 100 into register $t0
lw $t0, 100
1.7.2. lb (load byte)
This instruction copies a single byte from the memory location specified by the source address
(RAM_source) and places it in the lowest-order byte of the destination register
(register_destination). The remaining bytes in the destination register are unaffected.
# Load the byte at memory address 200 into the lowest byte of
register $t1
lb $t1, 200
1.8. Store Instructions
1.8.1. sw (store word)
This instruction stores the word data from a source register (register_source) into the memory
location specified by the destination address (RAM_destination).
# Store the word in register $s1 into memory address 50
sw $s1, 50
1.8.2. sb (store byte)
This instruction stores the lowest-order byte from a source register (register_source) into the
memory location specified by the destination address (RAM_destination).
# Store the lowest byte of register $a0 into memory address 30
sb $a0, 30
1.9. Immediate Load
1.9.1. li (load immediate)
This instruction loads an immediate value (a constant) directly into a destination register
(register_destination). This allows you to pre-load values into registers without needing to access
memory.
# Load the immediate value 15 into register $v0
li $v0, 15

# Load the immediate value -10 (use negative sign) into register $t2
li $t2, -10

5. Task # 1: Program to prompt and read an integer from a user


# Program to read an integer number from a user, and print that number
back to the console.
.text
main:
# Prompt for the integer to enter
li $v0, 4
la $a0, prompt
syscall

# Read the integer and save it in $s0


li $v0, 5
syscall

move $s0, $v0


# Output the text
li $v0, 4
la $a0, output
syscall

# Output the number


li $v0, 1
move $a0, $s0
syscall

# Exit the program


li $v0, 10
syscall
.data
prompt: .asciiz "Please enter an integer: "
output: .asciiz "\nYou typed the number "
1.10. Program Explanation
1.10.1. Text Segment (.text):
main: This label marks the beginning of the program's main function.

1.10.2. Prompt for the integer to enter:


 li $v0, 4 - Loads the system call code for print_string (code 4) into $v0.
 la $a0, prompt - Loads the address of the string stored in the prompt variable
(".asciiz directive") into $a0.
 syscall - Triggers the system call to print the string pointed to by $a0. This prints
"Please enter an integer: " to the console.
1.10.3. Read the integer and save it in $s0:
 li $v0, 5 - Loads the system call code for read_int (code 5) into $v0.
 syscall - Triggers the system call to read an integer from the console. The integer
value is returned in $v0.
 move $s0, $v0 - Moves the integer value from $v0 (where read_int stores it) to
register $s0 for further use.
1.10.4. Output the text:
 li $v0, 4 - Similar to before, loads the system call code for print_string (code 4)
into $v0.
 la $a0, output - Loads the address of the string stored in the output variable
(".asciiz directive") into $a0.
 syscall - Triggers the system call to print the string pointed to by $a0. This prints
"\nYou typed the number " to the console.
1.10.5. Output the number:
 li $v0, 1 - Loads the system call code for print_int (code 1) into $v0.
 move $a0, $s0 - Moves the integer value from $s0 (where it was stored after
reading) to $a0. This register is used by print_int to print the value.
 syscall - Triggers the system call to print the integer value in $a0. This prints the
entered integer to the console.
1.10.6. Exit the program:
 li $v0, 10 - Loads the system call code for exit (code 10) into $v0.
 syscall - Triggers the system call to terminate the program.
1.11. Data Segment (.data):
 prompt: This label defines a string literal "Please enter an integer: " using the .asciiz
directive. This string is used for the prompt displayed to the user.
 output: This label defines a string literal "\nYou typed the number " using the .asciiz
directive. This string is used as the prefix before printing the entered integer.
1.12. Observation in MARS
1. Assemble the program. Please observe the data segment. Enable ASCII option so that you
can see ASCII characters. Write the ASCII values of data until you get \0.
Address Value(+0) Value(+4) Value(+8) Value(+c) Value(+10) Value(+14) Value(+18) Value(+1c)
0x1001000 a e l p
0x1001020 \0
0x1001040
0x1001060

2. Why the message is not in correct order? Such as “Please enter an integer”

3. Run one step at a time and observe the values of following registers.

Program Counter Register Name Register Number Value (Hex)


0x00400000
0x00400004 $V0 2 0x0000 0004
0x00400008
0x0040000c
0x00400010
0x00400014
0x00400018
0x0040001c
0x00400020
0x00400024
0x00400028
0x0040002c
0x00400030
0x00400034
Program Counter Register Name Register Number Value (Hex)
0x00400038
0x0040003c

4. The instruction la $a0, prompt is translated into two instruction. Which instruction and
why?

5. Why li $v0, 5 is translated into addiu, $2, $0, 0x00000005?

6. The instruction move $a0, $s0 is translated into which instruction?

7. Why the source code is being started from Line # 5 and ending at Line # 26 in the below
given figure? However, the MIPS code written in editor started from Line # 1 and ends at
Line # 29?
8. What is machine code (hex) of syscall?

6. Task # 2: Program to prompt and read a string from a user


# Program to read a string from a user, and print that string back to
the console.
.text
main:
# Prompt for the string to enter
li $v0, 4
la $a0, prompt
syscall

# Read the string.


li $v0, 8
la $a0, input
lw $a1, inputSize
syscall

# Output the text


li $v0, 4
la $a0, output
syscall

# Output the number


li $v0, 4
la $a0, input
syscall

# Exit the program


li $v0, 10
syscall
.data
input: .space 81
inputSize: .word 80
prompt: .asciiz "Please enter an string: "
output: .asciiz "\nYou typed the string: "

8.1. Program Explanation


8.1.1. Text Segment (.text):
 main: This label marks the beginning of the program's main function.

8.1.2. Prompt for Input


 li $v0, 4 loads the system call code for print_string (code 4) into $v0.
 la $a0, prompt loads the address of the prompt string ("Please enter an string:
") into $a0.
 syscall triggers the system call to print the prompt to the console.
8.1.3. Read String
 li $v0, 8 loads the system call code for read_string (code 8) into $v0.
 la $a0, input loads the address of the input buffer where the string will be
stored into $a0.
 lw $a1, inputSize loads the maximum number of characters to read (80)
from the inputSize variable into $a1.
 syscall triggers the system call to read a string from the console and store it
in the input buffer.
8.1.4. Output Text
 li $v0, 4 loads the print_string code again to print another string.
 la $a0, output loads the address of the output string ("You typed the string: ")
into $a0.
 syscall triggers the system call to print this string to the console.
8.1.5. Output String
 move $a0, input moves the address of the input buffer (containing the user-
entered string) into $a0.
 li $v0, 4 loads the print_string code to print the string.
 syscall triggers the system call to print the string from the input buffer.
8.1.6. Exit
 li $v0, 10 loads the system call code for exit (code 10) into $v0.
 syscall triggers the system call to terminate the program.
8.1.7. Data Segment (.data)
 input: This label defines a block of memory with 81 bytes (.space 81) to
store the string entered by the user.
 inputSize: This label stores the maximum number of characters to read
(.word 80).
 prompt: This label holds the prompt string ("Please enter an string: ").
 output: This label holds the output string ("You typed the string: ").
8.2. Observation in MARS
1. When you assemble the above program, why the message (Please enter a string) does not
starts from Address 0x1001 0000?

2. Run one step at a time and observe the values of following registers.

Program Counter Register Name Register Number Value (Hex)


0x00400000
0x00400004
0x00400008
0x0040000c
0x00400010
0x00400014
0x00400018
0x0040001c
0x00400020
0x00400024
0x00400028
0x0040002c
0x00400030
0x00400034
0x00400038
0x0040003c
0x00400040
0x00400044
Program Counter Register Name Register Number Value (Hex)
0x00400048
0x0040004c

3. Enter a string and observe where it is stored in data memory and why?
Address Value(+0) Value(+4) Value(+8) Value(+c) Value(+10) Value(+14) Value(+18) Value(+1c)
0x100100
0
0x100102
0
0x100104
0
0x100106
0

4. When the required string is entered, in which registers its data address is stored?

Task # 3:
1. Prompt User to Enter 1st (1-digit) number
2. Prompt User to Enter 2nd (1-digit) number
3. Add two numbers
4. Display result in the following format:

“The sum of 2 and 7 is 9”

Task # 4:
1. Prompt user to enter a lower case letter character
2. Convert it into Upper Case character
3. Output the result in the following format:

“Lower case f converted to upper case F”

You might also like