51.0k views
5 votes
Write a MIPS assembly language program that adds the following two integers and displays the sum and the difference. In the .data section, define two variables num1 and num2 both words. Initialize num1 to 50274 10and num2 to CB7 16 (use 0xCB7 to initialize). Your main procedure should load the values of num1 and num2 into two temporary registers, and display them on the console window. Then add the values together, and use the print_int system call function to display the sum on the console window.

Also compute the difference of two numbers and display it on the console window. To print an integer on the console window, you must put the integer to be printed in the $a0 register, and the value 1 in the $v0 register. Then perform a syscall operation. This makes a call to the PCSPIM operating system which will display the integer in $a0 on the console window. Name your source code file assignment2.s.

Your output format should look as follows:

num1 is: XXX
num2 is: XXX
num1+num2 = XXX
num1-num2 = XXX

where XXX is the correct number for each.

1 Answer

1 vote

Answer:

The Assembly code is given below with appropriate comments

Step-by-step explanation:

#Data Section

.data

#Display the Labels

number1: .asciiz "num1 is : "

number2: .asciiz "\\num2 is : "

addition: .asciiz "\\num1 + num2 = "

subtraction: .asciiz "\\num1 - num2 = "

#variable declarations

num1: .word 50274

num2: .word 0xCB7

.text

#Main method

main :

# print the label number 1

li $v0,4

la $a0,number1

syscall

# print the value

li $v0,1

la $a0,num1

lw $a0,($a0)

syscall

# print the label number2

li $v0,4

la $a0,number2

syscall

# print the value

li $v0,1

la $a0,num2

lw $a0,($a0)

syscall

#Compute addition of two numbers

la $t0,num2

lw $t0,($t0)

la $t1,num1

lw $t1,($t1)

add $t0,$t1,$t0

# print the label

li $v0,4

la $a0,addition

syscall

# print the value

li $v0,1

addi $a0,$t0,0

syscall

#Compute subtraction of two numbers

la $t0,num2

lw $t0,($t0)

la $t1,num1

lw $t1,($t1)

sub $t0,$t1,$t0

# print the label3

li $v0,4

la $a0,subtraction

syscall

# print the value

li $v0,1

addi $a0,$t0,0

syscall

User Joz
by
4.2k points