Answered step by step
Verified Expert Solution
Link Copied!

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 2. 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 PROC_GREATER_DIGIT_SUM calls the procedure PROC_ADD_DIGITS. Thus, it is necessary to save any caller-saved 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:
1. You are only allowed to use following instructions: addi, lw, sw.
2. 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 4 such blocks in the file stack.asm.
3. 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 PROC_GREATER_DIGIT_SUM
.globl PROC_ADD_DIGITS
# -----------------------------------------------------------------------------
# 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.
# digit_sum(432)==9
# If they have equal digit sums, return -1
#
# Pre:
# - $a0 contains the first positive integer
# - $a1 contains the second positive integer
# Post:
# - $a0 contains the integer with the greater digit sum
# -----------------------------------------------------------------------------
PROC_GREATER_DIGIT_SUM:
# -- START EDITING HERE --
# -- END EDITING HERE --
addi s0, a0,0 # Save the first integer
addi s1, a1,0 # Save the second integer
# Sum digits of first integer and save to t0
addi a0, s0,0
jal ra, PROC_ADD_DIGITS
addi t0, a0,0
# -- START EDITING HERE --
# -- END EDITING HERE --
# Sum digits of second integer and save to t1
addi a0, s1,0
jal ra, PROC_ADD_DIGITS
addi t1, a0,0
# -- START EDITING HERE --
# -- END EDITING HERE --
# Assume the first integer has the greater digit sum
addi a0, s0,0
# Check our assumption
beq t0, t1, PROC_GDS_EQUAL
blt t0, t1, PROC_GDS_SECOND_INTEGER_GREATER
j PROC_GDS_EXIT
PROC_GDS_EQUAL:
li a0,-1
j PROC_GDS_EXIT
PROC_GDS_SECOND_INTEGER_GREATER:
addi a0, s1,0
PROC_GDS_EXIT:
# -- START EDITING HERE --
# -- END EDITING HERE --
jr ra,0
# -----------------------------------------------------------------------------
# Given a positive integer, return the sum of its digits.
#
# Pre:
# - $a0 contains the positive integer
# Post:
# - $a0 contains the integer's digit sum
# -----------------------------------------------------------------------------
PROC_ADD_DIGITS:
li t0,0 # Accumulator
li t1,10 # Constant 10 for divrem
li t2,0 # Temporary to store rem result
PROC_AD_LOOP:
rem t2, a0, t1
add t0, t0, t2
ble a0, t1, PROC_AD_EXIT
div a0, a0, t1
j PROC_AD_LOOP
PROC_AD_EXIT:
addi a0, t0,0
jr ra,0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Flash XML Applications Use AS2 And AS3 To Create Photo Galleries Menus And Databases

Authors: Joachim Schnier

1st Edition

0240809173, 978-0240809175

More Books

Students also viewed these Databases questions

Question

how to dilute 1mg/ml form stock to 100 nM

Answered: 1 week ago

Question

1. How can evolutionary theory explain prosocial behaviour?

Answered: 1 week ago

Question

1. Define and explain culture and its impact on your communication

Answered: 1 week ago