Write an assembly language program that runs under MIPSym. It should input two n
ID: 3812670 • Letter: W
Question
Write an assembly language program that runs under MIPSym. It should input two numbers A and B from the keyboard, then calculate 6 different values as follows:
1) Y1 = A + B
2) Y2 = A – B
3) Y3 = A * B
4) Y4 = A / B
5) Y5 = A << B
6) Y6 = A >> B
The division operation is an integer division so the decimal value is truncated. The << and >> operations correspond to “shift left” and “shift right” respectively. You will need to use the syscall for inputting integers and the syscalls for outputting integers. The syscall for reading is as follows: syscall $read_int # read an integer into register (variable) $v0 The syscall for writing an integer stored in register (variable) $t0 is as follows: mov $a0,$t0 # move the value from register $t0 to register $a0 syscall $print_int # print the integer stored in register $a0 Your program should print each of these 6 values after computing them. Please do NOT use QtSPIM or MARS style syscall conventions. You must use the syscall convention presented in class.
Explanation / Answer
up vote0down vote
subtraction
multiplication
divison
shifting left
shifting right
up vote0down vote
.text main: #print the msg1 li $v0 4 la $a0 msg1 syscall #read the num 1 li $v0 5 syscall sw $v0 a1 #print the msg2 li $v0 4 la $a0 msg2 syscall #read the Num 2 li $v0 5 syscall sw $v0 a2 #print the msg3 li $v0 4 la $a0 msg3 syscall lw $t0 a1 lw $t1 a2 li $v0 1 add $a0 $t0 $t1 sub $a0 $t0 $t1 mul $a0 $t0 $t1 div $a0 $t0 $t1
syscall .data msg1: .asciiz "Enter the first number :" msg2: .asciiz "Enter the second number :" a1 : .word 0 a2 : .word 0 msg3: .asciiz "The sum is = "
subtraction
.data # Program data are placed below the .data directive num1: .word 0 # First integer variable, initialized to 0 num2: .word 0 # Second integer variable, initialized to 0 subs: .word 0 # Variable for storing the sum of the two integers str: .asciiz "Name: Name Surname A.M: Somenumber " # Storing string in variable str str1: .asciiz "Enter first integer: " # str2: .asciiz "Enter second integer: " # finalStr: .asciiz "The result of the substraction is " # .text # Program is placed under the .text directive main: # Standard label in QtSpim for the main program. It should be always used li $v0, 4 la $a0, str # Store string syscall # Use this MIPS command to execute a system call li $v0, 4 la $a0, str1 # Store string syscall # Use this MIPS command to execute a system call li $v0,5 # Read integer syscall # Invoke the operating system. li $v0,1 # Print integer lw $a0,num1 # Load the integer syscall # Invoke the operating system. li $v0, 4 la $a0, str2 # Store string syscall # Use this MIPS command to execute a system call li $v0,5 # Read integer syscall # Invoke the operating system. li $v0,1 # Print integer lw $a1,num2 # Load the integer syscall # Invoke the operating system. sub $s0, $a1, $a0 # Substraction sw $s0, subs # Store the difference in memory (in variable subs) la $a0, finalStr # To print a string, first its address should be stored to register $a0 li $v0, 4 # System call value for print_string. syscall # Use this MIPS command to execute a system call move $a0, $s0 # To print an integer, it should be first stored to register $a0 li $v0, 1 # System call value for print_int li $v0,10 syscall
multiplication
li $a0, 5 li $a1, 3 mult $a0, $a1 mfhi $a2 # 32 most significant bits of multiplication to $a2 mflo $v0 # 32 least significant bits of multiplication to $v0
divison
div $a0, $a1 mfhi $a2 # reminder to $a2 mflo $v0 # quotient to $v0
shifting left
sll d,s,shft # $d gets the bits in $s # shifted left logical # by shft positions, # where 0 shft < 32
shifting right
srl d,s,shft # $d gets the logical # right shift of $s # by shft positions. # shft is a 5-bit # integer, 0 shft < 32
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.