Introduction to Assembly Language using MIPS | Read and Print out Integers and Strings In MIPS.

Introduction to Assembly Language using MIPS

Introduction

Welcome to the world of assembly language programming using MIPS! Assembly language is a low-level programming language that is used to communicate directly with the computer's hardware. In this tutorial, we will introduce you to the basics of assembly language using the MIPS architecture.

MIPS (Microprocessor without Interlocked Pipeline Stages) is a reduced instruction set computer (RISC) architecture developed by MIPS Technologies. It is widely used in embedded systems and for teaching computer architecture.

Getting Started

To get started with MIPS programming, we will be using the MARS (MIPS Assembler and Runtime Simulator) software. MARS is a Java-based program that provides a virtual platform for running MIPS assembly code. You can download the latest version of MARS from their website here

Once you have downloaded and installed MARS, you can launch the program and begin writing assembly code.

MIPS Instruction Set and Syntax

Before we start writing our program, let's first understand the MIPS instruction set and syntax. MIPS instructions are divided into three categories: arithmetic and logical instructions, memory instructions, and control instructions.

The basic syntax for a MIPS instruction is:

<instruction> <destination>, <source1>, <source2>

The instruction is the operation that is performed, such as add, subtract, or move. The destination is the register where the result of the operation is stored, and source1 and source2 are the registers or values that are used as input for the operation.

Reading and Printing Integers

Now that we have a basic understanding of the MIPS instruction set and syntax, let's move on to reading and printing out integers in MIPS.

To read in an integer value, we will use the li (load immediate) instruction to load the system call code for reading an integer into a register. Then, we will use the syscall instruction to make the system call. Here's an example code:

.data
prompt: .asciiz "Enter an integer: "
.text
main:
    li $v0, 4 # load immediate - system call code for printing a string
    la $a0, prompt # load address - address of the prompt string
    syscall # call the system to print the prompt
    li $v0, 5 # load immediate - system call code for reading an integer
    syscall # call the system to read the integer
    move $t0, $v0 # move the integer value to register $t0

In the above code, we first declare a string prompt that asks the user to enter an integer. Then, we use the li instruction to load the system call code for printing a string into register $v0, and use the la instruction to load the address of the prompt string into register $a0. We then use the syscall instruction to make the system call to print the prompt.

Next, we use the li instruction to load the system call code for reading an integer into register $v0, and use the syscall instruction to make the system call to read the integer. Finally, we use the move instruction to move the integer value from register $v0 to register $t0.

Printing an Integer

To print out the integer value, we can use the li instruction to load the system call code for printing an integer into register $v0, and use the move instruction to move the integer value to register $a0.

Here is an example:

.data
message: .asciiz "Hello, world!\n"   # Define the message to be printed
.text
.globl main                         # Declare the entry point of the program

main:
    li $v0, 4                       # Load the print string system call code into register $v0
    la $a0, message                 # Load the address of the message into register $a0
    syscall                         # Execute the system call
    li $v0, 10                      # Load the exit system call code into register $v0
    syscall                         # Execute the system call to exit the program

In the example above, we first define the string we want to print in the .data section of the program. The message label is followed by the .asciiz directive, which defines a null-terminated ASCII string.

In the .text section of the program, we declare the entry point of the program using the .globl directive. In this case, we name the entry point main.

Inside the main function, we first load the system call code for printing a string into register $v0 using the li instruction. The system call code for printing a string is 4.

Next, we load the address of the message label into register $a0 using the la instruction. Register $a0 is the first argument to the system call, which is the address of the string to print.

Finally, we execute the system call using the syscall instruction. The printed string is automatically terminated by a newline character, which is represented by the escape sequence \n in the string definition.

After printing the string, we load the system call code for exiting the program into register $v0 using the li instruction. The system call code for exiting the program is 10.

Finally, we execute the system call to exit the program using the syscall instruction.

In this tutorial, we've introduced you to assembly language programming using MIPS. We've covered the basics of the language, including data types, memory allocation, and simple input and output operations.

We've also shown you some basic MIPS instructions for printing out integers and strings to the console. These can be useful tools for debugging your code and checking the output of your program.

Assembly language programming can be challenging, but it can also be rewarding to work at such a low level and gain a deeper understanding of how computers work. We hope this tutorial has given you a good introduction to MIPS and inspired you to continue learning about assembly language programming.

If you have any questions or comments, please leave them below. We'd love to hear from you and help you in your journey of learning programming.

Leave a Reply

Your email address will not be published. Required fields are marked *

Request Tutorial