Write in the following order a MIPS implementation of Arithmetic Functions.asm :
ID: 3807651 • Letter: W
Question
Write in the following order a MIPS implementation of Arithmetic Functions.asm:
· The sum function and the calling code in main
· The negative function and the calling code in main
· The difference function and the calling code in main
· The product function and the calling code in main
Following a coding strategy of incremental development, write the MIPS code for the sum function, the calling code in main, and verify that it assembles and executes correctly before you proceed to write the code for negative, difference and product.
The purpose of this MIPS assembly language program is to reduce functional abstraction for arithmetic operations to more basic primitives and other functions that you may define, as you make progress. The eligible primitives for this programming exercise include:
· Logical instructions such as: and, or, not, xor
· Shift and rotate instructions such as: sll, srl, ror, rol, sra
· Branch and jump instructions such as: beq, bne, bls, ble, bgt, bge, j, jal, jr
For this assignment, you are constructing arithmetic functions for instructions, so do not use any of the arithmetic instructions as primitives. The exception is that you may use the addi instruction to change the stack pointer register, $sp, in the push and pop operations. Arithmetic Functions asm:
# .include "....macros.asm"
.include "macros.asm"
.text
jal main
li $v0, 10
syscall
.text
#---------------------------------------
# int sum(int augend, int addend )
#---------------------------------------
# {
# int sumNoCarry
sum:
BUILD_ACTIVATION 4
# while ( addend != 0 )
# {
# sumNoCarry = augend XOR addend
# addend = (augend AND addend) * (B)10
# augend = sumNoCarry
# }
# return augend
# }
ReturnSum:
DELETE_ACTIVATION
jr $ra
#-----------------------------------------------------------------------------
#-----------------------------------------------------
#
# int negative( int number )
#
# Returns the 2s complement, (-number)
#
#-----------------------------------------------------
# {
negative:
BUILD_ACTIVATION 0
# return sum( 1, not number )
restoreNegative:
DELETE_ACTIVATION
jr $ra
# }
#---------------------------------------------------------------------------------------
#---------------------------------------------------------------
#
# int difference( int minuend, int subtrhnd )
#
# Returns a value for the difference, (minend - subtrhnd)
#
#---------------------------------------------------------------
# {
difference:
BUILD_ACTIVATION 0
# return sum( minuend, Negative(subtrhnd) )
restoreDifference:
DELETE_ACTIVATION
jr $ra
# }
#---------------------------------------------------------------------------------------
#---------------------------------------------------------------
#
# int product( int mltplcnd, int mltplr )
#
# Returns a value for the product, (mltplcnd * mltplr)
#
#---------------------------------------------------------------
# {
# int partPrd
product:
BUILD_ACTIVATION 4
# partPrd = 0
# while (mltplcnd != 0)
# {
# if odd( mltplr )
# {
# partPrd = sum( partPrd, mltplcnd )
# }
# mltplcnd = mltplcnd * (b)10
# mltplr = mltplr / (b)10
# }
# return partPrd
restoreProduct:
DELETE_ACTIVATION
jr $ra
# }
# -------------------------------------------------------------------------------------
.data
# int num1 = 0x1000
num1: .word 0x1000
# int num2 = 0x61
num2: .word 0x61
# int sum1
sum1: .word 0
# int sum2
sum2: .word 0
# int neg1
neg1: .word 0
# int diff1
diff1: .word 0
# int prod1
prod1: .word 0
name: .asciiz "< your name goes here>"
num1Label: .asciiz " = num1 "
num2Label: .asciiz " = num2 "
sum1Label: .asciiz " = sum1 "
sum2Label: .asciiz " = sum2 "
neg1Label: .asciiz " = neg1 "
diff1Label: .asciiz " = diff1 "
prod1Label: .asciiz " = prod1 "
.text
# void main( )
.globl main
main:
# {
# sum1 = sum( num1, num2 )
PUSHW_ num2
PUSHW_ num1
jal sum
addi $sp, $sp, 8
sw $v0, sum1
# sum2 = sum( 0x20, num2 )
PUSHW_ num2
PUSHI_ 0x20
jal sum
addi $sp, $sp, 8
sw $v0, sum2
# neg1 = negative( num1 )
# diff1 = difference( num1,1 )
# prod1 = product( num1,num2 )
# }
# Console.Write( name )
PUSHA_ name
PRINT_STRING
jal println
DUMP_WITH_LABEL num1, num1Label
DUMP_WITH_LABEL num2, num2Label
DUMP_WITH_LABEL sum1, sum1Label
DUMP_WITH_LABEL sum2, sum2Label
DUMP_WITH_LABEL neg1, neg1Label
DUMP_WITH_LABEL diff1, diff1Label
DUMP_WITH_LABEL prod1, prod1Label
#}
li $v0, 10
syscall
# .include "....dump.asm"
.include "dump.asm"
Explanation / Answer
Hi code for sum function look like below
main: #assume value x is already in $t0, y in $t1
add $a0,$0,$t0 # it is the move function
add $a1,$0,$t1
jal addFunction # call function
add $t4,$0,$v0 # move the return value from $v0 to the register which we want
syscall
addFunction:
addi $sp,$sp,-4 # Stack pointer move
sw $t0, 0($sp) # Previous value store
add $t0,$a0,$a1 # Function Body aadding a0 and a1 and moving to t0
add $v0,$0,$t0 # Result
lw $t0, 0($sp) # Load previous value
addi $sp,$sp,4 # Stack pointer move
jr $ra # return value
and in the similar way we can call other functions as well.Please let us know if you need further more details.
Thanks!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.