Experiment-2-Arithmetic-Operations

Do following steps to run a simple code in MARS simulator tool.

Step 1: Click on “File” tab at top menu bar and select “New”

Step 2: Paste below code and save the file by some name and “.asm” extension

Step 3: Click on “Run” and select “Assemble”.

Step 4: Click on “Run” and select “Go”.

# MIPS Assembly Code for Basic Arithmetic Operations

.data
    # Data section (for storing variables)
    num1:   .word 10      # First integer
    num2:   .word 5       # Second integer

.text
    # Text section (contains main code)

    # Load values from memory into registers
    lw $t0, num1          # Load num1 into register $t0
    lw $t1, num2          # Load num2 into register $t1

    # Addition
    add $t2, $t0, $t1     # Add values in $t0 and $t1, store result in $t2

    # Subtraction
    sub $t3, $t0, $t1     # Subtract $t1 from $t0, store result in $t3

    # Multiplication
    mul $t4, $t0, $t1     # Multiply values in $t0 and $t1, store result in $t4

    # Exit the program
    li $v0, 10            # System call code 10 for program exit
    syscall

How to see output for above code

When you click on “Go”, then “Register” window will automatically be opened at right side in which you can verify values in registers.

Above code using Intel x86 assembly language.

section .data
    ; Data section (for storing variables)
    num1    dd 10       ; First integer (4 bytes)
    num2    dd 5        ; Second integer (4 bytes)

section .text
    global _start       ; Entry point for the program

_start:
    ; Load values from memory into registers
    mov eax, dword [num1]  ; Load num1 into eax
    mov ebx, dword [num2]  ; Load num2 into ebx

    ; Addition
    add eax, ebx        ; Add ebx to eax (result in eax)

    ; Subtraction
    mov eax, dword [num1]  ; Reload num1 into eax
    sub eax, ebx  ; Subtract ebx from eax (result in eax)

    ; Multiplication
    mov eax, dword [num1]  ; Reload num1 into eax
    mul ebx

    ; Division
    mov eax, dword [num1]  ; Reload num1 into eax
    div ebx                ; eax will hold result, EDX will hold quotient

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Design a site like this with WordPress.com
Get started