Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Part 2 . The Stack In this part of the assignment, you are given the file stack.asm, containing simple procedures to compare the digit sum
Part The Stack
In this part of the assignment, you are given the file stack.asm, containing simple procedures to compare the digit sum of two integers. The details of the algorithm in the procedures are irrelevant to the assignment, but you are free to investigate and understand the algorithm used.
Your task is to save and retrieve any necessary registers on the stack. Note that the procedure PROCGREATERDIGITSUM calls the procedure PROCADDDIGITS. Thus, it is necessary to save any callersaved registers used in the caller before calling a procedure, and restore the saved registers after the call to the procedure.
Specification
Your solution must follow the following specification:
You are only allowed to use following instructions: addi, lw sw
You are only allowed to write assembly code within the block delimited by comments containing the words START EDITING HERE and END EDITING HERE. There are such blocks in the file stack.asm.
Do not modify any other line in the file. Do not modify the lines containing START EDITING HERE and END EDITING HERE. Failure to follow the instructions will result in a grade penalty.
data
text
globl PROCGREATERDIGITSUM
globl PROCADDDIGITS
#
# Given two positive integers, return the integer with the greater digit sum.
# The digit sum of an integer is the sum of its digits, ie
# digitsum
# If they have equal digit sums, return
#
# Pre:
# $a contains the first positive integer
# $a contains the second positive integer
# Post:
# $a contains the integer with the greater digit sum
#
PROCGREATERDIGITSUM:
# START EDITING HERE
# END EDITING HERE
addi s a # Save the first integer
addi s a # Save the second integer
# Sum digits of first integer and save to t
addi a s
jal ra PROCADDDIGITS
addi t a
# START EDITING HERE
# END EDITING HERE
# Sum digits of second integer and save to t
addi a s
jal ra PROCADDDIGITS
addi t a
# START EDITING HERE
# END EDITING HERE
# Assume the first integer has the greater digit sum
addi a s
# Check our assumption
beq t t PROCGDSEQUAL
blt t t PROCGDSSECONDINTEGERGREATER
j PROCGDSEXIT
PROCGDSEQUAL:
li a
j PROCGDSEXIT
PROCGDSSECONDINTEGERGREATER:
addi a s
PROCGDSEXIT:
# START EDITING HERE
# END EDITING HERE
jr ra
#
# Given a positive integer, return the sum of its digits.
#
# Pre:
# $a contains the positive integer
# Post:
# $a contains the integer's digit sum
#
PROCADDDIGITS:
li t # Accumulator
li t # Constant for divrem
li t # Temporary to store rem result
PROCADLOOP:
rem t a t
add t t t
ble a t PROCADEXIT
div a a t
j PROCADLOOP
PROCADEXIT:
addi a t
jr ra
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started